簡體   English   中英

在 R 中使用 fct_relevel 和 ggplot 重新排序變量

[英]Reordering variables using fct_relevel with ggplot in R

我正在嘗試使用 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"))

在 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)

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM