如何使用R中的ggplot更改我的箱线图的顺序?

huangapple go评论141阅读模式
英文:

How do I change the order of my boxplots in ggplot using R?

问题

I am trying to make my two boxplots switch places, Any_Enrichment to the right and Any_Enrichment_Control to the left.

我试图交换我的两个箱线图的位置,将Any_Enrichment放在右边,将Any_Enrichment_Control放在左边。

I'm sure its super trivial but I could not figure out how its done with the right syntax.

我确信这非常简单,但我无法找到正确的语法来实现它。

My boxplot:

我的箱线图:

data3 %>%
dplyr::select(Any_Enrichment, Any_Enrichment_Control) %>%
pivot_longer(cols = everything(), names_to = "Variable", values_to = "Value") %>%
ggplot(aes(x= Variable, y = Value, fill = Variable)) +
stat_boxplot(geom= "errorbar", width= 0.5) +
geom_boxplot(fill=c("#fa9d5a", "cadetblue3"))+
labs(x="", y="Frequency (total)", title="Stay on enclosure site - Any enrichment")+
theme_minimal()+
theme(axis.title.x = element_text(size =16))+
theme(axis.title.y = element_text(size =16))+
theme(plot.title=element_text(size = 17))+
theme(plot.title = element_text(hjust = 0.5))+
theme(axis.text.x = element_text(size = 13, color="black"))+
theme(plot.title = element_text(hjust = 0.5))+
theme(plot.title = element_text(vjust = 1.5))+
theme(axis.title.y =element_text(vjust = 2))+
stat_summary(fun = mean, geom = "errorbar", aes(ymax = ..y.., ymin = ..y..),
width = .75,lwd = 0.5, linetype = "dashed", col = "red") +
#scale_x_discrete(labels = c("Enrichment absent", "Enrichment present"))+
ylim(0,30)

I'm very new to R and any help is highly appreciated!

我对R非常陌生,非常感谢任何帮助!

英文:

如何使用R中的ggplot更改我的箱线图的顺序?I am trying to make my two boxplots switch places, Any_Enrichment to the right and Any_Enrichment_Control to the left.

I'm sure its super trivial but I could not figure out how its done with the right syntax.

My boxplot:

data3 %>% 
  dplyr::select(Any_Enrichment, Any_Enrichment_Control) %>%
  pivot_longer(cols = everything(), names_to = "Variable", values_to = "Value") %>%
  ggplot(aes(x= Variable, y = Value, fill = Variable)) +
  stat_boxplot(geom= "errorbar", width= 0.5) +
  geom_boxplot(fill=c("#fa9d5a", "cadetblue3"))+
  labs(x="", y="Frequency (total)", title="Stay on enclosure site - Any enrichment")+
  theme_minimal()+
  theme(axis.title.x = element_text(size =16))+
  theme(axis.title.y = element_text(size =16))+
  theme(plot.title=element_text(size = 17))+
  theme(plot.title = element_text(hjust = 0.5))+
  theme(axis.text.x = element_text(size = 13, color="black"))+
  theme(plot.title = element_text(hjust = 0.5))+
  theme(plot.title = element_text(vjust = 1.5))+
  theme(axis.title.y =element_text(vjust = 2))+
  stat_summary(fun = mean, geom = "errorbar", aes(ymax = ..y.., ymin = ..y..),
               width = .75,lwd = 0.5, linetype = "dashed", col = "red") +
  #scale_x_discrete(labels = c("Enrichment absent", "Enrichment present"))+
  ylim(0,30) 

I'm very new to R and any help is highly appreciated!

答案1

得分: 2

要在ggplot中沿着离散刻度更改项目(例如箱线图)的顺序,您可以重新排列/重新排序轴下的因子。虽然这可以在基本R中完成,但【forcats】提供了一些便利的因子操作工具。

例如,使用forcats:

library(dplyr)
library(tidyr)

## 将示例数据转换为长格式:
d <- structure(list(Any_Enrichment = c(0L, 30L, 30L, 28L, 4L, 12L),
                    Any_Enrichment_Control = c(16L, 9L, 30L, 18L, 30L, 3L)),
               row.names = c(NA,  6L),
               class = "data.frame") 

d <- d %>%
    pivot_longer(contains('Enrichment'),
                 names_to = 'Treatment', values_to = 'Value'
                 )
library(ggplot2)

## 默认按字母数字顺序的箱线图
d %>%
    ggplot(aes(Treatment, Value)) +
    geom_boxplot()

如何使用R中的ggplot更改我的箱线图的顺序?

library(forcats)

## 使用relevel重新排列Treatment后的箱线图
d %>%
    mutate(Treatment = fct_relevel(Treatment,
                                   c('Any_Enrichment_Control', 'Any_Enrichment')
                                   )
           ) %>%
    ggplot(aes(Treatment, Value)) +
    geom_boxplot()

如何使用R中的ggplot更改我的箱线图的顺序?

英文:

To change the order of items (e.g. boxplots) along a discrete scale in ggplot, you can relevel/reorder the factor underlying the axis. While this can be done in base R, {forcats} offers a number of helpers for convenient factor manipulation.

Example, using forcats:

library(dplyr)
library(tidyr)

## your sample data, pivoted into long format:
d &lt;- structure(list(Any_Enrichment = c(0L, 30L, 30L, 28L, 4L, 12L),
                    Any_Enrichment_Control = c(16L, 9L, 30L, 18L, 30L, 3L)),
               row.names = c(NA,  6L),
               class = &quot;data.frame&quot;) 


d &lt;- d |&gt;
    pivot_longer(contains(&#39;Enrichment&#39;),
                 names_to = &#39;Treatment&#39;, values_to = &#39;Value&#39;
                 )
library(ggplot2)

## boxplots in default alphanumeric order 
d |&gt;
    ggplot(aes(Treatment, Value)) +
    geom_boxplot()

如何使用R中的ggplot更改我的箱线图的顺序?

library(forcats)

## boxplots after `relevel`ing Treatment
d |&gt;
    mutate(Treatment = fct_relevel(Treatment,
                                   c(&#39;Any_Enrichment_Control&#39;, &#39;Any_Enrichment&#39;)
                                   )
           ) |&gt;
    ggplot(aes(Treatment, Value)) +
    geom_boxplot()

如何使用R中的ggplot更改我的箱线图的顺序?

huangapple
  • 本文由 发表于 2023年7月3日 04:05:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76600583.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定