于是尝试用ggplot2包“临摹”了一张,下面是代码(画出就成,没有优化过)
- # 环境设置
- library(tidyverse)
- windowsFonts(myCFont = windowsFont("微软雅黑"))
- windowsFonts(myNFont = windowsFont("Miriam"))
- # 数据和参数
- width <- 0.42
- df <- data_frame(source = c("遗弃", "送养", "其他"),
- boy = c(476, 3848, 17585 - 476 - 3848),
- girl = c(1900, 9512, 18265 - 1900 - 9512))
- df <- df %>%
- mutate(boy_cum = cumsum(boy),
- girl_cum = cumsum(girl))
- df_p <- data_frame(x = c(1 + width, rep(2 - width, 2), 1 + width),
- y1 = c(0, 0, 1900, 476),
- y2 = c(476, 1900, 11412, 4324),
- y3 = c(4324, 11412, 18265, 17585))
- df_t <- data_frame(x1 = 1 - width,
- y1 = c(476, 4324),
- l1 = c("476", "3,848"),
- x2 = 2 - width,
- y2 = c(1900, 11412),
- l2 = c("1,900", "9,512"))
- # 画图
- df %>%
- select(source, boy, girl) %>%
- gather(type, value, -1) %>%
- mutate(type = as.integer(factor(type))) %>%
- ggplot(aes(x = type, y = value, fill = source)) +
- geom_bar(position = "stack", stat = "identity",
- show.legend = FALSE,
- width = 2 * width) +
- theme_void() +
- ylim(0, 20200) +
- xlim(-0.1, 2.5) +
- scale_fill_manual(values = c("#adc1da", "#6d8cbb", "#486390")) +
- geom_polygon(data = df_p, aes(x = x, y = y1), fill = "#bec9db") +
- geom_polygon(data = df_p, aes(x = x, y = y2), fill = "#cdd9e9") +
- geom_polygon(data = df_p, aes(x = x, y = y3), fill = "#e6edf5") +
- annotate(geom = "text", x = df_t$x1, y = df_t$y1, label = df_t$l1,
- color = "white", size = 6, hjust = -0.1, vjust = 1.2,
- family = "myNFont") +
- annotate(geom = "text", x = df_t$x2, y = df_t$y2, label = df_t$l2,
- color = "white", size = 6, hjust = -0.1, vjust = 1.,
- family = "myNFont") +
- annotate(geom = "text", x = 1:2 - width, y = c(17585, 18265),
- label = c("17,585", "18,265"), size = 6, hjust = 0, vjust = -0.8) +
- annotate(geom = "text", x = c(1, 1, 2) - width, y = c(18265, rep(18265 + 1000, 2)),
- label = c("所有情况", "男孩", "女孩"), size = 6, hjust = 0,
- vjust = -1.2, family = "myCFont") +
- geom_segment(x = 1 - width, xend = 0.95, y = 19450, yend = 19450, color = "gray50", size = 1) +
- geom_segment(x = 2 - width, xend = 1.86, y = 19450, yend = 19450, color = "gray50", size = 1) +
- geom_segment(x = 0.35, xend = 0.50, y = 476, yend = 476, size = 1, color = "gray50") +
- geom_segment(x = 0.35, xend = 0.50, y = 4324, yend = 4324, size = 1, color = "gray50") +
- annotate(geom = "text", x = 0.35, y = c(476, 4324), label = c("遗弃", "送养"),
- vjust = 1.5, hjust = 0, size = 5, family = "myCFont") +
- annotate(geom = "text", x = 0.45, y = 19265 - 0:6 * 1100,
- label = c("是", "家", "人", "主", "动", "放", "弃"),
- hjust = 1, vjust = -0.2, family = "myCFont", size = 11, color = "#486390") +
- annotate(geom = "text", x = 0.20, y = 19265 - 0:6 * 1100,
- label = c("相", "当", "多", "儿", "童", "失", "踪"),
- hjust = 1, vjust = -0.2, family = "myCFont", size = 11, color = "#6d8cbb") +
- geom_segment(x = 0.03, xend = 0.45, y = 12300, yend = 12300, color = "gray50", size = 1) +
- annotate(geom = "text", x = 0.05, y = 12100 - 0:13 * 400,
- label = c("注", ":", "这", "里", "的", "送", "养", "、", "遗", "弃", "仅", "为", "家", "人"),
- vjust = 1.5, hjust = 0, size = 4, family = "myCFont", color = "gray50") +
- annotate(geom = "text", x = 0.14, y = 12100 - 0:15 * 400,
- label = c("后", "来", "想", "念", "孩", "子", "主", "动", "寻", "找", ",", "非", "完", "整", "情", "况"),
- vjust = 1.5, hjust = 0, size = 4, family = "myCFont", color = "gray50") +
- annotate(geom = "text", x = 0.34, y = 12100 - 0:8 * 400,
- label = c("数", "据", "来", "源", ":", "宝", "贝", "回", "家"),
- vjust = 1.5, hjust = 1, size = 4, family = "myCFont", color = "gray50") +
- annotate(geom = "text", x = 0.45, y = 12100 - 0:3 * 400, label = c("单", "位", ":", "人"),
- vjust = 1.5, hjust = 1, size = 4, family = "myCFont", color = "gray50") +
- theme(text = element_text(face = "bold"))
【该图仅做学习用途,勿做他用】
过程中有几点感悟:
1、商业图表专业的地方在于构图和色彩,构图我是照抄的,色彩我是抓取原图的色彩,所以我“临摹”的技术含量不高;
2、对于商业展示图表,个人感觉用ggplot2和base R的画图差别没有那么大了,都要用“绣花”功夫,逐个添加元素;
3、ggplot2最有优势的地方应该在数据探索和分析者与数据的互动上;
4、字体很重要,可惜我没有足够的字体库。