我正在尝试绘制来自九个调查项目 (q1...q9) 的数据,这些项目都以 11 分制衡量对给定陈述的不同意/一致意见。我将数据重新排列成长格式,这有助于我将其可视化。
head(df)
# A tibble: 5 × 4
name value left right
<chr> <dbl> <chr> <chr>
1 q1 8 increase social spending decrease social spending
2 q2 1 invest in climate invest in economy
3 q3 3 increase help for ukraine maintain neutrality
4 q4 9 facilitate migration restrict migration
5 q5 4 more equity and diversity social equity already achieved
我想生成的是每个“变量”或
name
变量(q1
到q9
)的多面密度图,很像ggridges
。然而,我实际上并没有使用ggridges
,因为事实证明它不太通用。
问题: 我想在各自的密度图的任一侧绘制同意和不同意的选项(例如“减少社会支出”......“增加社会支出”)。我认为还需要两个步骤才能到达那里:
当前代码:
ggplot(df, aes(x = value, y = ..count.., fill = name)) +
geom_density(color = "transparent") +
facet_grid(name ~ ., switch = "y") +
scale_fill_viridis_d(direction = -1, guide = "none") +
labs(x="", y="") +
xlim(-1, 13) +
theme(panel.grid.minor = element_blank(),
axis.text.y = element_blank(),
strip.text.y.left = element_text(angle = 0, vjust = 0),
strip.text.y.right = element_text(angle = 0, vjust = 0))
示例数据: 这是重现我的 df(样本)的代码。
structure(list(name = c("q1", "q2", "q3", "q4", "q5", "q6", "q7",
"q8", "q9", "q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8", "q9",
"q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8", "q9", "q1", "q2",
"q3", "q4", "q5", "q6", "q7", "q8", "q9", "q1", "q2", "q3", "q4",
"q5", "q6", "q7", "q8", "q9", "q1", "q2", "q3", "q4", "q5", "q6",
"q7", "q8", "q9", "q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8",
"q9", "q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8", "q9", "q1",
"q2", "q3", "q4", "q5", "q6", "q7", "q8", "q9", "q1", "q2", "q3",
"q4", "q5", "q6", "q7", "q8", "q9"),
value = structure(c(8, 1, 3, 9, 4, 11, 3, 10, 6, 5, 1, 11, 8, 1, 7, 8, 9, 5, 4, 8, 6, 6,
8, 7, 4, 6, 5, 1, 3, 11, 9, 9, 6, 4, 10, 4, 7, 3, 5, 9, 6, 9,
6, 10, 8, 1, 3, 6, 8, 6, 11, 11, 11, 11, 3, 3, 1, 6, 9, 11, 6,
11, 8, 3, 9, 11, 1, 3, 11, 11, 3, 11, 5, 4, 11, 2, 8, 10, 6,
6, 9, 6, 10, 5, 6, 11, 10, 3, 8, 1)),
left = c("increase social spending", "invest in climate",
"increase help for ukraine", "facilitate migration", "more equity and diversity",
"more family aid for rich", "more surveillance more security",
"further european integration", "maintain mask mandate",
"increase social spending", "invest in climate", "increase help for ukraine",
"facilitate migration", "more equity and diversity", "more family aid for rich",
"more surveillance more security", "further european integration",
"maintain mask mandate", "increase social spending", "invest in climate",
"increase help for ukraine", "facilitate migration", "more equity and diversity",
"more family aid for rich", "more surveillance more security",
"further european integration", "maintain mask mandate",
"increase social spending", "invest in climate", "increase help for ukraine",
"facilitate migration", "more equity and diversity", "more family aid for rich",
"more surveillance more security", "further european integration",
"maintain mask mandate", "increase social spending", "invest in climate",
"increase help for ukraine", "facilitate migration", "more equity and diversity",
"more family aid for rich", "more surveillance more security",
"further european integration", "maintain mask mandate",
"increase social spending", "invest in climate", "increase help for ukraine",
"facilitate migration", "more equity and diversity", "more family aid for rich",
"more surveillance more security", "further european integration",
"maintain mask mandate", "increase social spending", "invest in climate",
"increase help for ukraine", "facilitate migration", "more equity and diversity",
"more family aid for rich", "more surveillance more security",
"further european integration", "maintain mask mandate",
"increase social spending", "invest in climate", "increase help for ukraine",
"facilitate migration", "more equity and diversity", "more family aid for rich",
"more surveillance more security", "further european integration",
"maintain mask mandate", "increase social spending", "invest in climate",
"increase help for ukraine", "facilitate migration", "more equity and diversity",
"more family aid for rich", "more surveillance more security",
"further european integration", "maintain mask mandate",
"increase social spending", "invest in climate", "increase help for ukraine",
"facilitate migration", "more equity and diversity", "more family aid for rich",
"more surveillance more security", "further european integration",
"maintain mask mandate"),
right = c("decrease social spending",
"invest in economy", "maintain neutrality", "restrict migration",
"social equity already achieved", "less family aid for rich",
"less surveillance more freedom", "european integration gone too far",
"abolish mask mandate", "decrease social spending", "invest in economy",
"maintain neutrality", "restrict migration", "social equity already achieved",
"less family aid for rich", "less surveillance more freedom",
"european integration gone too far", "abolish mask mandate",
"decrease social spending", "invest in economy", "maintain neutrality",
"restrict migration", "social equity already achieved", "less family aid for rich",
"less surveillance more freedom", "european integration gone too far",
"abolish mask mandate", "decrease social spending", "invest in economy",
"maintain neutrality", "restrict migration", "social equity already achieved",
"less family aid for rich", "less surveillance more freedom",
"european integration gone too far", "abolish mask mandate",
"decrease social spending", "invest in economy", "maintain neutrality",
"restrict migration", "social equity already achieved", "less family aid for rich",
"less surveillance more freedom", "european integration gone too far",
"abolish mask mandate", "decrease social spending", "invest in economy",
"maintain neutrality", "restrict migration", "social equity already achieved",
"less family aid for rich", "less surveillance more freedom",
"european integration gone too far", "abolish mask mandate",
"decrease social spending", "invest in economy", "maintain neutrality",
"restrict migration", "social equity already achieved", "less family aid for rich",
"less surveillance more freedom", "european integration gone too far",
"abolish mask mandate", "decrease social spending", "invest in economy",
"maintain neutrality", "restrict migration", "social equity already achieved",
"less family aid for rich", "less surveillance more freedom",
"european integration gone too far", "abolish mask mandate",
"decrease social spending", "invest in economy", "maintain neutrality",
"restrict migration", "social equity already achieved", "less family aid for rich",
"less surveillance more freedom", "european integration gone too far",
"abolish mask mandate", "decrease social spending", "invest in economy",
"maintain neutrality", "restrict migration", "social equity already achieved",
"less family aid for rich", "less surveillance more freedom",
"european integration gone too far", "abolish mask mandate")),
row.names = c(NA, -90L), class = c("tbl_df", "tbl", "data.frame"))
简单的第二个 y 轴怎么样?
library(tidyverse)
## use after_stat
ggplot(df, aes(x = value, y = after_stat(count), fill = name)) +
geom_density(color = "transparent") +
facet_grid(name ~ ., switch = "y") +
scale_fill_viridis_d(direction = -1, guide = "none") +
## use NULL!!!
labs(x = NULL) +
## use coord_ instead, but even better would be to remove the expansion,
## e.g. with scale_x_continuous(expand = c(0,0))
coord_cartesian(xlim = c(-1, 13)) +
theme(
panel.grid.minor = element_blank(),
axis.text.y = element_blank(),
strip.text.y.left = element_text(angle = 0, vjust = 0),
strip.text.y.right = element_text(angle = 0, vjust = 0)
) +
scale_y_continuous(
"decrease social spending",
sec.axis = dup_axis(name = "increase social spending")
)