在多个因素下改变 x 轴顺序

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

Changing order in x axis with multiple factors

问题

我创建了一个图表,显示不同因子水平的均值,但对于分类因子(Pretreatment),我希望它按顺序显示为None-Mortar-Discs,而不是默认的字母顺序Discs-Motar-None。以下是代码和当前图表的图片:

  1. > dput(scenedesmus)
  2. structure(list(Temperature = structure(c(1L, 1L, 1L, 1L, 1L,
  3. 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("20",
  4. "30", "40"), class = "factor"), Time = structure(c(1L, 1L, 2L,
  5. 2L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L), .Label = c("0.5",
  6. "1", "2"), class = "factor"), Ratio = structure(c(1L, 1L, 2L,
  7. 2L, 3L, 3L, 2L, 2L, 3L, 3L, 1L, 1L, 3L, 3L, 1L, 1L, 2L, 2L), .Label = c("3",
  8. "6", "12"), class = "factor"), Pretreatment = structure(c(1L,
  9. 1L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 1L,
  10. 1L), .Label = c("None", "Mortar", "Discs"), class = "factor"),
  11. PRY = c(7.10618979550317, 6.99107348052751, 9.81654489395678,
  12. 10.0937678454159, 15.8872899104855, 16.5147395153748, 15.6085073784574,
  13. 15.8904572330355, 9.85155639002801, 10.3291566375677, 9.81557388225615,
  14. 10.1774212169006, 12.0972576247432, 11.1350551614397, 14.7591913822601,
  15. 14.8846506719242, 9.47697977090569, 10.8328555963545), CRY = c(12.9913707456184,
  16. 13.2037056981015, 14.6223886369729, 14.4156689100426, 20.8510599220091,
  17. 21.1334682925674, 20.7517385553227, 20.3784601114164, 13.1903022986714,
  18. 12.7481614338955, 14.3799945987187, 15.1548695641213, 16.3653561008515,
  19. 17.3492383422838, 22.4414097199122, 22.4340213280367, 14.0895227253865,
  20. 16.0388931794408), PCR = c(0.546993072143667, 0.529478135939726,
  21. 0.671336615218633, 0.700194205929921, 0.761941597689038,
  22. 0.781449560798454, 0.752154203217537, 0.779767320305684,
  23. 0.746878742196859, 0.810246770966047, 0.682585366418063,
  24. 0.671561122571146, 0.739199168670324, 0.641818098394623,
  25. 0.657676659642481, 0.663485625438106, 0.672626032522028,
  26. 0.675411668071984)), row.names = c(NA, -18L), class = c("tbl_df",
  27. "tbl", "data.frame"))
  1. scenedesmus %>%
  2. mutate(across(Temperature:Pretreatment, as.character)) %>%
  3. pivot_longer(Temperature:Pretreatment) %>%
  4. mutate(value = factor(value, levels(factor(value))[
  5. c(10:12, 6, 9, 3, 5, 7:8, 1:2, 4)])) %>%
  6. ggplot(aes(value, PCR, group = name, color = name)) +
  7. geom_point(stat = 'summary', fun = mean) +
  8. geom_hline(yintercept = mean(scenedesmus$PCR, na.rm=TRUE), linetype='dotted', col = 'grey', size=1.5) +
  9. stat_summary(fun.data="mean_sdl", fun.args = list(mult = 1), geom="errorbar", width = 0.2, size=1, alpha = 0.5) +
  10. scale_y_continuous(limits = c(0.5, 0.9)) +
  11. geom_line(stat = 'summary', fun = mean, size=1) +
  12. facet_grid(~name, scales = 'free_x', switch = 'x') +
  13. scale_color_manual(values = c("#0072B2", "#D55E00", "#CC79A7","#009E73")) +
  14. coord_cartesian(clip = 'off') +
  15. geom_vline(data = data.frame(a = 0.4, name = 'Pretreatment'),
  16. aes(xintercept = a)) +
  17. theme_classic(base_size = 20) +
  18. theme(strip.placement = 'outside',
  19. legend.position="none",
  20. strip.background = element_blank(),
  21. axis.title.x = element_blank(),
  22. panel.grid.major.x = element_blank(),
  23. panel.spacing.x = unit(0, 'mm'),
  24. axis.ticks = element_line(),
  25. axis.line.x = element_line(),
  26. axis.title.y = element_text(size=20, face="bold"),
  27. strip.text = element_text(face = "bold")) +
  28. labs(y = "Protein carbohydrate ratio")

在多个因素下改变 x 轴顺序

英文:

I created a plot with the mean values of different factor levels, but for the categorical factor (Pretreatment) I want it in the order None-Mortar-Discs, instead of the automatically alphabetical order Discs-Motar-None. Here is the code and a picture of the current plot:

  1. > dput(scenedesmus)
  2. structure(list(Temperature = structure(c(1L, 1L, 1L, 1L, 1L,
  3. 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("20",
  4. "30", "40"), class = "factor"), Time = structure(c(1L, 1L, 2L,
  5. 2L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L), .Label = c("0.5",
  6. "1", "2"), class = "factor"), Ratio = structure(c(1L, 1L, 2L,
  7. 2L, 3L, 3L, 2L, 2L, 3L, 3L, 1L, 1L, 3L, 3L, 1L, 1L, 2L, 2L), .Label = c("3",
  8. "6", "12"), class = "factor"), Pretreatment = structure(c(1L,
  9. 1L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 1L,
  10. 1L), .Label = c("None", "Mortar", "Discs"), class = "factor"),
  11. PRY = c(7.10618979550317, 6.99107348052751, 9.81654489395678,
  12. 10.0937678454159, 15.8872899104855, 16.5147395153748, 15.6085073784574,
  13. 15.8904572330355, 9.85155639002801, 10.3291566375677, 9.81557388225615,
  14. 10.1774212169006, 12.0972576247432, 11.1350551614397, 14.7591913822601,
  15. 14.8846506719242, 9.47697977090569, 10.8328555963545), CRY = c(12.9913707456184,
  16. 13.2037056981015, 14.6223886369729, 14.4156689100426, 20.8510599220091,
  17. 21.1334682925674, 20.7517385553227, 20.3784601114164, 13.1903022986714,
  18. 12.7481614338955, 14.3799945987187, 15.1548695641213, 16.3653561008515,
  19. 17.3492383422838, 22.4414097199122, 22.4340213280367, 14.0895227253865,
  20. 16.0388931794408), PCR = c(0.546993072143667, 0.529478135939726,
  21. 0.671336615218633, 0.700194205929921, 0.761941597689038,
  22. 0.781449560798454, 0.752154203217537, 0.779767320305684,
  23. 0.746878742196859, 0.810246770966047, 0.682585366418063,
  24. 0.671561122571146, 0.739199168670324, 0.641818098394623,
  25. 0.657676659642481, 0.663485625438106, 0.672626032522028,
  26. 0.675411668071984)), row.names = c(NA, -18L), class = c("tbl_df",
  27. "tbl", "data.frame"))
  1. scenedesmus %>%
  2. mutate(across(Temperature:Pretreatment, as.character)) %>%
  3. pivot_longer(Temperature:Pretreatment) %>%
  4. mutate(value = factor(value, levels(factor(value))[
  5. c(10:12, 6, 9, 3, 5, 7:8, 1:2, 4)])) %>%
  6. ggplot(aes(value, PCR, group = name, color = name)) +
  7. geom_point(stat = 'summary', fun = mean) +
  8. geom_hline(yintercept = mean(scenedesmus$PCR, na.rm=TRUE),linetype='dotted', col = 'grey', size=1.5) +
  9. stat_summary(fun.data="mean_sdl", fun.args = list(mult = 1), geom="errorbar", width = 0.2, size=1, alpha = 0.5) +
  10. scale_y_continuous(limits = c(0.5, 0.9)) +
  11. geom_line(stat = 'summary', fun = mean, size=1) +
  12. facet_grid(~name, scales = 'free_x', switch = 'x') +
  13. scale_color_manual(values = c("#0072B2", "#D55E00", "#CC79A7","#009E73")) +
  14. coord_cartesian(clip = 'off') +
  15. geom_vline(data = data.frame(a = 0.4, name = 'Pretreatment'),
  16. aes(xintercept = a)) +
  17. theme_classic(base_size = 20) +
  18. theme(strip.placement = 'outside',
  19. legend.position="none",
  20. strip.background = element_blank(),
  21. axis.title.x = element_blank(),
  22. panel.grid.major.x = element_blank(),
  23. panel.spacing.x = unit(0, 'mm'),
  24. axis.ticks = element_line(),
  25. axis.line.x = element_line(),
  26. axis.title.y = element_text(size=20, face="bold"),
  27. strip.text = element_text(face = "bold")) +
  28. labs(y = "Protein carbohydrate ratio")

在多个因素下改变 x 轴顺序

答案1

得分: 2

你已经在你的 mutate() 中对级别进行了排序,只需按照你实际想要的顺序重新排列它们。

  1. mutate(value = factor(value, levels(factor(value))[
  2. c(12:10, 6, 9, 3, 5, 7:8, 1:2, 4)])) %>%
  3. # ^ 而不是 10:12
英文:

You are already ordering the levels in your mutate(), just reorder them to the order you actually want

  1. mutate(value = factor(value, levels(factor(value))[
  2. c(12:10, 6, 9, 3, 5, 7:8, 1:2, 4)])) %>%
  3. # ^ rather than 10:12

huangapple
  • 本文由 发表于 2023年6月8日 23:12:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76433320.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定