ggplot多重线/点/条形图的传说

时间:2018-03-14 00:52:08

标签: r ggplot2

所以,我遇到了多行/点/条图的传说。只是出现了行,我无法更改其名称,也没有添加bar元素,这是我正在运行的代码:

df.rain %>% 
  ggplot(aes(x = date))+
  labs(y = "Rain (mm); Temperature (°C)")+
  geom_bar(aes(y = P_mm), 
           fill = "honeydew4",
           stat = "identity",
           width = 5)+ 

  geom_point(aes(y = T_max), shape = 2)+
  geom_line(aes(y = T_max, linetype = "dashed"))+
  geom_point(aes(y = T_min), shape = 3)+
  geom_line(aes(y = T_min, linetype = "solid"))+
  geom_point(aes(y = DPV_d*15), shape = 1)+
  geom_line(aes(y = DPV_d*15, linetype = "dotted"))+
  scale_y_continuous(sec.axis = sec_axis(trans = ~ . /15 , 
                                         name = "DPV (kPa)"))+ 
  scale_x_date(date_breaks = "2 month",
               date_minor_breaks = "1 month",
               date_labels = "%b %Y") +
  theme_bw() +
  theme(axis.title.x = element_blank()) 

结果:

enter image description here

2 个答案:

答案 0 :(得分:1)

这是一个可能的解决方案,包含模拟数据:

编辑::我添加了两行来控制图例标签:

  1. mutate(variable = factor(fct_relevel(variable, "T_min", "T_max", "DPV_d", "P_mm"))):您在哪里定义订单
  2. geom_blank():首先使用空白geom进行正确排序。
  3. <强> PLOT:

    plot v2

    <强>代码:

    library(tidyverse)
    
    w_dat <- tibble(date = seq(as.Date("2017-01-01"), 
                           as.Date("2017-12-01"), 
                           by = "month"),
                    P_mm = sample(20:150, 12, replace = TRUE),
                    T_max = sample(15:30, 12, replace = TRUE),
                    T_min = T_max * sample(seq(0.5, 0.9, 0.05), 12, replace = TRUE),
                    DPV_d = sample(seq(0.5, 2, 0.1), 12, replace = TRUE))
    
    w_dat %>% 
      mutate(DPV_d = DPV_d * 15) %>%
      gather(variable, value, -date) %>%
      mutate(variable = factor(fct_relevel(variable, "T_min", "T_max", "DPV_d", "P_mm"))) %>% 
      ggplot(data = ., aes(x = date, y = value, shape = variable, linetype = variable, fill = variable)) +
      geom_blank() +
      geom_bar(data = . %>% filter(variable == "P_mm"),
           stat = "identity",
           width = 5) + 
      geom_point(data = . %>% filter(variable != "P_mm")) +
      geom_line(data = . %>% filter(variable != "P_mm")) +
      labs(y = "Rain (mm); Temperature (°C)") +  
      scale_y_continuous(sec.axis = sec_axis(trans = ~ . /15 , 
                                         name = "DPV (kPa)")) + 
      scale_x_date(date_breaks = "2 month",
               date_minor_breaks = "1 month",
               date_labels = "%b %Y") +
      theme_bw() +
      theme(axis.title.x = element_blank()) +
      scale_shape_manual("The legend you want", 
                     values = c("T_min" = 3,
                                "T_max" = 2,
                                "DPV_d" = 1,
                                "P_mm" = NA)) +
      scale_linetype_manual("The legend you want",
                        values = c("T_min" = "solid",
                                   "T_max" = "dashed",
                                   "DPV_d" = "dotted",
                                   "P_mm" = "blank")) +
      scale_fill_manual("The legend you want",
                    values = c("T_min" = "white",
                               "T_max" = "white",
                               "DPV_d" = "white",
                               "P_mm" = "honeydew4"))
    

答案 1 :(得分:0)

不完全是我想要的,无法合并点和线条,但设法产生有意义的传奇

df.rain %>% 
  ggplot(mapping = aes(x = date))+
  labs(y = "Rain (mm); Temperature (°C)")+
  geom_bar(aes(y = P_mm, fill = "Chuva"),
           stat = "identity",
           width = 5)+ 

  geom_point(aes(y = T_max, shape = "tmax"))+
  geom_line(aes(y = T_max), linetype = "dashed")+
  geom_point(aes(y = T_min, shape = "tmin"))+
  geom_line(aes(y = T_min), linetype = "solid")+
  geom_point(aes(y = DPV_d*45, shape = "dpv"))+
  geom_line(aes(y = DPV_d*45), linetype = "dotted")+
  scale_y_continuous(sec.axis = sec_axis(trans = ~ . /45 , 
                                         name = "DPV (kPa)"))+ 
  scale_x_date(date_breaks = "2 month",
               date_minor_breaks = "1 month",
               date_labels = "%b %Y") +
  scale_fill_manual(name = "", values = c("Chuva" = "honeydew4")) +
  scale_shape_manual(name = "", values = c("tmax" = 2, "tmin" = 3, "dpv" = 1), labels = c("T° max", "T° min", "DPV")) +
  theme_bw() +
  theme(axis.title.x = element_blank(),
        axis.text.x = element_text(angle = 45, vjust = 1, 
                                   size = 9, hjust = 1),
        legend.position = "bottom",
        legend.text = element_text(size = 8),
        legend.box.background = element_rect(),
        legend.box.margin = margin(0.6, 0.6, 0.6, 0.6))

Resulting plot