ggplot2:图例丢失以及如何添加?

4

我想从数据框中绘制条形图和折线图。以下是代码,

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

结果不是我想要的,因为条形图没有图例

enter image description here

我在下面的Excel中使用相同的数据集复制了我需要的图表, enter image description here 我需要更改我的代码以获取所需的图表吗?
谢谢, Lobbie
2个回答

5

这里有一个简单的例子。通常,我建议您重新格式化ggplot()分配以使调试更容易。例如: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

enter image description here


谢谢你的答案。如果我可以接受两个答案,我会采纳你的建议。再次感谢。 - Lobbie
Lobbie - 没问题。很高兴能帮忙,积分只是附带的。保重。 - Technophobe01

4

在不改变原始代码的情况下,您只需要将您的fill放入aes映射中,然后添加scale以设置颜色标签

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

enter image description here


1
@zyurnaidi- 很好的例子 - 你比我快了 :-) - Technophobe01
1
@zyumaidi,谢谢!我现在明白我哪里错了。 - Lobbie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接