我有以下点图:
我想实现两件事:
可以通过
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))
你最后做了什么?