【问题标题】:Ordering of facets in ggplot2ggplot2中的刻面排序
【发布时间】:2020-12-11 08:51:32
【问题描述】:

我正在研究随着时间的推移从不同罐子中释放出的丙酮和乙醛。我的数据集由四列组成:days_incubated jar compound emission

我希望在各个方面可视化每个罐子的发射。随着时间的推移,每个方面都应显示罐子的排放物,其中丙酮和乙醛具有不同的颜色和形状。应根据 days_incubated = 0 时的乙醛排放量对方面进行分类。例如,第 0 天乙醛排放量较高的罐子应首先显示在图表中

非常感谢所有帮助!

我已经想出了这段代码,它带我走了一些路。

[![失败的情节][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")

这是数据:

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

【问题讨论】:

  • 这个答案没有帮助? stackoverflow.com/questions/65139760/…
  • 你的意思是每个 X 变量中的两个黑点 - 你不希望它们都在那里吗?
  • 很好,其他答案帮助了@Tiptop!现在这是一个需要解决的单独问题吗?如果另一个答案在那里回答了问题,请务必将其标记为“已接受”,然后这个答案可以专注于新问题是什么。
  • 它们是由stat_summary(fun = mean) 调用添加的。您的geom_point() 为数据集的每一行添加一个点(每个化合物为黑色和黄色),stat_summary() 为每个分组(按 jar 和 days_incubated)取这两个值的平均值并绘制一个新点。如果你不想要那个额外的 pont,你似乎不需要这条线?
  • (为了清楚起见,请在下面放置代码和图形示例 - 如果有帮助或是否被误解,请告诉我)

标签: r ggplot2


【解决方案1】:

尝试不带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")

reprex package (v0.3.0) 于 2020 年 12 月 11 日创建

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-16
    • 1970-01-01
    • 2020-04-07
    相关资源
    最近更新 更多