ggplot2: missing legend and how to add?

被刻印的时光 ゝ 提交于 2020-01-11 10:31:52

问题


I want to plot a bar and line chart from a dataframe. Code below,

library("ggplot2")

numb <- c(1,2,3,4,5,6,7,8,9)
mydist <- c(53.846154,15.384615,15.384615,7.692308,7.692308,0,0,0,0)
basedist <- c(30.103,17.609126,12.493874,9.691001,7.918125,6.694679,5.799195,5.115252,4.575749)
df <- data.frame(numb, mydist, basedist)

ggplot(data=df,aes(x=numb)) + 
  geom_bar(stat="identity", aes(y=mydist), colour="green", fill="green") +
  geom_line(aes(y=basedist,group=1, colour="base distribution")) +  
  geom_point(aes(y=basedist), colour="red") +
  ggtitle("My Chart") +
  labs(x="numb", y="percentage") +
  scale_x_discrete(limits=c("1","2","3","4","5","6","7","8","9")) +
  scale_y_continuous(breaks=seq(0,100,10)) +
  theme(axis.title.x = element_text(size=10, colour ="#666666")) +
  theme(axis.title.y = element_text(size=10, color="#666666")) +
  theme(plot.title = element_text(size=16, face="bold", hjust=0, color="#666666")) +
  theme(axis.text = element_text(size=12)) +
  theme(legend.title = element_text(colour="white", size = 16, face='bold'))

Result is not I wanted because there is no legend for the bars

I reproduced the chart I need with the same data set in Excel below,

What do I need to change in my code to get the chart I need?

Thanks, Lobbie


回答1:


Without changing much of the original code, you only need to put your fill into aes mapping, then add the scale to set the colour values and labels:

ggplot(data=df,aes(x=numb)) + 
  geom_bar(stat="identity", aes(y=mydist, fill="green"), colour="green") +
  geom_line(aes(y=basedist,group=1, colour="base distribution")) +  
  geom_point(aes(y=basedist), colour="red") +
  ggtitle("My Chart") +
  labs(x="numb", y="percentage") +
  scale_x_discrete(limits=c("1","2","3","4","5","6","7","8","9")) +
  scale_y_continuous(breaks=seq(0,100,10)) +
  scale_fill_manual(values = "green", labels = "my distribution") +
  theme(axis.title.x = element_text(size=10, colour ="#666666")) +
  theme(axis.title.y = element_text(size=10, color="#666666")) +
  theme(plot.title = element_text(size=16, face="bold", hjust=0, color="#666666")) +
  theme(axis.text = element_text(size=12)) +
  theme(legend.title = element_text(colour="white", size = 16, face='bold'))




回答2:


Here is a brief example. In general, I would recommend you reformat ggplot() assignment to make debugging easier. i.e. gp <- gp +

gp <- ggplot(data=df, aes(x=numb))
gp <- gp + geom_bar( aes(y = mydist, fill = "green"), stat="identity", color="green")
gp <- gp + geom_line( aes( y = basedist, group = 1, colour = "base distribution"))
gp <- gp + scale_fill_manual(values = "green", labels = "my distribution")
gp <- gp + geom_point(aes(y=basedist), colour="red") 
gp <- gp + ggtitle("My Chart") 
gp <- gp + labs(x="numb", y="percentage") 
gp <- gp + scale_x_discrete(limits=c("1","2","3","4","5","6","7","8","9")) 
gp <- gp + scale_y_continuous(breaks=seq(0,100,10)) 
gp <- gp + theme(axis.title.x = element_text(size=10, colour ="#666666"))
gp <- gp + theme(axis.title.y = element_text(size=10, color="#666666"))
gp <- gp + theme(plot.title = element_text(size=16, face="bold", hjust=0, color="#666666"))
gp <- gp + theme(axis.text = element_text(size=12)) 
gp <- gp + theme(legend.title = element_text(colour="white", size = 16, face='bold'))
gp <- gp + theme(legend.key = element_blank(), legend.title=element_blank(), legend.box  ="vertical")
gp



来源:https://stackoverflow.com/questions/37267081/ggplot2-missing-legend-and-how-to-add

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!