传说不到底

Legend doesn't go to the bottom

我已经复制了另一个用户的代码并正在根据我的数据进行调整。 代码:

library(gridExtra)
library(grid)
library(ggplot2)

    x <- data.frame(
  date = seq(as.Date("2012-01-01"),as.Date("2012-12-31"), by="week"), 
  rain = sample(0:20,53,replace=T),
  flow1 = sample(50:150,53,replace=T),
  flow = sample(50:200,53,replace=T))


g.top <- ggplot(x, aes(x = date, y = rain, ymin=0, ymax=rain)) +
  geom_linerange() +
  scale_y_continuous(limits=c(22,0),expand=c(0,0), trans="reverse")+
  theme_classic() +
  theme(plot.margin = unit(c(5,5,-32,6),units="points"),
        axis.title.y = element_text(vjust = 0.3))+
  labs(y = "Rain (mm)")

g.bottom <- ggplot(x, aes(x = date)) +
  geom_line(aes(y = flow, colour = "flow")) + 
  geom_line(aes(y = flow1, colour = "flow1")) + 
  theme(legend.position="bottom") +
  theme_classic() +
  theme(plot.margin = unit(c(0,5,1,1),units="points")) +
  labs(x = "Date", y = "River flow (m/s)") 


grid.arrange(g.top, g.bottom , heights = c(1/5, 4/5))

本想把图例放到底部,结果没有。

enter image description here

试试这个:

library(gridExtra)
library(grid)
library(ggplot2)

x <- data.frame(
  date = seq(as.Date("2012-01-01"),as.Date("2012-12-31"), by="week"), 
  rain = sample(0:20,53,replace=T),
  flow1 = sample(50:150,53,replace=T),
  flow = sample(50:200,53,replace=T))


g.top <- ggplot(x, aes(x = date, y = rain, ymin=0, ymax=rain)) +
  geom_linerange() +
  scale_y_continuous(limits=c(22,0),expand=c(0,0), trans="reverse")+
  theme_classic() +
  theme(plot.margin = unit(c(5,5,-32,6),units="points"),
        axis.title.y = element_text(vjust = 0.3))+
  labs(y = "Rain (mm)")

g.bottom <- ggplot(x, aes(x = date)) +
  geom_line(aes(y = flow, colour = "flow")) + 
  geom_line(aes(y = flow1, colour = "flow1")) + 
  theme_classic() +
# here the mod
  theme(plot.margin = unit(c(0,5,1,1),units="points"),legend.position="bottom") + 
  labs(x = "Date", y = "River flow (m/s)") 


grid.arrange(g.top, g.bottom , heights = c(1/5, 4/5))