ggplot2绘制两个传说 [英] ggplot2 draws two legends

查看:149
本文介绍了ggplot2绘制两个传说的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了下面的图表,但是有一个问题。

图中的图例绘制两次。



以下是数据:

  structure(list(Period = c(1997年1月 -  2003年8月,2003年9月 -  2005年6月,
2005年7月 - 2009年12月,1997年1月 - 2003年8月, 2005年7月 - 2009年12月),Time.Period = structure(c(1L,3L,2L,
1L,3L,2L),.Label = c(Jan 1997 - Aug 2003,Jul 2005 - 2009年12月,
2003年9月 - 2005年6月),class =factor),Variable = structure(c(2L,
2L,2L,1L,1L,1L) =(显着,Zscore),class =factor),
Score = c(8.798129,4.226768,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(1997年1月 - 2003年8月,2003年9月 - 2005年6月,2005年7月 - 2009年12月),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 (Moran's I Z-Score)+
主题(axis.title.x = element_text(size = 14))+
theme(axis.title.y =元素文字(size = 14))+
主题(legend.position = c(.75,.85))+
主题(legend.background = element_rect(fill =white))+
主题(legend.key = element_blank())

有谁知道,为什么ggplot2会产生两个传说?

解决方案

您有三种美学映射到 Variable :shape,颜色和线型。当他们拥有相同的标题和标签时,图例一起折叠。您已将标题设置为空白,并为其指定了自定义标签(重要Z分数和Moran's I Z分数)。您需要为线型和形状做到这一点,以便让它们一起折叠。



更改

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

  scale_linetype_manual(values = c(1,3),name =,labels = c(重要的Z值)Moran's I Z-Score))+ 

并添加

  scale_shape_discrete(name =,label = c(Signficant Z-Score,Moran's I Z-Score))+ 

(您也可以去掉 scale_fill_discrete

这给出


I have almost complete the following graph, but there is one problem with it.

The legend in the graph is drawn twice.

Here is the data:

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())

Does anyone know, why the ggplot2 produces two legends?

解决方案

You have three aesthetics that are mapped to Variable: shape, colour, and linetype. Legends are collapsed together when they have the same title and labels. You have set the title to blank for colour and given it custom labels ("Significant Z-Score" and "Moran's I Z-Score"). You need to do that for linetype and shape as well to get them to collapse all together.

Change

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

to

scale_linetype_manual(values=c(1,3), name="", labels=c("Signficant Z-Score",   "Moran's I Z-Score")) +

and add

scale_shape_discrete(name="", label=c("Signficant Z-Score", "Moran's I Z-Score")) +

(You can also get rid of the scale_fill_discrete because you don't actually use the fill aesthetic anywhere.)

This gives

这篇关于ggplot2绘制两个传说的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆