在 R 中使用 fct_relevel 和 ggplot 重新排序变量
Posted
技术标签:
【中文标题】在 R 中使用 fct_relevel 和 ggplot 重新排序变量【英文标题】:Reordering variables using fct_relevel with ggplot in R 【发布时间】:2021-05-23 13:05:04 【问题描述】:我正在尝试使用 fct_relevel() 对图中的变量进行重新排序。我尝试将列更改为一个因子。我不确定为什么我的代码不起作用。我需要“拥有的面板”显示在“没有拥有的面板”前面。我也对不依赖 fct_relevel() 的替代方案持开放态度。
图形代码:
groups %>%
mutate(panels = fct_relevel(panels), "Owned Panels", "Did Not Own Panels") %>%
ggplot(., aes(x=reason, y = mean, fill = panels)) +
geom_bar(stat = "identity", color = "black", position = position_dodge()) +
geom_errorbar(aes(ymin = mean - se, ymax = mean+se), width = .2, position = position_dodge(.9)) +
geom_text(aes(label = round(mean, digits =2)), position = position_dodge(width=1.0), vjust = -1.5) +
#facet_wrap(~dv) +
labs(title = ~ "Likelihood of solar panel installation after meeting ambassador",
y = "Likelihood of installing solar panels",
x = "Reason to install solar panels") +
scale_fill_discrete(name = "Ambassador solar\npanel ownership") +
scale_y_continuous(limits = c(1, 7), oob = scales::oob_squish)
数据:
structure(list(dv = c("behavior", "behavior", "behavior", "behavior"
), panels = c("Owned Panels", "Owned Panels", "Did Not Own Panels",
"Did Not Own Panels"), reason = c("Environment", "Money", "Environment",
"Money"), mean = c(5.15789473684211, 5.36065573770492, 4.85454545454545,
4.35483870967742), se = c(0.224988824122626, 0.194223670966034,
0.187878787878788, 0.210884132880012)), row.names = c(NA, -4L
), groups = structure(list(dv = c("behavior", "behavior"), panels = c("Did Not Own Panels",
"Owned Panels"), .rows = structure(list(3:4, 1:2), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = 1:2, class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
【问题讨论】:
【参考方案1】:在 OP 的代码中,fct_relevel
在指定级别之前已关闭
library(forcats)
library(dplyr)
library(ggplot2)
groups %>%
mutate(panels = fct_relevel(panels), "Owned Panels", "Did Not Own Panels")
^
相反,它会是(在这样做之前也是ungroup
)
groups %>%
ungroup %>%
mutate(panels = fct_relevel(panels, "Owned Panels", "Did Not Own Panels") ) %>%
ggplot(., aes(x=reason, y = mean, fill = panels)) +
geom_bar(stat = "identity", color = "black", position = position_dodge()) +
geom_errorbar(aes(ymin = mean - se, ymax = mean+se), width = .2, position = position_dodge(.9)) +
geom_text(aes(label = round(mean, digits =2)),
position = position_dodge(width=1.0), vjust = -1.5) +
#facet_wrap(~dv) +
labs(title = ~ "Likelihood of solar panel installation after meeting ambassador",
y = "Likelihood of installing solar panels",
x = "Reason to install solar panels") +
scale_fill_discrete(name = "Ambassador solar\npanel ownership") +
scale_y_continuous(limits = c(1, 7), oob = scales::oob_squish)
【讨论】:
为什么需要 ungroup()? @melbez 原因是如果有组,那么fct_relevel
在该组中找不到某些级别(因为其中一些只有元素),因为您显示的 dput 有 panels
作为character
类。根据代码,我们在绘图之前没有进行任何分组汇总活动。所以,取消分组是安全的以上是关于在 R 中使用 fct_relevel 和 ggplot 重新排序变量的主要内容,如果未能解决你的问题,请参考以下文章