ggpubr 没有在 ggdotchart 中创建多个条
ggpubr not creating multiple bars in ggdotchart
利用 ggpubr
中的示例包代码,ggdotchart
函数不会像示例中那样创建单独的段,而是只有一个段,尽管这些点似乎是放置在正确的方向。有没有人对问题可能有什么建议?我一直以为可能是tibbles vs. 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()
)
预期输出为:
但是我得到的是:
这是一种无需 ggpubr::ggdotchart
即可获得所需情节的方法。问题似乎是 geom_segment
不允许闪避,如此处讨论: and here: 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
时,我们将需要它。我从 那里得到了这个想法。我们创建 df
的副本并将 counts
列更改为 df2
中的 0
。最后我们使用 bind_rows
从 df
和 df2
.
创建一个数据框
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()
您需要一个额外的“组”参数!
df <- diamonds %>%
dplyr::filter(color %in% c("J", "D")) %>%
dplyr::group_by(cut, color) %>%
dplyr::summarise(counts = n())
ggdotchart(df, x = "cut", y ="counts",
color = "color", group="color", # here it is
palette = "jco", size = 3,
add = "segment",
add.params = list(color = "lightgray", size = 1.5),
position = position_dodge(0.3),
ggtheme = theme_pubclean()
)
利用 ggpubr
中的示例包代码,ggdotchart
函数不会像示例中那样创建单独的段,而是只有一个段,尽管这些点似乎是放置在正确的方向。有没有人对问题可能有什么建议?我一直以为可能是tibbles vs. 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()
)
预期输出为:
但是我得到的是:
这是一种无需 ggpubr::ggdotchart
即可获得所需情节的方法。问题似乎是 geom_segment
不允许闪避,如此处讨论:
# your data
df <- diamonds %>%
filter(color %in% c("J", "D")) %>%
group_by(cut, color) %>%
summarise(counts = n())
第一步是扩展数据。当我们调用允许躲避的 geom_line
时,我们将需要它。我从 df
的副本并将 counts
列更改为 df2
中的 0
。最后我们使用 bind_rows
从 df
和 df2
.
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()
您需要一个额外的“组”参数!
df <- diamonds %>%
dplyr::filter(color %in% c("J", "D")) %>%
dplyr::group_by(cut, color) %>%
dplyr::summarise(counts = n())
ggdotchart(df, x = "cut", y ="counts",
color = "color", group="color", # here it is
palette = "jco", size = 3,
add = "segment",
add.params = list(color = "lightgray", size = 1.5),
position = position_dodge(0.3),
ggtheme = theme_pubclean()
)