gpt4 book ai didi

r - ggpubr不在ggdotchart中创建多个酒吧

转载 作者:行者123 更新时间:2023-12-01 13:00:17 25 4
gpt4 key购买 nike

利用ggpubr中的示例程序包代码,ggdotchart函数不会创建如示例中所示的单独的段,而是仅存在一个段,尽管点似乎以正确的方向放置。是否有人对可能出现的问题有任何提示?我认为这可能是由于小故障与df引起的,但我无法确定问题所在。

码:

df <- diamonds %>%
filter(color %in% c("J", "D")) %>%
group_by(cut, color) %>%
summarise(counts = n())

ggdotchart(df, x = "cut", y ="counts",
color = "color", palette = "jco", size = 3,
add = "segment",
add.params = list(color = "lightgray", size = 1.5),
position = position_dodge(0.3),
ggtheme = theme_pubclean()
)

预期输出:
enter image description here

但是相反,我得到了:
enter image description here

最佳答案

这是一种无需ggpubr::ggdotchart即可获得所需图的方法。问题似乎是geom_segment不允许闪避,如此处所述:R - ggplot dodging geom_lines和此处:how to jitter/dodge geom_segments so they remain parallel?

# your data
df <- diamonds %>%
filter(color %in% c("J", "D")) %>%
group_by(cut, color) %>%
summarise(counts = n())

第一步是扩展数据。当我们调用允许躲避的 geom_line时,我们将需要它。我从 @Stibu's answer采纳了这个想法。我们创建 df的副本,并将 counts列中的 0列更改为 df2。最后,我们使用 bind_rowsdfdf2创建单个数据帧。
df2 <- df
df2$counts <- 0

df_out <- purrr::bind_rows(df, df2)
df_out

然后,我使用 ggplot创建/复制所需的输出。
ggplot(df_out, aes(x = cut, y = counts)) +
geom_line(
aes(col = color), # needed for dodging, we'll later change colors to "lightgrey"
position = position_dodge(width = 0.3),
show.legend = FALSE,
size = 1.5
) +
geom_point(
aes(fill = color),
data = subset(df_out, counts > 0),
col = "transparent",
shape = 21,
size = 3,
position = position_dodge(width = 0.3)
) +
scale_color_manual(values = c("lightgray", "lightgray")) + #change line colors
ggpubr::fill_palette(palette = "jco") +
ggpubr::theme_pubclean()

enter image description here

关于r - ggpubr不在ggdotchart中创建多个酒吧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53308501/

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