Вот способ получить желаемый участок без ggpubr::ggdotchart
.Кажется, проблема в том, что geom_segment
не позволяет уклоняться, как обсуждалось здесь: R - ggplot dodging geom_lines и здесь: как дрожать / уклоняться от geom_segments, чтобы они оставались параллельными? .
# your data
df <- diamonds %>%
filter(color %in% c("J", "D")) %>%
group_by(cut, color) %>%
summarise(counts = n())
Первым шагом является расширение ваших данных.Нам это понадобится, когда мы позвоним geom_line
, что позволяет уклоняться.Я взял эту идею из ответа @ Стибу .Мы создаем копию df
и изменяем столбец counts
на 0
в df2
.Наконец, мы используем 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()
![enter image description here](https://i.stack.imgur.com/ZBLxe.png)