ggplot:geom_dotplot,将点集中在多条线上

问题描述 投票:0回答:2

我有以下点图:

我想实现两件事:

  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)
r ggplot2 plot point facet
2个回答
1
投票

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

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))


0
投票

你最后做了什么?

© www.soinside.com 2019 - 2024. All rights reserved.