Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot2绘制了两个图例_R_Ggplot2_Legend_Legend Properties - Fatal编程技术网

R ggplot2绘制了两个图例

R ggplot2绘制了两个图例,r,ggplot2,legend,legend-properties,R,Ggplot2,Legend,Legend Properties,我几乎完成了下面的图表,但有一个问题 图表中的图例绘制了两次 以下是数据: structure(list(Period = c("January 1997 - August 2003", "September 2003 - Jun 2005", "Jul 2005 - Dec 2009", "January 1997 - August 2003", "September 2003 - Jun 2005", "Jul 2005 - Dec 2009"), Time.Period = struc

我几乎完成了下面的图表,但有一个问题

图表中的图例绘制了两次

以下是数据:

structure(list(Period = c("January 1997 - August 2003", "September 2003 - Jun 2005", 
"Jul 2005 - Dec 2009", "January 1997 - August 2003", "September 2003 - Jun 2005", 
"Jul 2005 - Dec 2009"), Time.Period = structure(c(1L, 3L, 2L, 
 1L, 3L, 2L), .Label = c("Jan 1997 - Aug 2003", "Jul 2005 - Dec 2009", 
 "Sep 2003 - Jun 2005"), class = "factor"), Variable = structure(c(2L, 
 2L, 2L, 1L, 1L, 1L), .Label = c("Significant", "Zscore"), class = "factor"), 
 Score = c(8.798129, 4.267268, 7.280275, 1.64, 1.64, 1.64)), .Names = c("Period", 
 "Time.Period", "Variable", "Score"), class = "data.frame", row.names = c(NA, 
 -6L))

ggplot(glomor, aes(x=Time.Period, y=Score, group=Variable, shape=Variable, color=Variable)) +
 geom_point() +
 guides(fill=FALSE) +
 scale_x_discrete(limits=c("Jan 1997 - Aug 2003","Sep 2003 - Jun 2005","Jul 2005 - Dec 2009"),    expand=c(.08,0)) +
  geom_line(aes(linetype=Variable), size=1.5) +
  geom_point(size=4.2) +
  scale_linetype_manual(values=c(1,3)) +
  scale_color_manual(values=c("black", "grey40"), name="", labels=c("Signficant Z-Score",   "Moran's I Z-Score")) +
  scale_fill_discrete(name="", label=c("Signficant Z-Score", "Moran's I Z-Score")) +
  theme_classic()+
  ylim(0,10) +
  xlab("Time Periods") +
  ylab("Moran's I Z-Score") +
  theme(axis.title.x=element_text(size=14)) +
  theme(axis.title.y=element_text(size=14)) +
  theme(legend.position=c(.75, .85)) +
  theme(legend.background = element_rect(fill="white")) +
  theme(legend.key = element_blank())

有人知道为什么ggplot2会产生两个传说吗

映射到
变量的美学有三种:形状、颜色和线型。当图例具有相同的标题和标签时,它们会一起折叠。您已经为颜色将标题设置为空白,并为其提供自定义标签(“显著Z分数”和“莫兰I Z分数”)。您还需要对线型和形状执行此操作,以使它们全部折叠在一起

改变

scale_linetype_manual(values=c(1,3)) +

scale_shape_discrete(name="", label=c("Signficant Z-Score", "Moran's I Z-Score")) +
(您也可以去掉
比例填充离散
,因为您实际上没有在任何地方使用填充美学。)

这给

请使用
dput(您的_数据)
并粘贴输出。在
scale\u color
中,您将比例名称设置为
,但要将颜色和形状比例合并为一个,它必须相同。或者我相信
指南(guide=“none”)
也可以工作?@Arun我没有粘贴图片的特权,这张图的输出是什么case@SimonO101我以前试过使用指南,但没有成功。非常感谢。这就是答案,从直觉上讲,这是有道理的。
scale_shape_discrete(name="", label=c("Signficant Z-Score", "Moran's I Z-Score")) +