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

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

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:

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

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

答案1

得分: 2

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

例如,使用forcats:

  1. library(dplyr)
  2. library(tidyr)
  3. ## 将示例数据转换为长格式:
  4. d <- structure(list(Any_Enrichment = c(0L, 30L, 30L, 28L, 4L, 12L),
  5. Any_Enrichment_Control = c(16L, 9L, 30L, 18L, 30L, 3L)),
  6. row.names = c(NA, 6L),
  7. class = "data.frame")
  8. d <- d %>%
  9. pivot_longer(contains('Enrichment'),
  10. names_to = 'Treatment', values_to = 'Value'
  11. )
  1. library(ggplot2)
  2. ## 默认按字母数字顺序的箱线图
  3. d %>%
  4. ggplot(aes(Treatment, Value)) +
  5. geom_boxplot()

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

  1. library(forcats)
  2. ## 使用relevel重新排列Treatment后的箱线图
  3. d %>%
  4. mutate(Treatment = fct_relevel(Treatment,
  5. c('Any_Enrichment_Control', 'Any_Enrichment')
  6. )
  7. ) %>%
  8. ggplot(aes(Treatment, Value)) +
  9. 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:

  1. library(dplyr)
  2. library(tidyr)
  3. ## your sample data, pivoted into long format:
  4. d &lt;- structure(list(Any_Enrichment = c(0L, 30L, 30L, 28L, 4L, 12L),
  5. Any_Enrichment_Control = c(16L, 9L, 30L, 18L, 30L, 3L)),
  6. row.names = c(NA, 6L),
  7. class = &quot;data.frame&quot;)
  8. d &lt;- d |&gt;
  9. pivot_longer(contains(&#39;Enrichment&#39;),
  10. names_to = &#39;Treatment&#39;, values_to = &#39;Value&#39;
  11. )
  1. library(ggplot2)
  2. ## boxplots in default alphanumeric order
  3. d |&gt;
  4. ggplot(aes(Treatment, Value)) +
  5. geom_boxplot()

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

  1. library(forcats)
  2. ## boxplots after `relevel`ing Treatment
  3. d |&gt;
  4. mutate(Treatment = fct_relevel(Treatment,
  5. c(&#39;Any_Enrichment_Control&#39;, &#39;Any_Enrichment&#39;)
  6. )
  7. ) |&gt;
  8. ggplot(aes(Treatment, Value)) +
  9. 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:

确定