Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
711 views
in Technique[技术] by (71.8m points)

r - How to add a legend for the secondary axis ggplot

I am trying to add a legend for the secondary axis in the following code

library(ggplot2)
library(dplyr)
library(reshape2)

df = data.frame(period = seq(as.POSIXct("2017-01-01"),as.POSIXct("2017-12-01"), by = "month"), 
                b = c(100, 110, 105, 200, 210, 190, 180, 170, 165, 175, 140, 145),
                c = c(120, 130, 150, 170, 250, 160, 130, 120, 110, 130, 120, 170),
                d = c(1060, 1180, 1050, 2070, 2150, 1900, 1850, 1070, 1605, 1750, 1460, 1250)) %>% 
             mutate(period = factor(period))

df_bar = melt(df, id.vars = "period", measure.vars = c("b", "c", "d")) %>% filter(variable != "d")

df_line = df %>% select(period, d)

ggplot(data = df_bar, aes(x = period, y = value, fill = variable)) + 
 geom_bar(stat = "identity", position = "dodge") +
 theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
 theme(axis.text=element_text(size=9), 
 axis.title=element_text(size=14,face="bold")) +
 ylab("primary axis") +
 geom_line(data = df_line, aes(x = period, y = (d)/10, group = 1), inherit.aes = FALSE) + 
 geom_point(data = df_line, aes(x = period, y = (d)/10, group = 1), inherit.aes = FALSE) +
 scale_y_continuous(sec.axis = sec_axis(~.*10, name = "secondary axis"))

plot

I would like to have a legend for the line graph as well.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can add linetype inside aes in your geom_line call to create a separate legend for the line then move its legend closer to fill legend

See also this answer

library(reshape2)
library(tidyverse)

ggplot(data = df_bar, aes(x = period, y = value, fill = variable)) +
  geom_bar(stat = "identity", position = "dodge") +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  theme(
    axis.text = element_text(size = 9),
    axis.title = element_text(size = 14, face = "bold")
  ) +
  ylab("primary axis") +
  geom_line(data = df_line, aes(x = period, y = (d) / 10, group = 1, linetype = "My line"), inherit.aes = FALSE) +
  scale_linetype_manual(NULL, values = 1) +
  geom_point(data = df_line, aes(x = period, y = (d) / 10, group = 1), inherit.aes = FALSE) +
  scale_y_continuous(sec.axis = sec_axis(~. * 10, name = "secondary axis")) +
  theme(legend.background = element_rect(fill = "transparent"), 
      legend.box.background = element_rect(fill = "transparent", colour = NA),
      legend.key = element_rect(fill = "transparent"), 
      legend.spacing = unit(-1, "lines"))

To get both the point and line in the same legend, we can map color to aes & use scale_color_manual

ggplot(data = df_bar, aes(x = period, y = value, fill = variable)) +
  geom_bar(stat = "identity", position = "dodge") +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  theme(
    axis.text = element_text(size = 9),
    axis.title = element_text(size = 14, face = "bold")
  ) +
  ylab("primary axis") +
  geom_line(data = df_line, aes(x = period, y = (d) / 10, group = 1, color = "My line"), inherit.aes = FALSE) +
  scale_color_manual(NULL, values = "black") +
  geom_point(data = df_line, aes(x = period, y = (d) / 10, group = 1, color = "My line"), inherit.aes = FALSE) +
  scale_y_continuous(sec.axis = sec_axis(~. * 10, name = "secondary axis")) +
  theme(legend.background = element_rect(fill = "transparent"), 
        legend.box.background = element_rect(fill = "transparent", colour = NA),
        legend.key = element_rect(fill = "transparent"), 
        legend.spacing = unit(-1, "lines"))

Created on 2018-07-21 by the reprex package (v0.2.0.9000).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...