ggplot geom-line和geom_bar颜色

问题描述 投票:0回答:2

我有一个数据框,我试图在同一个图中绘制一条线和一条条。我还需要一个传奇。我得到了条形图以显示所需的颜色,但更多的是添加轮廓而不是颜色,它显示了我想要移除的y = 0的水平线。

任何帮助表示赞赏!

这是我的代码:

windows()
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_bar(data=rr,stat = "identity", position = position_dodge(width = 0.9),aes(x=date, y=rain100 ,colour="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"))

这是我的数据集(缩短版)

    structure(list(date = structure(c(17444, 17444, 17444, 17444, 
17456, 17456, 17456, 17456, 17457, 17457, 17457, 17457, 17473, 
17473, 17473, 17473, 17485, 17485, 17485, 17485, 17508, 17508, 
17508, 17508, 17550, 17550, 17550, 17550), class = "Date"), depth = c("12-24 in", 
"12-24 in", "0-12 in", "0-12 in", "12-24 in", "0-12 in", "0-12 in", 
"12-24 in", "12-24 in", "12-24 in", "0-12 in", "0-12 in", "0-12 in", 
"12-24 in", "0-12 in", "12-24 in", "0-12 in", "12-24 in", "0-12 in", 
"12-24 in", "12-24 in", "0-12 in", "0-12 in", "12-24 in", "12-24 in", 
"0-12 in", "0-12 in", "12-24 in"), value = c(60, 60, 60, 60, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 50, 20, 12, 69.5, 
87, 30, 347, 12, 35, 17.4, 35, 35), trt = c("Experiment", "Control", 
"Control", "Experiment", "Control", "Control", "Experiment", 
"Experiment", "Experiment", "Control", "Control", "Experiment", 
"Experiment", "Experiment", "Control", "Control", "Experiment", 
"Control", "Control", "Experiment", "Experiment", "Control", 
"Experiment", "Control", "Experiment", "Control", "Experiment", 
"Control"), dose = c("High Preplant", "Low/No Preplant", "Low/No Preplant", 
"High Preplant", "Low/No Preplant", "Low/No Preplant", "High Preplant", 
"High Preplant", "High Preplant", "Low/No Preplant", "Low/No Preplant", 
"High Preplant", "High Preplant", "High Preplant", "Low/No Preplant", 
"Low/No Preplant", "High Preplant", "Low/No Preplant", "Low/No Preplant", 
"High Preplant", "High Preplant", "Low/No Preplant", "High Preplant", 
"Low/No Preplant", "High Preplant", "Low/No Preplant", "High Preplant", 
"Low/No Preplant"), rain = c(0, 0, 0, 0, 0, 0, 0, 0, 0.01, 0.01, 
0.01, 0.01, 0.09, 0.09, 0.09, 0.09, 0, 0, 0, 0, 0, 0, 0, 0, 0.03, 
0.03, 0.03, 0.03), rain100 = c(0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 
1, 1, 9, 9, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3)), row.names = c(NA, 
-28L), class = "data.frame")

Here is what I get

r ggplot2 geom-bar
2个回答
1
投票

关键是使用填充而不是颜色。代码将生成警告但将正确显示。验证图例是否为预期结果。

库(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="") 

enter image description here


1
投票

所以看起来你想要一个直方图而不是一个条形图。所以我把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"))
© www.soinside.com 2019 - 2024. All rights reserved.