Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
532 views
in Technique[技术] by (71.8m points)

r - ggplot2 draws two legends

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...