英文:
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非常陌生,非常感谢任何帮助!
英文:
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()
library(forcats)
## 使用relevel重新排列Treatment后的箱线图
d %>%
mutate(Treatment = fct_relevel(Treatment,
c('Any_Enrichment_Control', 'Any_Enrichment')
)
) %>%
ggplot(aes(Treatment, Value)) +
geom_boxplot()
英文:
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 <- 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)
## boxplots in default alphanumeric order
d |>
ggplot(aes(Treatment, Value)) +
geom_boxplot()
library(forcats)
## boxplots after `relevel`ing Treatment
d |>
mutate(Treatment = fct_relevel(Treatment,
c('Any_Enrichment_Control', 'Any_Enrichment')
)
) |>
ggplot(aes(Treatment, Value)) +
geom_boxplot()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论