我目前正在使用 ggridges 库,以制作“joychart”。我写了这个:
data3 %>%
mutate(ftFct = fct_rev(as.factor(ft_newnum))) %>%
ggplot(aes(y = ftFct)) +
geom_density_ridges(aes(x = ft, fill = paste(ftFct, rep)),
alpha = .8, color = "white", from = 0, to = 100) +
labs(x = "Feeling Themometer Responses (%)",
y = " ",
title = "Republican vs Democratic Views Towards...",
subtitle = "Analysis unit: students (n = 595)") +
scale_y_discrete(expand = c(0.01, 0)) +
scale_x_continuous(expand = c(0.01, 0)) +
scale_fill_cyclical(breaks = c("2 0", "2 1"),
labels = c(`2 0` = "Democrat", `2 1` = "Republican"),
values = c("#8080ff", "#ff8080", "#0000ff", "#ff0000"),
name = "Political Affiliation", guide = "legend") +
theme_ridges(grid = FALSE)
...这让我得到了这个数字:
这正是我想要的——完美的格式,每行在深色和浅色之间交替,提供一些对比度和提高可读性。
接下来,我标记 y 轴变量,以便我们知道我们在看什么。我这样标记“ft_newnum”:
data3$ft_newnum <- factor(data3$ft_newnum,
levels = c(2,3,4,5,6,7,9,11,12, 13, 14, 15),
labels = c("Donald Trump", "Christian fundamentalists", "Elites", 'Republicans', 'Denison Republicans', 'Denison Greeks', 'Denison Varsity Athlete','Hillary Clinton', 'Denison Democrats', 'Democrats', 'Bernie Sanders', 'Weinberg'))
然后编辑代码以合并此更改:
data3 %>%
mutate(ftFct = fct_rev(as.factor(ft_newnum))) %>%
ggplot(aes(y = ftFct)) +
geom_density_ridges(aes(x = ft, fill = paste(ftFct, rep)),
alpha = .8, color = "white", from = 0, to = 100) +
labs(x = "Feeling Themometer Responses (%)",
y = " ",
title = "Republican vs Democratic Views Towards...",
subtitle = "Analysis unit: students (n = 595)") +
scale_y_discrete(expand = c(0.01, 0)) +
scale_x_continuous(expand = c(0.01, 0)) +
scale_fill_cyclical(breaks = c("Donald Trump 0", "Donald Trump 1"),
labels = c(`Donald Trump 0` = "Democrat", `Donald Trump 1` = "Republican"),
values = c("#8080ff", "#ff8080", "#0000ff", "#ff0000"),
name = "Political Affiliation", guide = "legend") +
theme_ridges(grid = FALSE)
该代码绘制了这个数字:
它几乎是完美的,但问题是,浅色和深色之间的交替已关闭。前两条线是深色的,然后是两条浅色线。我需要保留标签,但也要保持准确的周期性交替,如第一张图所示。
有任何想法吗?谢谢!