gpt4 book ai didi

r - 在 R 中使用 ggridges 的循环错误(joyplot)

转载 作者:行者123 更新时间:2023-12-04 09:32:21 26 4
gpt4 key购买 nike

我目前正在与 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)

......这让我得到了这个数字:

enter image description here

这正是我想要的 - 完美的格式,每行在深色和浅色之间交替,提供一些对比度和增加可读性。

接下来,我标记 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)

该代码绘制了这个图:

enter image description here

这几乎是完美的,但问题是,浅色和深色之间的交替是关闭的。前两条线是深色的,然后是两条浅色的线。我需要保留标签,但也要保留准确的循环交替,如第一张图所示。

有任何想法吗?谢谢!

最佳答案

啊,我想通了。不是覆盖 'ft_newnum' 变量,而是创建一个新变量 (ft_newnum2)。

data3$ft_newnum2 <- 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'))

ft_num2 用于设置 y 轴,而原始 ft_num 保留并用于填充绘图。
data3 %>%
mutate(ftFct = fct_rev(as.factor(ft_newnum2))) %>%
ggplot(aes(y = ftFct)) +
geom_density_ridges(aes(x = ft, fill = paste(ft_newnum, 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(`Donald Trump 0` = "Democrat", `Donald Trump 1` = "Republican"),
values = c("#8080ff", "#ff8080", "#0000ff", "#ff0000"),
name = "Political Affiliation", guide = "legend") +
theme_ridges(grid = FALSE) +
theme(legend.position="bottom")

enter image description here

关于r - 在 R 中使用 ggridges 的循环错误(joyplot),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47863456/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com