重现具有数据源的情节

标签 r plot ggplot2

考虑到以下数据,我正在尝试编写代码来重现最初使用 Excel 制作的图表。

df = data.frame(year = c(rep(2013, 4), rep(2014, 4), rep(2015, 3)),
                quarter = c(rep(c("Q1", "Q2", "Q3", "Q4"), 2), c("Q1", "Q2", "Q3")),
                Miss = c(5, 3, 7, 4, 6, 5.7, 12, 15, 20, 33, 42),
                Meet = c(100-40-5, 100-37-3, 100-29-7,100-28-4,
                         100-20-6, 100-29-5.7, 100-27-12, 100-25-15,
                         100-13-20, 100-5-33, 100-12-42),
                Exceed = c(40, 37, 29, 28, 20, 29, 27, 25, 13, 5, 12))

footnote = "Data source: XYZ Dashboard; the total number of projects has increased over time from 230 in early 2013 to nearly 270 in Q3 2015."

textbox = "As of Q3 2015, more than 1/3 of projects are missing goals"

title = "Goal attainment over time"

y_label = "% of total projects"

我正在尝试编写一个通用代码以获得尽可能类似于以下的结果: enter image description here

我遇到的主要问题是在图中对齐元素(标题、轴标签、图例、文本框)的“简单”方法。

最佳答案

不确定您想要什么是通用的,什么是灵活的,但这是我的第一次尝试。缺少字幕的自定义颜色和其他一些调整,欢迎改进。

library(tidyverse)
df <-  tibble(
  year = c(rep(2013, 4), rep(2014, 4), rep(2015, 3)),
  quarter = c(rep(c("Q1", "Q2", "Q3", "Q4"), 2), c("Q1", "Q2", "Q3")),
  Miss = c(5, 3, 7, 4, 6, 5.7, 12, 15, 20, 33, 42),
  Meet = c(100-40-5, 100-37-3, 100-29-7,100-28-4,
           100-20-6, 100-29-5.7, 100-27-12, 100-25-15,
           100-13-20, 100-5-33, 100-12-42),
  Exceed = c(40, 37, 29, 28, 20, 29, 27, 25, 13, 5, 12)
)
footnote = "Data source: XYZ Dashboard; the total number of projects has increased over time from 230 in early 2013 to nearly 270 in Q3 2015."
textbox = "As of Q3 2015, more than 1/3\nof projects are missing goals"
title = "Goal attainment over time"
y_label = "% of total projects"

tbl <- df %>%
  gather("category", "value", Miss:Exceed) %>%
  mutate(
    quarter = parse_factor(quarter, levels = c("Q1", "Q2", "Q3", "Q4")),
    category = parse_factor(category, levels = c("Exceed", "Meet", "Miss"))
  )

ggplot(tbl, aes(x = quarter, y = value)) +
  theme_minimal() +
  geom_col(aes(fill = category)) +
  geom_text(
    data = . %>%
      mutate(
        text = str_c(value, "%"),
        text = case_when(
          category != "Miss"                        ~ "",
          year == 2013                              ~ "",
          year == 2014 & quarter %in% c("Q1", "Q2") ~ "",
          TRUE                                      ~ text
        )
      ),
    mapping = aes(label = text, y = value - 4),
    position = "stack",
    col = "white"
    ) +
  facet_wrap(~ year, strip.position = "bottom") +
  scale_fill_manual(values = c("#777777", "#A3A3A1", "#7F141A")) +
  scale_y_continuous(
    breaks = seq(0, 100, 10),
    labels = function(x) str_c(x, "%"),
    expand = c(0, 0)
    ) +
  labs(
    title = title,
    x = "",
    y = y_label,
    # subtitle = textbox,
    caption = footnote,
    fill = NULL
  ) +
  theme(
    text = element_text(colour = "#888888"),
    axis.line = element_line(colour = "#E3E3E3"),
    axis.title.y = element_text(hjust = 1),
    legend.justification = "left",
    legend.position = "top",
    panel.grid = element_blank(),
    plot.title = element_text(hjust = 0),
    plot.subtitle = element_text(hjust = 1),
    plot.caption = element_text(hjust = 0.5, size = 7.5, colour = "#D0D0D0"),
    strip.placement = "outside"
  ) +
  guides(fill = guide_legend(reverse = TRUE, keywidth = 0.6, keyheight = 0.6))

reprex package 创建于 2018-04-21 (v0.2.0).

关于重现具有数据源的情节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49960189/

相关文章:

r - 如何将模型保存为带循环的矢量?

r - 如何使用 ggplot 按降序对水平条形图进行排序?

r - 像 Wolframalpha 网站上那样绘制多项式函数,以便于理解

r - 为什么我不能使用 override.aes 更改图例中的颜色?

r - 具有堆叠和分组选项的条形图,没有面网格

r - ggplot sec_axis 我可以使用向量作为反式公式吗?

r - 美学中的未知问题破坏了 Shiny 应用程序中的 ggplotly 绘图

使用 mtext 删除添加到绘图中的文本

r - 在R中使用geom_rect进行时间序列着色

r - 如何在单个命令中组合两个不同的 dplyr 摘要