[英]Ordering of facets in ggplot2
I am looking at the emission of acetone and acetaldehyde from different jars over time.我正在研究不同 jars 随时间推移释放的丙酮和乙醛。 My data set consists of four columns:
days_incubated jar compound emission
.我的数据集由四列组成:
days_incubated jar compound emission
。
I wish to visualize the emission from each jar in facets.我希望在各个方面可视化每个 jar 的发射。 Each facet should show the emission from a jar over time, where acetone and acetaldehyde has different color and shape.
每个方面都应显示 jar 随时间的排放,其中丙酮和乙醛具有不同的颜色和形状。 The facets should be sorted according to the emission of acetaldehyde at days_incubated =0.
应根据 days_incubated = 0 时的乙醛排放量对刻面进行分类。 Eg jars with higher emission of acetaldehyde at day 0 should be shown first in the graph
例如,jars 在第 0 天的乙醛排放量较高,应首先显示在图表中
All help is much appreciated!非常感谢所有帮助!
I have come up with this code, that takes me some of the way.我想出了这段代码,这让我有了一些帮助。
[![failed plot][1]][1] [![失败的情节][1]][1]
library(ggplot2)
df %>%
group_by(days_incubated)%>%
mutate(emission_rate_day0 = ifelse(days_incubated == 0, y = emission, NA),
jar = fct_reorder(factor(jar),
emission_rate_day0,
mean,
na.rm = TRUE,
.desc = TRUE)) %>%
ggplot(aes(x=days_incubated, y=emission))+
stat_summary(fun = mean) +
geom_point(aes(shape=compound, color=compound)) +
scale_shape_manual(values=c(16, 17))+
scale_color_manual(values=c("black", "yellow")) +
labs(color = "Compound", shape = "Compound") +
theme_bw() +
facet_wrap(vars(jar), scales = "free_y")
and here's the data:这是数据:
df <- structure(list(days_incubated = c(0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L), jar = c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 1L, 1L,
2L, 2L, 3L, 3L, 4L, 4L, 1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L), compound = c("Acetone",
"Acetaldehyde", "Acetone", "Acetaldehyde", "Acetone", "Acetaldehyde",
"Acetone", "Acetaldehyde", "Acetone", "Acetaldehyde", "Acetone",
"Acetaldehyde", "Acetone", "Acetaldehyde", "Acetone", "Acetaldehyde",
"Acetone", "Acetaldehyde", "Acetone", "Acetaldehyde", "Acetone",
"Acetaldehyde", "Acetone", "Acetaldehyde"), emission = c(2.27,
2.48, 0.15, 0.13, 7, 3.13, 0.33, 0.33, 6.26, 0.92, 0.11, 0.01,
14.04, 10.38, 0.64, 0.98, 9.1, 0.32, 0.83, 0.02, 6.27, 0.51,
0.77, 11.18)), row.names = c(NA, -24L), class = c("tbl_df", "tbl",
"data.frame"))
[1]: https://i.stack.imgur.com/T2aUD.png
Try it without the stat_summary()
part, is this the graph you're trying to produce?:尝试不使用
stat_summary()
部分,这是您要生成的图表吗?:
library(ggplot2)
library(dplyr)
library(forcats)
df %>%
group_by(days_incubated)%>%
mutate(emission_rate_day0 = ifelse(days_incubated == 0, y = emission, NA),
jar = fct_reorder(factor(jar),
emission_rate_day0,
mean,
na.rm = TRUE,
.desc = TRUE)) %>%
ggplot(aes(x=days_incubated, y=emission))+
geom_point(aes(shape=compound, color=compound)) +
scale_shape_manual(values=c(16, 17))+
scale_color_manual(values=c("black", "yellow")) +
labs(color = "Compound", shape = "Compound") +
theme_bw() +
facet_wrap(vars(jar), scales = "free_y")
Created on 2020-12-11 by the reprex package (v0.3.0)由代表 package (v0.3.0) 于 2020 年 12 月 11 日创建
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.