简体   繁体   English

如何将以下功能添加到现有的 ggplot2 图中?

[英]How can I add the following feature to my existing ggplot2 graph?

I have the following R codes running in RStudio.我在 RStudio 中运行了以下 R 代码。

library(ggplot2)
library(tidyverse)

DF <- structure(list(Type = 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("Type", "variable", "value"), class = "data.frame")


diff_df = DF %>%
  group_by(variable) %>%
  spread(Type, value) %>%
  mutate(diff = Current - SPLY,
         max_y = max(Current, SPLY),
         sim_higher = Current > SPLY)

ggplot(DF, aes(variable, value)) +
  geom_bar(aes(y = max_y), data = diff_df, stat = "identity", fill = "grey80", width = 0.4) +
  geom_bar(aes(fill = Type), position = "dodge", stat="identity", width=.5) +
  geom_text(aes(label=value, group=Type), position=position_dodge(width=0.5), vjust=3.0) +
  geom_text(aes(label = diff, y = max_y), vjust=-0.5, data = diff_df %>% filter(sim_higher), 
            hjust = 0.0, colour = scales::muted("red")) +
  geom_text(aes(label = diff, y = max_y), vjust=-0.5, data = diff_df %>% filter(!sim_higher), 
            hjust = 1.0, colour = scales::muted("red")) +
  theme_bw(base_size = 18) +
  ylab('Room Nights') + xlab('Week')

The above codes produces the following graph:上面的代码生成以下图表:

图 1

I would like to add the % change next to the bars in the chart.我想在图表中的条形旁边添加百分比变化。

Expected output:预期 output:

图 2

How can I achieve this?我怎样才能做到这一点?

The easiest way to do this is to create a separate little data frame for the circles.最简单的方法是为圆圈创建一个单独的小数据框。 You can plot these as large green points, then plot white text labels over them:您可以将 plot 这些作为大绿点,然后将 plot 白色文本标签覆盖在它们上面:

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

ggplot(DF, aes(variable, value)) +
  geom_col(aes(y = max_y), data = diff_df, fill = "grey80", width =0.4) +
  geom_col(aes(fill = Type), position = "dodge", width = 0.5) +
  geom_text(aes(label=value, group=Type), position = position_dodge(width = 0.5),
            vjust=3.0) +
  geom_text(aes(label = diff, y = max_y), vjust=-0.5, 
            data = diff_df %>% filter(sim_higher), 
            hjust = 0.0, colour = scales::muted("red")) +
  geom_text(aes(label = 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, size = 20, colour = "forestgreen") +
  geom_text(data = circle_df, aes(label = labels), colour = "white") +
  theme_bw(base_size = 18) +
  ylab('Room Nights') + xlab('Week') 

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM