【问题标题】:How can I reproduce this chart in ggplot2?如何在 ggplot2 中重现此图表?
【发布时间】:2022-08-15 21:19:35
【问题描述】:

我想使用ggplot2 复制以下图表。有什么帮助吗?

这是数据。 (icv 是 LHS 条,afl 是 RHS 条)


library(tibble)

vectorcolors <- c(\"#967D2D\", \"#333333\", \"white\")

df <- tibble(
reg = c(\"Duarte\",\"La Vega\",\"Santiago\",\"Hermanas Mirabal\",
         \"Sanchez Ramírez\",\"María Trinidad Sánchez\",\"Monseñor Nouel\",
         \"Puerto Plata\",\"Samaná\",\"Espaillat\",\"Valverde\",
         \"Santiago Rodríguez\",\"Dajabón\",\"Montecristi\"), 
icv = c(69.9, 58.8, 57.1, 54.7, 53.9, 53.3, 49.3,
         48.8, 47.0, 45.4, 44.2, 43.2, 42.8, 42.5),
afl = c(799.3, 800.6, 851.1, 711.7, 839.6, 710.9,
         823.2, 912.7, 605.8, 832.7, 692.0, 858.4,
         758.1, 616.6)
)
  • 你试过什么吗?你到底在哪里卡住了?
  • 我建议您使用两个geom_col() 图层,一个使用aes(x = -icv, y = reg),一个使用aes(x = afl, y= reg),左侧使用棕色填充,右侧使用白色填充。然后是两个geom_text() 的数字。一个geom_text() 用于y 轴名称,使用数据的精简版本,每个都有一个值。最后theme_void() 删除任何轴线、刻度线、标签或标题。请证明您已经尝试过什么,人们会很乐意帮助您解决问题。

标签: r ggplot2 bar-chart


【解决方案1】:
library(ggplot2)
library(tibble)

colors <- list("sand" = "#967D2D", "gray" = "#333333", "white" = "white")

df <- tibble(
  reg = c("Duarte","La Vega","Santiago","Hermanas Mirabal",
          "Sanchez Ramírez","María Trinidad Sánchez","Monseñor Nouel",
          "Puerto Plata","Samaná","Espaillat","Valverde",
          "Santiago Rodríguez","Dajabón","Montecristi"), 
  icv = c(69.9, 58.8, 57.1, 54.7, 53.9, 53.3, 49.3,
          48.8, 47.0, 45.4, 44.2, 43.2, 42.8, 42.5),
  afl = c(799.3, 800.6, 851.1, 711.7, 839.6, 710.9,
          823.2, 912.7, 605.8, 832.7, 692.0, 858.4,
          758.1, 616.6)
)


df %>%
  ggplot(aes(y = forcats::fct_reorder(reg, icv))) +
  geom_col(aes(x = icv * -1), fill = colors$sand, color = colors$gray) +
  geom_col(aes(x = afl / 10), fill = colors$white, color = colors$gray) +
  geom_text(aes(label = icv, x = icv * -1, hjust = -0.2), color = "white", size = 3) +
  geom_text(aes(label = afl , x = afl / 10, hjust = 1.1), color = colors$gray, size = 3) +
  geom_text(aes(label = reg , x = 1), color = colors$gray, size = 3, hjust = 0) +
  labs(fill = NULL, color = NULL, x = NULL, y = NULL) +
  theme_minimal() +
  theme(
    legend.position = "none",
    panel.grid = element_blank(),
    axis.text = element_blank(),
    axis.title.y =  element_text()
  )

【讨论】:

    猜你喜欢
    • 2021-12-01
    • 2021-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    相关资源
    最近更新 更多