首页 > 解决方案 > 如何在我的 ggplot2 柱形图上添加自定义图例?

问题描述

我在 RStudio 中运行了以下 R 代码:

library(ggplot2)
library(tidyverse)
library(ggthemes)
library(scales)

DF <- structure(list(Period = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 
                                          2L), .Label = c("Current", "SPLY"), class = "factor"), 
                     variable = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L), .Label = c("Wk 06 Jan 2020-12 Jan 2020", "Wk 13 Jan 2020-19 Jan 2020", "Wk 20 Jan 2020-26 Jan 2020", "Wk 27 Jan 2020-02 Feb 2020"), class = "factor"), value = c(6212, 12195,5508,      10574,15060,    9763,5341,      9478)), row.names = c(NA, -8L), .Names = c("Period", "variable", "value"), class = "data.frame")


circle_df <- data.frame(variable = 1:4 + 0.4, value = rep( 1000, 4),
                        labels = scales::percent(1- DF$value[DF$Period == "SPLY"]/
                                                   DF$value[DF$Period == "Current"]))


p<- ggplot(DF, aes(variable, value)) +
  geom_col(aes(y = max_y), data = diff_df, fill = "grey80", width =0.4) +
  geom_col(aes(fill = Period), position = "dodge", width = 0.5) +
  geom_text(aes(label=scales::comma(value), group=Period), position = position_dodge(width = 0.5), fontface="bold",
            vjust=3.0) +
  geom_text(aes(label = scales::comma(diff), y = max_y), vjust=-0.5, 
            data = diff_df %>% filter(sim_higher), 
            hjust = 0.0, colour = scales::muted("red")) +
  geom_text(aes(label = scales::comma(diff), y = max_y), vjust=-0.5, 
            data = diff_df %>% filter(!sim_higher), 
            hjust = 1.0, colour = scales::muted("red")) +
  geom_point(data = circle_df, fill="greenyellow", shape = 21, size= 20, colour = "white") +
  geom_text(data = circle_df, aes(label = labels), colour = "black", family="Trebuchet MS",fontface="bold") +
    theme_minimal()+
  theme(axis.title.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank(), axis.title.x=element_blank(), axis.text.x = element_text(color = "grey20", size = 11, hjust = .5, vjust = .5, face = "bold")) +
  ylab('Room Nights') + xlab('Week') +
  
  ggtitle("ALL HOTELS: Pick Up (Room Nights) in January 2020")

p

上述代码的输出如下所示。

电流输出

我的预期输出如下所示:

(1) 去掉图例中的“句号”

(2) 增加“Variance”和“%”Change的图标

预期产出

如何修改现有代码以实现此目的?

标签: rggplot2legend

解决方案


老实说,这需要大量不同规模的黑客才能实现:

mutate(DF, Period = factor(Period, levels = c("Current", "SPLY", "Variance")) %>%
  ggplot(aes(variable, value)) +
    geom_col(aes(y = max_y), data = diff_df, fill = "grey80", width = 0.4) +
    geom_col(aes(fill = Period), position = "dodge", width = 0.5) +
    geom_text(aes(label=scales::comma(value), group = Period), 
              position = position_dodge(width = 0.5), fontface = "bold", vjust = 3.0) +
    geom_text(aes(label = scales::comma(diff), y = max_y), vjust = -0.5, 
              data = diff_df %>% filter(sim_higher), 
              hjust = 0.0, colour = scales::muted("red")) +
    geom_text(aes(label = scales::comma(diff), y = max_y), vjust = -0.5, 
              data = diff_df %>% filter(!sim_higher), 
              hjust = 1.0, colour = scales::muted("red")) +
    geom_point(data = circle_df, aes(colour = "", shape = "", size = ""))+
    geom_point(data = circle_df, fill = "yellowgreen", shape = 21, 
               color = "white", size = 20) +
    scale_fill_manual(values = c("#F8766D", "#00BFC4", "gray80"), 
                      drop = FALSE, name = " ") +
    scale_size_manual(guide = "legend", breaks = "", values = 10,
                      name = "% change") +
    scale_color_manual(guide = "legend",  breaks = "", 
                       values = "yellowgreen", name = "% change") +
    scale_shape_manual(guide = "legend",  breaks = "", 
                       values = 16, name = "% change") +
    geom_text(data = circle_df, aes(label = labels), colour = "black", 
              family="Trebuchet MS",fontface="bold") +
    theme_minimal() +
    theme(axis.title.y=element_blank(), 
          axis.text.y=element_blank(), 
          axis.ticks.y=element_blank(), 
          axis.title.x=element_blank(), 
          axis.text.x = element_text(color = "grey20", size = 11, 
                                     hjust = .5, vjust = .5, face = "bold")) +
    labs(y = "Room Nights", x = "Week", fill = "") +
    ggtitle("ALL HOTELS: Pick Up (Room Nights) in January 2020")

在此处输入图像描述


推荐阅读