gpt4 book ai didi

r - 这种类型的情节可以用ggplot2完成吗?

转载 作者:行者123 更新时间:2023-12-04 22:09:45 24 4
gpt4 key购买 nike

我正在使用 Rggplot2 来做一些用于发布目的的绘图。我遇到了这个情节,我想使用 ggplot2 复制它。但是,我从未见过这样的情节使用 ggplot2

可以用 ggplot2 完成吗?条形下方的文字呢?我想这些必须在 ggplot2 代码中进行硬编码。你如何对齐这些文本?

Bar graph

最佳答案

这非常接近:

# Generate sample data (I'm too lazy to type out the full labels)
df <- data.frame(
perc = c(60, 36, 44, 41, 42, 57, 34, 52),
type = rep(c("blue", "green"), 4),
label = rep(c(
"Individual reports created as needed",
"Regular reports on single topics",
"Analytics using data integrated from multiple systems",
"Business unit-specific dashboards and visuals"), each = 2))


library(ggplot2)
ggplot(df, aes(1, perc, fill = type)) +
geom_col(position = "dodge2") +
scale_fill_manual(values = c("turquoise4", "forestgreen"), guide = FALSE) +
facet_wrap(~ label, ncol = 1, strip.position = "bottom") +
geom_text(
aes(y = 1, label = sprintf("%i%%", perc)),
colour = "white",
position = position_dodge(width = .9),
hjust = 0,
fontface = "bold") +
coord_flip(expand = F) +
theme_minimal() +
theme(
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
strip.text = element_text(angle = 0, hjust = 0, face = "bold"))

enter image description here

几个解释:
  • 我们分别使用躲避条和匹配躲避标签与 position = "dodge2" (注意这需要 ggplot_ggplot2_3.0.0 ,否则使用 position = position_dodge(width = 1.0) )和 position = position_dodge(width = 0.9)
  • 我们使用 facet_wrap 并强制采用一列布局;条形标签移到底部。
  • 我们使用 coord_flip(expand = F) 旋转整个图,其中 expand = F 确保左对齐( hjust = 0 )小平面条文本与 0 对齐。
  • 最后,我们调整主题以增加整体审美相似性。
  • 关于r - 这种类型的情节可以用ggplot2完成吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52095354/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com