关键是使用填充而不是颜色。代码将生成警告但将正确显示。验证图例是否为预期结果。
库(GGPLOT2)
ggplot(data = rr, aes(x = date))+ geom_point(aes(y=value, fill="Nitrogen"), colour="black")+ geom_line(aes(y=value, fill="Nitrogen"), colour="black")+ facet_grid(depth~dose) + geom_bar(data=rr,stat = "identity", position = position_dodge(width = 0.9),aes(x=date, y=rain100, fill="Rains") )+ theme(strip.text.x = element_text(size = 7, colour = "black", angle = 0),axis.text.x = element_text(angle=90,vjust=0.5,hjust=0.5, size=8))+ ylab("Soil nitrogen measured as nitrate, lb/ac and rains, 100ths of inch")+ scale_x_date(date_breaks = "1 month",date_labels = "%m-%d-%y")+ ggtitle("MR Ranch")+ ylim(0,350) + scale_fill_manual(labels=c("Nitrogen","Rains"), breaks=c("Nitrogen","Rains"), values=c("black","blue"), name="")
所以看起来你想要一个直方图而不是一个条形图。所以我改变了 geom_bar 至 geom_histogram 。此外,我相信你正在获得这个传奇,因为你正在使用 color 争论而不是 fill ,这会给你一个大纲。试试这段代码:
geom_bar
geom_histogram
color
fill
ggplot(data = rr, aes(x = date)) + geom_point(aes(y = value, colour = "Nitrogen")) + geom_line(aes(y = value, colour = "Nitrogen")) + facet_grid(depth ~ dose) + geom_histogram(data= rr, stat = "identity", position = position_dodge(width = 0.9), aes(x = date, y = rain100 ,fill= "Rains")) + theme(strip.text.x = element_text(size = 7, colour = "black", angle = 0), axis.text.x = element_text(angle = 90,vjust = 0.5, hjust = 0.5, size = 8)) + ylab("Soil nitrogen measured as nitrate, lb/ac and rains, 100ths of inch") + scale_x_date(date_breaks = "1 month", date_labels = "%m-%d-%y") + ggtitle("MR Ranch")+ ylim(0,350)+ scale_colour_manual("", breaks=c("Nitrogen","Rains"), values=c("black","blue"))