向2 y轴的图添加图例

时间:2018-09-05 08:54:02

标签: r ggplot2 legend

我有一个数据:

ex <- structure(list(subgroups = c("Monday", "Tuesday", "Wednesday", 
"Thursday", "Friday", "Saturday", "Sunday"), y_right = c(113.42, 
95.3, 94.81, 145.21, 94.71, 161.08, 123.1), y_left = c(634, 453, 
479, 633, 365, 616, 472)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -7L))

和由代码生成的图:

ggplot(ex) + 
  geom_bar(aes(factor(subgroups, levels = c('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 
                                        'Saturday', 'Sunday')),
           y_left), fill = rgb(16/255, 72/255, 128/255), stat = 'identity') +
  geom_line(aes(factor(subgroups, levels = c('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 
                                         'Saturday', 'Sunday')),
            y_right * 4), colour = rgb(237/255, 165/255, 6/255), group = 1) + 
  geom_point(aes(factor(subgroups, levels = c('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 
                                          'Saturday', 'Sunday')),
             y_right * 4), color = rgb(237/255, 165/255, 6/255), size = 3) +
  theme_bw() +
  labs(x = 'weekday') +
  scale_y_continuous(sec.axis = sec_axis(~. / 4, name = 'y_right'))

如下所示: enter image description here

我需要在图上方添加一个图例,以反映哪个轴指向哪个几何体,即: enter image description here

如何做到?

2 个答案:

答案 0 :(得分:3)

您可以尝试tidyverse

library(tidyverse)
ex %>% 
  mutate(y_right = y_right *4) %>% 
  mutate(subgroups = factor(subgroups,
                      levels = c('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 
                                 'Saturday', 'Sunday'))) %>% 
  gather(k, v, - subgroups) %>% 
  ggplot(aes(subgroups, v)) + 
    geom_col(data = . %>% filter(k == "y_left"), aes(fill = k)) + 
    geom_point(data= . %>% filter(k == "y_right"), aes(color= k)) + 
    geom_line(data= . %>% filter(k == "y_right"), aes(color= k, group = k))  +
    scale_color_manual("", values =rgb(237/255, 165/255, 6/255)) + 
    scale_fill_manual("", values=rgb(16/255, 72/255, 128/255))+
    scale_y_continuous("y_left", sec.axis = sec_axis(~. / 4, name = 'y_right')) +
    theme(legend.spacing.y = unit(0,"pt"),
          legend.margin =  margin(t = 0, r = 0, b = 0, l = 0, unit = "pt"))

enter image description here

这个想法是给ggplot所有从宽到长的数据。然后分别分别为条形图和点/线子集data.frame。最后,更改颜色和填充参数以及图例位置。

答案 1 :(得分:1)

在不重塑数据的情况下,您可以执行以下操作:

ex$subgroups <- factor(ex$subgroups, levels = c('Monday', 'Tuesday', 'Wednesday', 'Thursday', 
                                                'Friday', 'Saturday', 'Sunday'))

ggplot(ex, aes(x = subgroups)) + 
  geom_bar(aes(y = y_left, fill = "y_left"), stat = 'identity') +
  geom_line(aes(y = y_right * 4, colour = "y_right"), group = 1) + 
  geom_point(aes(y = y_right * 4, colour = "y_right"), size = 3) +
  scale_fill_manual("", values = rgb(16/255, 72/255, 128/255)) +
  scale_color_manual("", values = rgb(237/255, 165/255, 6/255)) +
  theme_bw() +
  labs(x = 'weekday') +
  scale_y_continuous(sec.axis = sec_axis(~. / 4, name = 'y_right'))

enter image description here