Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot几何图形线和几何图形条颜色_R_Ggplot2_Geom Bar - Fatal编程技术网

R ggplot几何图形线和几何图形条颜色

R ggplot几何图形线和几何图形条颜色,r,ggplot2,geom-bar,R,Ggplot2,Geom Bar,我有一个数据框,我试图在同一个图中绘制一条线和一条线。我还需要一个传奇。我得到了显示所需颜色的条形图,但它更像是一个添加的轮廓,而不是一种颜色,它显示了一条y=0的水平线,我想删除它 感谢您的帮助 这是我的密码: windows() ggplot(data = rr, aes(x = date))+ geom_point(aes(y=value, colour="Nitrogen"))+ geom_line(aes(y=value, colour="Nitrogen"))+ face

我有一个数据框,我试图在同一个图中绘制一条线和一条线。我还需要一个传奇。我得到了显示所需颜色的条形图,但它更像是一个添加的轮廓,而不是一种颜色,它显示了一条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")

所以看起来您想要的是直方图而不是条形图。因此,我将
geom\u条
更改为
geom\u直方图
。另外,我相信您得到了这个图例,因为您使用的是
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"))

看起来你想要的是直方图而不是条形图。因此,我将
geom\u条
更改为
geom\u直方图
。另外,我相信您得到了这个图例,因为您使用的是
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"))

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

图书馆(GG2)


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

图书馆(GG2)


哇,这正是我想要的!谢谢你,戴夫!哇,正是我想要的!谢谢你,戴夫!嗨,J.文,谢谢你的意见。我想我需要一个条形图,我用百分之一百英寸绘制了降雨,这样我就可以把它们绘制在与土壤中的氮磅相同的轴上。你使用填充而不是颜色的输入正是我想要的。再次感谢!GerryHi J.Moon,谢谢你的意见。我想我需要一个条形图,我用百分之一百英寸绘制了降雨,这样我就可以把它们绘制在与土壤中的氮磅相同的轴上。你使用填充而不是颜色的输入正是我想要的。再次感谢!格里