r - 如何在我的 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的图标
如何修改现有代码以实现此目的?
解决方案
老实说,这需要大量不同规模的黑客才能实现:
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")
推荐阅读
- javascript - 如何检查我的 for 循环中的所有列
- stringbuilder - Apache Common 的 StrBuilder 和 Java 的 StringBuilder 有什么区别?
- model-view-controller - 如何在 KendoUI 调度程序上制作年度视图?
- angular - 如何使用primeng表格单元格添加带有无变形文本的图标结尾
- android - 从谷歌商店获取应用版本和新功能
- c# - DataTable 到 GridView 没有显示结果表
- numpy - Optimal way to find index of first occurrence of subarray in each frame of batch data without for loop
- vhdl - 将输出连接到 DAC - VHDL
- c# - 如何在 c# 中获取@rErr / 输出参数值?
- mysql - 如何在 SSMS 中加入名为“身份”的列?