我正在尝试在一张图中绘制两个流量和一个降雨数据。我将其分解为顶部和底部,如下图所示。在这里,我有两个关于该图和使用年龄的问题,但无法解决。
最重要的是,如何为底部绘图添加图例?我尝试了许多不同的代码,但是它们似乎对我不起作用。
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))
更新:
我已经用蓝线颜色解决了这个问题。我不小心把论点放在错误的地方。但是我仍然在为传奇而挣扎。
g.bottom <- ggplot(x, aes(x = date, y = obsflow, ymin=0, ymax=obsflow)) +
geom_linerange(colour = "blue",size=0.5) + #plot flow
为了解释@pierre的含义,请使用reshape2::melt
,将数据从“宽”格式转换为“长”格式,以便每个日期的流类型在一列中flow_type
,值在另一列中(flow_val
)。然后,将其指定flow_type
为用于分配颜色的分组变量:
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"))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句