Add legend to ggplot histogram with different types of aesthetics

谁都会走 提交于 2019-12-05 19:34:53

I think @Jimbou's suggestion is preferable, but there is a work-around for creating legends artificially by assigning a character value to the geom_histogram aes fill value and geom_vline aes colour value, then setting colors in scale_fill_manual or scale_colour_manual.

However with this approach aes fill will only take one value (length 1) so you have to subset your df for the blue and turquoise values and plot a histogram for each, with the cutoff determined by your binwidth.

Here is the approach. Note your data needed reformatting.

# reformat data
set.seed(1)
df <- data.frame(runif(1000, 0, 200))
colnames(df) <- "Time_Diff"


bp_overall <- ggplot(data = df) 
bp_overall +
  geom_histogram(data = subset(df, Time_Diff <= 12.5), aes(x = Time_Diff, fill="BAR BLUE"), binwidth = 5) + # subset for blue data, where aes fill is fill group 1 label
  geom_histogram(data = subset(df, Time_Diff > 12.5), aes(x = Time_Diff, fill="BAR TURQUOISE"), binwidth = 5) + # subset for turquoise data, where aes fill is fill group 2 label
  scale_fill_manual("Histogram Legend", values=c("blue", "paleturquoise2")) + # manually assign histogram fill colors
  geom_vline(aes(xintercept = 3, colour="LINE DARK BLUE"), linetype="twodash", size = 1) + # where aes colour is vline label
  scale_colour_manual("Line Legend", values="darkblue") + # manually assign vline colors
  scale_x_continuous(breaks = seq(0, 202, 10)) +
  ggtitle("Time Difference")  +
  xlab("Time in Days") +
  ylab("Amount") +
  theme_light() +
  theme(panel.grid.minor = element_blank(),
    panel.border = element_blank(), 
    axis.line.x = element_line(size = 0.5, linetype = "solid", colour = "black"), 
    axis.line.y = element_line(size = 0.5, linetype = "solid", colour = "black"))

Etc. add your remaining theme.

EDITED: To answer question on how to unify legend and decrease spacing between the two legend types

(1) Remove legend name for vline by setting to "" in scale_fill_manual, change histogram fill legend name to "Legend" in scale_colour_manual.

(2) Specify order in which legends should appear, fill first then colour using guides guide_legend.

(3) Remove the y-spacing between the two legend types by setting legend.spacing.y to 0, and remove margins on top and bottom using legend.margin in theme

bp_overall <- ggplot(data = df) 
bp_overall +
  geom_histogram(data = subset(df, Time_Diff <= 12.5), aes(x = Time_Diff, fill="BAR BLUE"), binwidth = 5) + 
  geom_histogram(data = subset(df, Time_Diff > 12.5), aes(x = Time_Diff, fill="BAR TURQUOISE"), binwidth = 5) + 
  scale_fill_manual(name="Legend", values=c("blue", "paleturquoise2")) +
  geom_vline(aes(xintercept = 3, colour="LINE DARK BLUE"), linetype="twodash", size = 1) + 
  scale_colour_manual(name="", values="darkblue") + 
  scale_x_continuous(breaks = seq(0, 202, 10)) +
  ggtitle("Time Difference")  +
  xlab("Time in Days") +
  ylab("Amount") +
  theme_light() +
  theme(panel.grid.minor = element_blank(),
    panel.border = element_blank(),
    axis.line.x = element_line(size = 0.5, linetype = "solid", colour = "black"),
    axis.line.y = element_line(size = 0.5, linetype = "solid", colour = "black"),
    legend.spacing.y = unit(0, "cm"),
    legend.margin=margin(t=0, r=0.5, b=0, l=0.5, unit="cm")) +
  guides(fill = guide_legend(order = 1), 
     colour = guide_legend(order = 2))

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!