Легенда не идет ко дну - PullRequest
       18

Легенда не идет ко дну

0 голосов
/ 12 июня 2018

Я скопировал код другого пользователя и адаптирую его к своим данным.Код:

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

1 Ответ

0 голосов
/ 12 июня 2018

попробуйте это:

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

...