r - 用ggplot中的一个x轴绘制两个图(3个变量) [英] r - Ploting two plots (3 variables) with one x-axes in ggplot
问题描述
- 为什么观察到的流程总是黑色的,即使我已经设置它变成蓝色?我不小心使用了其他一些参数来覆盖它吗?
-
最重要的是,我如何能够为底部图表添加图例?我尝试了许多不同的代码,但他们似乎并不适合我。
x = data.frame(date = Date,雨= Obs_rain,obsflow = Obs_flow,simflow = Sim_flow)
g.top < - ggplot(x,aes(x = date,y = rain,ymin = 0,ymax = rain))+
geom_linerange()+
scale_y_continuous(trans =reverse)+
theme_bw()+
theme(plot.margin = unit(c(1,5,-30, 6),units =points),
axis.title.y = element_text(vjust = 0.3))+
labs(x =Date,y =Rain(mm))
g.bottom < - ggplot(x,aes(x = date,y = obsflow,ymin = 0,ymax = obsflow),color =blue,size = 0.5)+
geom_linerange()+ #plot flow
geom_linerange(aes(y = simflow,ymin = 0,ymax = simflow),color =red,size = 0.5)+
labs(x =Date ,y =River flow(ML / day))+
theme_classic()+
theme(plot.background = element_rect(fill =transparent),
plot.margin =单元(c(2 ,0,1,1),units =lines))
grid.arrange(g.top,g.bottom,heights = c(1/5,4/5))
更新:
我用蓝线的颜色解决了这个问题。我无意中将论点置于错误的地方。但我仍然在为这个传奇而努力。
g.bottom <-ggplot(x,aes(x = date,y = obsflow,ymin = 0,ymax = obsflow))+
geom_linerange(color =blue,size = 0.5)+ #plot flow
解释@pierre意味着什么......使用 reshape2 :: melt $>将数据从宽格式转换为长格式 c $ c>,因此每个日期的流类型都在一列
flow_type
中,并且该值是另一个( flow_val
)。然后指定 flow_type
作为分配变量的颜色:
require(reshape2)
x.melted< - melt(x,id.vars = c(date,rain),variable.name =flow_type,
(x =熔点,aes(x =日期),大小= 0.5)+
geom_linerange(aes(ymin = value.name =flow_val)
g.bottom< - ggplot 0,ymax = flow_val,color = flow_type))+ #plot flow
labs(x =Date,y =River flow(ML / day))+
theme_classic()+
主题(plot.background = element_rect(fill =transparent),
plot.margin =单位(c(2,0,1,1),units =lines),
legend .position =bottom)+
scale_colour_manual(guide = guide_legend(title =Flow Type),
values = c(obsflow=blue,simflow=red) )
I am trying to plot two flows and one rainfall data in one graph. I have broke it up into top and bottom parts as shown in the following pic. Here I have two issues with this plots and spent ages but cannot solve it.
- Why the observed flow always in black, even I have set it up as blue? Did I accidentally used some other arguments to overwrite it?
The most importantly is, how do I able to add a legend for the bottom plot? I tried many different codes but they don't seem to work for me.
x = data.frame(date = Date, rain = Obs_rain, obsflow = Obs_flow,simflow=Sim_flow) g.top <- ggplot(x, aes(x = date, y = rain, ymin=0, ymax=rain)) + geom_linerange() + scale_y_continuous(trans = "reverse") + theme_bw() + theme(plot.margin = unit(c(1,5,-30,6),units="points"), axis.title.y = element_text(vjust =0.3)) + labs(x = "Date",y = "Rain(mm)") g.bottom <- ggplot(x, aes(x = date, y = obsflow, ymin=0, ymax=obsflow), colour = "blue",size=0.5) + geom_linerange() + #plot flow geom_linerange(aes(y = simflow, ymin=0, ymax=simflow), colour = "red", size =0.5)+ labs(x = "Date", y = "River flow (ML/day)") + theme_classic() + theme(plot.background = element_rect(fill = "transparent"), plot.margin = unit(c(2,0,1,1),units="lines")) grid.arrange(g.top,g.bottom, heights = c(1/5, 4/5))
Update:
I have resolved the issue with blue line colour. I accidently put arguments in the wrong place. But I'm still struggling with the legend.
g.bottom <- ggplot(x, aes(x = date, y = obsflow, ymin=0, ymax=obsflow)) +
geom_linerange(colour = "blue",size=0.5) + #plot flow
As an explanation of what @pierre means... turn your data from "wide" to "long" format using reshape2::melt
, so that the flow type for each date is in one column flow_type
, and the value is another (flow_val
). Then you specify flow_type
as the grouping variable with which to assign colour:
require(reshape2)
x.melted <- melt(x, id.vars = c("date", "rain"), variable.name="flow_type",
value.name="flow_val")
g.bottom <- ggplot(x.melted, aes(x = date),size=0.5) +
geom_linerange(aes(ymin=0, ymax=flow_val, colour=flow_type)) + #plot flow
labs(x = "Date", y = "River flow (ML/day)") +
theme_classic() +
theme(plot.background = element_rect(fill = "transparent"),
plot.margin = unit(c(2,0,1,1),units="lines"),
legend.position="bottom") +
scale_colour_manual(guide = guide_legend(title = "Flow Type"),
values = c("obsflow"="blue", "simflow"="red"))
这篇关于r - 用ggplot中的一个x轴绘制两个图(3个变量)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!