R:箱线图在两个方面以不同方式重新排序
R: boxplot reorder differently in two facets
我用不同的传感器(A1、B2、B3、...F21)对一个 parameter:Formaldehyde 的两个季节(夏季和冬季)进行了测量。
数据如下所示:
sensor=A1,A1,A2,A2,A3,A3
Formaldehyde=21.3,34.2,55,66.3,70.8,90
Season= summer,winter,summer,winter,summer,winter
我正在尝试将 geom_boxplot 分为两个方面(每个季节一个),我希望通过增加中位数(每个季节独立)来订购传感器
我试过:
fac <- with(DATALL, reorder(sensor,Formaldehyde, median, order = TRUE))
DATALL$sensors <- factor(DATALL$sensor, levels = levels(fac))
a <- ggplot(DATALL,aes(sensors,Formaldehyde, fill=sensors)) +
geom_jitter(position=position_jitter(width=0.3, height=0.2), aes(colour=factor(sensors)), alpha=0.4) +
geom_boxplot(outlier.shape = NA)+
facet_wrap(~season, scales='free', ncol=1)+
scale_y_continuous(limits = quantile(DATALL$Formaldehyde, c(0.1, 0.98)))
a<-a+labs(x="",y=expression(Formaldehyde~(30~min)~(µg/ m^{3})))+theme(legend.position = "none")
但这命令传感器考虑两个季节的中值,而不是独立地考虑每个方面。
你有什么建议吗?
我认为您可以使用此 post 中的一些代码:https://drsimonj.svbtle.com/ordering-categories-within-ggplot2-facets
DATALL <- data.frame(sensors=c("A1","A1","A2","A2","A3","A3","A1","A1","A2","A2","A3","A3","A1","A1","A2","A2","A3","A3"),
Formaldehyde=c(21.3,34.2,55,90,66.3,71.8,22.3,44.2,65,90,69.3,79.8,25.3,35.2,45,70,56.3,80.8),
season= c("summer", "winter"))
DATALL <- merge(
# merge your dataset...
DATALL %>%
# adding a column with season and sensor index
unite(index, c("season", "sensors")),
# ... to a column giving the order of sensors by season
DATALL %>%
group_by(season, sensors) %>%
summarise(Formaldehyde = mean(Formaldehyde, na.rm = T)) %>%
arrange(Formaldehyde) %>%
mutate(order = row_number()) %>% arrange(order) %>%
# create the common row index
unite(index, c("season", "sensors")) %>% select(index, order)) %>%
separate(index, c("season", "sensors"))
ggplot(DATALL,aes(factor(order, levels = min(order):max(order)),Formaldehyde, fill=sensors)) +
geom_jitter(position=position_jitter(width=0.3, height=0.2), aes(colour=factor(sensors)), alpha=0.4) +
geom_boxplot(outlier.shape = NA) +
facet_wrap(~season, scales='free', ncol=1) +
scale_x_discrete(
breaks = DATALL$order,
labels = DATALL$sensors,
expand = c(0,0))DATALL <- data.frame(sensors=c("A1","A1","A2","A2","A3","A3","A1","A1","A2","A2","A3","A3","A1","A1","A2","A2","A3","A3"),
Formaldehyde=c(21.3,34.2,55,90,66.3,71.8,22.3,44.2,65,90,69.3,79.8,25.3,35.2,45,70,56.3,80.8),
season= c("summer", "winter"))
DATALL <- merge(
# merge your dataset...
DATALL %>%
# adding a column with season and sensor index
unite(index, c("season", "sensors")),
# ... to a column giving the order of sensors by season
DATALL %>%
group_by(season, sensors) %>%
summarise(Formaldehyde = mean(Formaldehyde, na.rm = T)) %>%
ungroup() %>% arrange(season, Formaldehyde) %>%
mutate(order = row_number()) %>% arrange(order) %>%
# create the common row index
unite(index, c("season", "sensors")) %>% select(index, order)) %>%
separate(index, c("season", "sensors"))
ggplot(DATALL,aes(factor(order, levels = min(order):max(order)),Formaldehyde, fill=sensors)) +
geom_jitter(position=position_jitter(width=0.3, height=0.2), aes(colour=factor(sensors)), alpha=0.4) +
geom_boxplot(outlier.shape = NA) +
facet_wrap(~season, scales='free', ncol=1) +
scale_x_discrete(
breaks = DATALL$order,
labels = DATALL$sensors,
expand = c(0,0)) +
labs(x = "sensors")
非常感谢罗莎莉,
不幸的是,当我使用你的代码时,它在添加更多数据时效果不佳。如果我添加一些额外的数据:
DATALL <- data.frame(sensors=c("A1","A1","A2","A2","A3","A3","A1","A1","A2","A2","A3","A3","A1","A1","A2","A2","A3","A3"),
Formaldehyde=c(21.3,34.2,55,90,66.3,71.8,22.3,44.2,65,90,69.3,79.8,25.3,35.2,45,70,56.3,80.8),
season= c("summer", "winter"))
您的代码输出的图形如下所示:
enter image description here
我用不同的传感器(A1、B2、B3、...F21)对一个 parameter:Formaldehyde 的两个季节(夏季和冬季)进行了测量。 数据如下所示:
sensor=A1,A1,A2,A2,A3,A3
Formaldehyde=21.3,34.2,55,66.3,70.8,90
Season= summer,winter,summer,winter,summer,winter
我正在尝试将 geom_boxplot 分为两个方面(每个季节一个),我希望通过增加中位数(每个季节独立)来订购传感器 我试过:
fac <- with(DATALL, reorder(sensor,Formaldehyde, median, order = TRUE))
DATALL$sensors <- factor(DATALL$sensor, levels = levels(fac))
a <- ggplot(DATALL,aes(sensors,Formaldehyde, fill=sensors)) +
geom_jitter(position=position_jitter(width=0.3, height=0.2), aes(colour=factor(sensors)), alpha=0.4) +
geom_boxplot(outlier.shape = NA)+
facet_wrap(~season, scales='free', ncol=1)+
scale_y_continuous(limits = quantile(DATALL$Formaldehyde, c(0.1, 0.98)))
a<-a+labs(x="",y=expression(Formaldehyde~(30~min)~(µg/ m^{3})))+theme(legend.position = "none")
但这命令传感器考虑两个季节的中值,而不是独立地考虑每个方面。 你有什么建议吗?
我认为您可以使用此 post 中的一些代码:https://drsimonj.svbtle.com/ordering-categories-within-ggplot2-facets
DATALL <- data.frame(sensors=c("A1","A1","A2","A2","A3","A3","A1","A1","A2","A2","A3","A3","A1","A1","A2","A2","A3","A3"),
Formaldehyde=c(21.3,34.2,55,90,66.3,71.8,22.3,44.2,65,90,69.3,79.8,25.3,35.2,45,70,56.3,80.8),
season= c("summer", "winter"))
DATALL <- merge(
# merge your dataset...
DATALL %>%
# adding a column with season and sensor index
unite(index, c("season", "sensors")),
# ... to a column giving the order of sensors by season
DATALL %>%
group_by(season, sensors) %>%
summarise(Formaldehyde = mean(Formaldehyde, na.rm = T)) %>%
arrange(Formaldehyde) %>%
mutate(order = row_number()) %>% arrange(order) %>%
# create the common row index
unite(index, c("season", "sensors")) %>% select(index, order)) %>%
separate(index, c("season", "sensors"))
ggplot(DATALL,aes(factor(order, levels = min(order):max(order)),Formaldehyde, fill=sensors)) +
geom_jitter(position=position_jitter(width=0.3, height=0.2), aes(colour=factor(sensors)), alpha=0.4) +
geom_boxplot(outlier.shape = NA) +
facet_wrap(~season, scales='free', ncol=1) +
scale_x_discrete(
breaks = DATALL$order,
labels = DATALL$sensors,
expand = c(0,0))DATALL <- data.frame(sensors=c("A1","A1","A2","A2","A3","A3","A1","A1","A2","A2","A3","A3","A1","A1","A2","A2","A3","A3"),
Formaldehyde=c(21.3,34.2,55,90,66.3,71.8,22.3,44.2,65,90,69.3,79.8,25.3,35.2,45,70,56.3,80.8),
season= c("summer", "winter"))
DATALL <- merge(
# merge your dataset...
DATALL %>%
# adding a column with season and sensor index
unite(index, c("season", "sensors")),
# ... to a column giving the order of sensors by season
DATALL %>%
group_by(season, sensors) %>%
summarise(Formaldehyde = mean(Formaldehyde, na.rm = T)) %>%
ungroup() %>% arrange(season, Formaldehyde) %>%
mutate(order = row_number()) %>% arrange(order) %>%
# create the common row index
unite(index, c("season", "sensors")) %>% select(index, order)) %>%
separate(index, c("season", "sensors"))
ggplot(DATALL,aes(factor(order, levels = min(order):max(order)),Formaldehyde, fill=sensors)) +
geom_jitter(position=position_jitter(width=0.3, height=0.2), aes(colour=factor(sensors)), alpha=0.4) +
geom_boxplot(outlier.shape = NA) +
facet_wrap(~season, scales='free', ncol=1) +
scale_x_discrete(
breaks = DATALL$order,
labels = DATALL$sensors,
expand = c(0,0)) +
labs(x = "sensors")
非常感谢罗莎莉, 不幸的是,当我使用你的代码时,它在添加更多数据时效果不佳。如果我添加一些额外的数据:
DATALL <- data.frame(sensors=c("A1","A1","A2","A2","A3","A3","A1","A1","A2","A2","A3","A3","A1","A1","A2","A2","A3","A3"),
Formaldehyde=c(21.3,34.2,55,90,66.3,71.8,22.3,44.2,65,90,69.3,79.8,25.3,35.2,45,70,56.3,80.8),
season= c("summer", "winter"))
您的代码输出的图形如下所示: enter image description here