如何为辅助y轴添加图例?

时间:2020-02-18 11:02:25

标签: r ggplot2

我正在使用ggplot2wesanderson软件包来绘制包含线和条的图形。我的数据示例如下:

        Year  PIB     Variation
        1971  13032.36  0.0629264087            
        1972  13686.28  0.0501766372
        1973  15595.61  0.1395068638
        1974  17343.64  0.1120847469
        1975  19246.61  0.1097214887
        1976  20670.32  0.0739719878
        1977  21002.05  0.0160486146
        1978  22200.60  0.0570682386
        1979  23029.58  0.0373404322
        1980  23883.67  0.0370866512
        1981  25224.23  0.0561287273
        1982  25379.32  0.0061484533
        1983  25293.82 -0.0033688846
        1984  25957.86  0.0262530531
        1985  26979.30  0.0393499310
        1986  27914.07  0.0346476743
        1987  27841.75 -0.0025908081
        1988  29481.76  0.0589047025
        1989  29778.28  0.0100577442
        1990  30874.09  0.0367989689
        1991  32199.01  0.0429136535
        

我想用辅助轴来描述演变和GDP增长率。因此,我的代码如下:

#For constructing the secondary axis
gdp_off_pib <- as.numeric(solve(
a=matrix(c(1,max(annual$Variation),1,min(annual$Variation)),
           nr=2,nc=2,byrow=TRUE),
b=matrix(c(max(annual$PIB),min(annual$PIB)),nc=1)))

graph_pib <- ggplot(annual, aes(x = Year)) + 
geom_col(aes(y = PIB, fill = "PIB")) +
scale_fill_manual(values = wes_palette("Zissou1", 3, type = "discrete"))+
geom_line(aes(y=(Variation*gdp_off_pib[2])+gdp_off_pib[1]), color="#F21A00"
           , size =0.8) +
scale_color_manual(values = wes_palette("Zissou1", 2, type = "continuous") )+
xlab("Year") +
ylab("Real GDP") + labs(color="Variables") +scale_y_continuous(    
   sec.axis = sec_axis(~(.- gdp_off_pib[1])/gdp_off_pib[2],
                      name = "GDP growth rate"))

graph_pib

上面的代码绘制了以下图形:

enter image description here

我的问题在右边,因为您可以看到该图并不能解释红线是增长率,并且还会出现单词fill。

感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

  • 要将线条添加到图例中,您需要通过美学为其赋予颜色,而不是对其进行硬编码:

    geom_line(aes(y = (Variation * gdp_off_pib[2L]) + gdp_off_pib[1L], color = 'GDP'))
    

    ...,并可能应用合适的调色板,您的代码会尝试使用它-问题是您明确需要在其中选择 second 颜色,否则您的行与列的颜色相同:

    wes_palette("Zissou1", 2L, type = "continuous")[2L]
    
  • 要删除图例标题,请将其设置为NULL""

    labs(color = NULL, fill = NULL)
    

最终代码是:

palette = wes_palette("Zissou1", 2L, type = "continuous")

ggplot(annual, aes(x = Year)) +
    geom_col(aes(y = PIB, fill = "PIB")) +
    scale_fill_manual(values = palette[1L]) +
    geom_line(
        aes(y = (Variation * gdp_off_pib[2L]) + gdp_off_pib[1L], color = 'Growth'),
        size = 0.8
    ) +
    scale_color_manual(values = palette[2L]) +
    scale_y_continuous(
        sec.axis = sec_axis(
            ~ (. - gdp_off_pib[1L]) / gdp_off_pib[2L],
            name = "GDP growth rate"
        )
    ) +
    labs(y = 'Real GDP', color = NULL, fill = NULL)

plot with legend

但是对于出版物,我将完全删除图例,而是将有关轴的信息添加到轴标题中—通过给它们指定适当的颜色,或者简单地在“轴”中添加单词“ bars”和“ line”轴标题。例如,过于使用辅助轴的《经济学人》以相应绘图元素的颜色为轴标题和轴标签上色:

plot without legend

代码:

ggplot(annual, aes(x = Year)) +
    geom_col(aes(y = PIB, fill = "PIB")) +
    scale_fill_manual(values = palette[1L], guide = FALSE) +
    geom_line(
        aes(y = (Variation * gdp_off_pib[2L]) + gdp_off_pib[1L], color = 'Growth'),
        size = 0.8
    ) +
    scale_color_manual(values = palette[2L], guide = FALSE) +
    scale_y_continuous(
        sec.axis = sec_axis(
            ~ (. - gdp_off_pib[1L]) / gdp_off_pib[2L],
            name = "GDP growth rate"
        )
    ) +
    ylab('Real GDP') +
    theme(
        axis.title.y = element_text(color = palette[1L]),
        axis.text.y = element_text(color = palette[1L]),
        axis.title.y.right = element_text(color = palette[2L]),
        axis.text.y.right = element_text(color = palette[2L])
    )