ggplot:geom_dotplot,将点置于多行中间

ggplot: geom_dotplot, center the dots over multiple lines

我有以下点图:

我想实现两件事:

  1. 我希望这些点排列成两行(第一行有 5 个点,第二行还有 5 个点),而不是每个面都在一行中的点,如下所示:

  1. 在每个方面,点图应位于最中心。

是否可以通过 geom_dotplot() 实现?非常感谢您的帮助!

我的数据框:

df <- tibble::tibble(
  has_twitter = c(0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 
    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1),
  Quartile = c("Q1", "Q1", "Q1", "Q1", "Q1", "Q1", "Q1", "Q1", "Q1", "Q1", 
    "Q2", "Q2", "Q2", "Q2", "Q2", "Q2", "Q2", "Q2", "Q2", "Q2", "Q3", 
    "Q3", "Q3", "Q3", "Q3", "Q3", "Q3", "Q3", "Q3", "Q3", "Q4", "Q4", 
    "Q4", "Q4", "Q4", "Q4", "Q4", "Q4", "Q4", "Q4")

我的 ggplot:

library(ggplot2)
ggplot(df, aes(x = Quartile, y = 1)) +
  geom_dotplot(binaxis="y",
               fill = factor(df$has_twitter),
               binwidth = 1,
               dotsize=0.2,
               stackdir="centerwhole") +
  theme(axis.title = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank()) +
  facet_wrap(~ Quartile, nrow = 4)

这是您正在寻找的解决方案吗?

library(tidyverse)

# data manipulation
df1 <- df %>% 
    mutate(group = ifelse(Quartile == "Q1" | Quartile == "Q2", "Q1_Q2", "Q3_Q4")) %>% 
    group_by(group) %>% 
    mutate(id = rep(row_number(), each=10, length.out = n()))

# plot
ggplot(df1, aes(x = 0.9, y = id)) +
    geom_dotplot(binaxis="y",
                 fill = factor(df$has_twitter),
                 binwidth = 1,
                 dotsize=0.2,
                 binpositions = "all") +
    theme(axis.title = element_blank(),
          axis.text = element_blank(),
          axis.ticks = element_blank()) +
    facet_wrap(~ group, nrow =2) +
    coord_cartesian(xlim = c(0.5, 1.5))