使用R和ggplot2重新创建条形图

yshpjwxd  于 2023-11-14  发布在  其他
关注(0)|答案(1)|浏览(235)

嘿,有谁能帮忙在R中用ggplot 2重建一个图吗?

  1. library(ggplot)
  2. library(tidyverse)
  3. library(ggthemes)

字符串
我们正在考虑创建两个独立的图形,并使用拼接或cowplot和ggthemes进行设计。如果有更多经验的人可以帮助我们完成这项任务,那就太好了。任何帮助都非常感谢!

5w9g7ksd

5w9g7ksd1#

出于好奇心和练习,这里有一种可能的方法来重新创建基于facet_grid的原始FiveThirtyEight图表,并进行许多额外的自定义,以获得正确的外观和所有细节。

  1. library(tidyverse)
  2. library(ggthemes)
  3. data <- read_csv("https://raw.githubusercontent.com/fivethirtyeight/data/master/masculinity-survey/raw-responses.csv", col_names = TRUE) %>%
  4. select(q0005, age3)
  5. table(data$q0005)
  6. pal_color <- c("#ED713A", "#8DDADF", "#CDCDCD")
  7. names(pal_color) <- c("Yes", "No", "No answer")
  8. pal_fte <- deframe(ggthemes::ggthemes_data[["fivethirtyeight"]])
  9. data_sum <- data |>
  10. count(q0005, age3) |>
  11. mutate(group = "b")
  12. data_sum <- data_sum |>
  13. summarise(
  14. group = "a",
  15. age3 = "All adult men",
  16. n = sum(n),
  17. .by = q0005
  18. ) |>
  19. bind_rows(data_sum) |>
  20. mutate(pct = n / sum(n), .by = age3) |>
  21. mutate(
  22. age3 = case_match(
  23. age3,
  24. "18 - 34" ~ "18-34",
  25. "35 - 64" ~ "35-64",
  26. "65 and up" ~ "65+",
  27. .default = age3
  28. ),
  29. age3 = factor(
  30. age3,
  31. rev(c("18-34", "35-64", "65+", "All adult men"))
  32. ),
  33. q0005 = factor(
  34. q0005,
  35. rev(c("Yes", "No answer", "No"))
  36. )
  37. )
  38. data_labels <- data_sum |>
  39. filter(group == "a") |>
  40. arrange(desc(q0005)) |>
  41. mutate(
  42. pct = cumsum(pct),
  43. pctlag = lag(pct, default = 0),
  44. y = .5 * pct + .5 * pctlag
  45. )
  46. data_segment <- data_labels |>
  47. filter(q0005 == "No answer")
  48. data_labels <- data_labels |>
  49. filter(q0005 != "No answer")
  50. ggplot(data_sum, aes(pct, age3)) +
  51. geom_segment(
  52. data = data_segment,
  53. aes(
  54. x = .4, xend = y,
  55. ),
  56. y = 0, yend = 0, color = pal_fte[["Dark Gray"]]
  57. ) +
  58. geom_segment(
  59. data = data_segment,
  60. aes(
  61. x = y, xend = y,
  62. ),
  63. y = 0, yend = 1, color = pal_fte[["Dark Gray"]]
  64. ) +
  65. geom_label(
  66. data = data.frame(
  67. x = .4, group = "a", hjust = 0,
  68. label = "No answer"
  69. ),
  70. aes(x = x, hjust = hjust, label = label),
  71. y = 0, hjust = 0, color = pal_fte[["Dark Gray"]],
  72. label.size = 0, fill = pal_fte[["Light Gray"]],
  73. inherit.aes = FALSE, size = .8 * 12 / .pt
  74. ) +
  75. geom_vline(
  76. xintercept = .5
  77. ) +
  78. geom_col(aes(fill = q0005), width = .6) +
  79. geom_vline(
  80. xintercept = c(0, 1)
  81. ) +
  82. geom_label(
  83. data = data_labels,
  84. aes(x = pctlag, label = q0005),
  85. hjust = 0, nudge_x = .025,
  86. fontface = "bold", label.size = 0, fill = NA,
  87. size = .8 * 12 / .pt
  88. ) +
  89. geom_text(
  90. aes(x = -.25, hjust = 0, label = age3),
  91. fontface = "bold", size = .8 * 12 / .pt
  92. ) +
  93. geom_text(
  94. data = data.frame(
  95. group = "b"
  96. ),
  97. aes(x = -.25, hjust = 0, label = "BY AGE GROUP"),
  98. y = 4, color = "black",
  99. size = .6 * 12 / .pt
  100. ) +
  101. scale_fill_manual(
  102. values = pal_color,
  103. guide = "none"
  104. ) +
  105. scale_x_continuous(
  106. breaks = seq(0, 1, .1),
  107. labels = \(x) paste0(100 * x, c("%", rep("", 10))),
  108. position = "top",
  109. expand = c(0, 0)
  110. ) +
  111. coord_cartesian(clip = "off") +
  112. facet_grid(group ~ ., space = "free_y", scales = "free_y") +
  113. ggthemes::theme_fivethirtyeight(base_size = 12) +
  114. theme(
  115. panel.grid.major.y = element_blank(),
  116. strip.text.y = element_blank(),
  117. panel.spacing.y = unit(1, "cm"),
  118. axis.text.y = element_blank(),
  119. axis.text.x = element_text(
  120. family = "mono",
  121. color = pal_fte[["Dark Gray"]]
  122. ),
  123. plot.title.position = "plot",
  124. plot.title = element_text(
  125. size = rel(1.25)
  126. ),
  127. plot.caption = element_text(
  128. family = "mono",
  129. size = rel(.6),
  130. color = pal_fte[["Dark Gray"]],
  131. margin = margin(20, unit = "pt")
  132. ),
  133. plot.caption.position = "plot"
  134. ) +
  135. labs(
  136. x = NULL, y = NULL,
  137. title = paste(
  138. "Do you think society puts pressure on men in a way",
  139. "that is unhealthy or bad for them?",
  140. sep = "\n"
  141. ),
  142. subtitle = "",
  143. caption = "SOURCE: FIVETHIRTYEIGHT/DEATH, SEX & MONEY/SURVEY"
  144. )
  145. ggsave("fivethirtyeight.png",
  146. width = 550, height = 350, units = "px",
  147. dpi = 300, scale = 300 / 96
  148. )

字符串


的数据

展开查看全部

相关问题