传说没有深究

时间:2018-06-12 12:10:44

标签: r ggplot2 gridextra

我复制了另一个用户的代码,并将其调整为我的数据。 代码:

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

enter image description here

1 个答案:

答案 0 :(得分:2)

试试这个:

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))

enter image description here