如何在`ggh4x::facet_wrap2()`中重新排列`geom_col`,仅基于其中一个图形。

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

How to reorder geom_col in ggh4x::facet_wrap2() based off only one of the graphs

问题

我尝试在facet_wrap中使用~fct_reorder(Method_Used, -total_count, .desc = TRUE)函数,但我收到以下错误消息:“Error in nrow * ncol : non-numeric argument to binary operator”。

以下是用于创建图形的代码:

  1. library(dplyr)
  2. library(ggplot2)
  3. library(stringr)
  4. library("ggh4x")
  5. library(forcats)
  6. cols = c("#b218d8", "#7AD151FF","#70EBE9", "#FDE725FF")
  7. Taxa_Plot = ggplot(
  8. data = df,
  9. aes(x = Method_Used, y = total_count,
  10. fill = factor(Min_group, levels=c('Small', 'Medium', 'Large', "Not_Reported"))) +
  11. scale_fill_manual(values = cols, breaks = c('Small', 'Medium', 'Large', "Not_Reported")) +
  12. geom_col(width=.5) +
  13. scale_y_continuous(expand = c(0,0)) +
  14. theme_classic() +
  15. facet_wrap2(~Water_Type, ncol=1, axes = "all", remove_labels = "all",
  16. ~fct_reorder(Method_Used, -total_count, .desc = TRUE)) +
  17. theme( axis.text.x = element_text(size = 15, angle = 45, hjust = 1),
  18. axis.text.y = element_text(size = 10),
  19. axis.title.x = element_text(
  20. size = 20,
  21. margin = margin(30, 0, 0, 0)
  22. ),
  23. axis.title.y = element_text(
  24. size = 20,
  25. margin = margin(0, 30, 0, 0)),
  26. panel.spacing = unit(1.5, "lines"),
  27. strip.background = element_blank(),
  28. strip.text.x = element_text(size = 15),
  29. plot.margin = margin(1,1,1.5,2, "cm")) +
  30. xlab("Method Used") +
  31. ylab("Total Number of publications") +
  32. labs(fill='Size of Individuals Tagged')
  33. Taxa_Plot

有没有办法让它们都根据底部图形重新排序?

英文:

I have created a facet_wrap graph and I am trying to reorder the bars from low to high. Obviously that order will be different for the 3 grids, but I want to make them go from low to high BASED off my bottom graph titled "Salt water". Here is my dataset

  1. df = structure(list(Method_Used = c("AnimalBorne_Acoustic", "AnimalBorne_Acoustic_Archival",
  2. "AnimalBorne_Archical", "AnimalBorne_Archical", "AnimalBorne_Archical",
  3. "AnimalBorne_Archical", "AnimalBorne_Archical", "AnimalBorne_Archical",
  4. "AnimalBorne_Archical", "AnimalBorne_Archical", "AnimalBorne_Archical",
  5. "AnimalBorne_Archical", "AnimalBorne_Archical", "AnimalBorne_Archival_PIT",
  6. "AnimalBorne_Archival_Satellite", "AnimalBorne_Satellite", "AnimalBorne_Satellite_Archival",
  7. "AnimalBorne_Satellite_Archival", "BRUV_Acoustic", "BRUV_Acoustic",
  8. "BRUV_Acoustic", "BRUV_Acoustic_Satellite", "BRUV_Acoustic_Satellite",
  9. "Controlled_Acoustic", "Controlled_Acoustic", "Controlled_Acoustic",
  10. "Controlled_Acoustic", "Controlled_Acoustic", "Controlled_Acoustic",
  11. "Controlled_Archical", "Controlled_Satellite", "Stationary_Acousitc_PIT",
  12. "Stationary_Acoustic", "Stationary_Acoustic", "Stationary_Acoustic",
  13. "Stationary_Acoustic", "Stationary_Acoustic", "Stationary_Acoustic",
  14. "Stationary_Acoustic", "Stationary_Acoustic", "Stationary_Acoustic",
  15. "Stationary_Acoustic_Archival", "Stationary_Acoustic_Archival",
  16. "Stationary_Acoustic_Archival", "Stationary_Acoustic_Archival",
  17. "Stationary_Acoustic_PIT", "Stationary_Acoustic_Radio", "Stationary_Acoustic_Radio_PIT",
  18. "Stationary_Archical", "Stationary_Archical", "Stationary_Archical",
  19. "Stationary_PIT", "Stationary_Radio", "Stationary_Radio", "Stationary_Radio_PIT",
  20. "Stationary_Satellite", "Stationary_Satellite"), Water_Type = c("Saltwater",
  21. "Saltwater", "Freshwater", "Saltwater", "Saltwater", "Saltwater",
  22. "Saltwater", "Saltwater", "Saltwater", "Saltwater", "Saltwater",
  23. "Saltwater", "Saltwater", "Saltwater", "Saltwater", "Saltwater",
  24. "Saltwater", "Saltwater", "Saltwater", "Saltwater", "Saltwater",
  25. "Saltwater", "Saltwater", "Freshwater", "Saltwater", "Saltwater",
  26. "Saltwater", "Saltwater", "Saltwater", "Saltwater", "Saltwater",
  27. "Freshwater", "Brackish", "Brackish", "Freshwater", "Freshwater",
  28. "Saltwater", "Saltwater", "Saltwater", "Saltwater", "Saltwater",
  29. "Saltwater", "Saltwater", "Saltwater", "Saltwater", "Freshwater",
  30. "Freshwater", "Freshwater", "Saltwater", "Saltwater", "Saltwater",
  31. "Freshwater", "Freshwater", "Freshwater", "Freshwater", "Saltwater",
  32. "Saltwater"), Taxa = c("Reptilia", "Chondrichthyes", "Phocidae",
  33. "Cetacea", "Cetacea", "Chondrichthyes", "Chondrichthyes", "Mollusca",
  34. "Phocidae", "Reptilia", "Reptilia", "Reptilia", "Teleostei",
  35. "Reptilia", "Reptilia", "Chondrichthyes", "Chondrichthyes", "Phocidae",
  36. "Chondrichthyes", "Teleostei", "Teleostei", "Chondrichthyes",
  37. "Chondrichthyes", "Chondrostei", "Chondrichthyes", "Teleostei",
  38. "Teleostei", "Teleostei", "Teleostei", "Cetacea", "Phocidae",
  39. "Teleostei", "Teleostei", "Teleostei", "Teleostei", "Teleostei",
  40. "Chondrichthyes", "Decapoda", "Teleostei", "Teleostei", "Teleostei",
  41. "Chondrichthyes", "Decapoda", "Gastropoda", "Teleostei", "Teleostei",
  42. "Teleostei", "Teleostei", "Gastropoda", "Reptilia", "Teleostei",
  43. "Teleostei", "Teleostei", "Teleostei", "Teleostei", "Cetacea",
  44. "Phocidae"), Min_group = c("Medium", "Large", "Not_Reported",
  45. "Large", "Not_Reported", "Large", "Not_Reported", "Medium", "Not_Reported",
  46. "Medium", "Not_Reported", "Small", "Large", "Not_Reported", "Large",
  47. "Large", "Large", "Not_Reported", "Medium", "Large", "Medium",
  48. "Large", "Medium", "Not_Reported", "Large", "Large", "Medium",
  49. "Not_Reported", "Small", "Large", "Not_Reported", "Not_Reported",
  50. "Not_Reported", "Small", "Medium", "Not_Reported", "Not_Reported",
  51. "Small", "Medium", "Not_Reported", "Small", "Large", "Small",
  52. "Not_Reported", "Medium", "Small", "Small", "Not_Reported", "Medium",
  53. "Not_Reported", "Medium", "Not_Reported", "Medium", "Not_Reported",
  54. "Medium", "Large", "Not_Reported"), total_count = c(1L, 1L, 1L,
  55. 2L, 5L, 5L, 1L, 1L, 2L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
  56. 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 3L,
  57. 1L, 1L, 1L, 5L, 4L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
  58. 1L, 1L, 1L, 1L, 1L, 1L)), class = c("tbl_df", "tbl", "data.frame"
  59. ), row.names = c(NA, -57L))

I tried using the function ~fct_reorder(Method_Used, -total_count, .desc = TRUE) within facet_wrapbut I get this error Error in nrow * ncol : non-numeric argument to binary operator

Here is the code I am using to make the graph

  1. library(dplyr)
  2. library(ggplot2)
  3. library(stringr)
  4. library("ggh4x")
  5. library(forcats)
  6. cols = c("#b218d8", "#7AD151FF","#70EBE9", "#FDE725FF")
  7. Taxa_Plot = ggplot(
  8. data = df,
  9. aes(x = Method_Used, y = total_count,
  10. fill = factor(Min_group, levels=c('Small', 'Medium', 'Large', "Not_Reported" )))) +
  11. scale_fill_manual(values = cols, breaks = c('Small', 'Medium', 'Large', "Not_Reported")) +
  12. geom_col(width=.5) +
  13. scale_y_continuous(expand = c(0,0)) +
  14. theme_classic() +
  15. #get multiple graphs, ncol puts them in vertical order instead of horizontal
  16. #and axes, remove labels keeps the x borders for all graphs
  17. facet_wrap2(~Water_Type, ncol=1, axes = "all", remove_labels = "all",
  18. ~fct_reorder(Method_Used, -total_count, .desc = TRUE)) +
  19. # increase the text size on x and y axis
  20. theme( axis.text.x = element_text(size = 15, angle = 45, hjust = 1),
  21. axis.text.y = element_text(size = 10),
  22. #change size of axis titles
  23. axis.title.x = element_text(
  24. size = 20,
  25. margin = margin(30, 0, 0, 0)
  26. ),
  27. axis.title.y = element_text(
  28. size = 20,
  29. margin = margin(0, 30, 0, 0)),
  30. #increase spacing between facet wrap plots
  31. panel.spacing = unit(1.5, "lines"),
  32. #get rid of border around panel
  33. strip.background = element_blank(),
  34. #increase text size of table
  35. strip.text.x = element_text(size = 15),
  36. #margin outside of graph, top, right, bottom, left
  37. plot.margin = margin(1,1,1.5,2, "cm")) +
  38. # change axis titles
  39. xlab("Method Used") +
  40. ylab("Total Number of publications") +
  41. labs(fill='Size of Individuals Tagged')
  42. Taxa_Plot

Any ideas of how to get them all to reorder based off JUST the bottom graph?

答案1

得分: 1

一种选择是在fct_reorder中使用一个辅助列,在其中将非"Saltwater"的值设为零,然后使用.fun=sum

  1. library(dplyr)
  2. library(ggplot2)
  3. library(stringr)
  4. library("ggh4x")
  5. library(forcats)
  6. cols <- c("#b218d8", "#7AD151FF", "#70EBE9", "#FDE725FF")
  7. df %>%
  8. mutate(Method_Used = fct_reorder(Method_Used,
  9. ifelse(!Water_Type %in% "Saltwater", 0, total_count),
  10. .fun = sum
  11. )) %>%
  12. ggplot(
  13. aes(
  14. x = Method_Used, y = total_count,
  15. fill = factor(Min_group, levels = c("Small", "Medium", "Large", "Not_Reported"))
  16. )
  17. ) +
  18. scale_fill_manual(values = cols, breaks = c("Small", "Medium", "Large", "Not_Reported")) +
  19. geom_col(width = .5) +
  20. scale_y_continuous(expand = c(0, 0)) +
  21. theme_classic() +
  22. facet_wrap2(~Water_Type, ncol = 1, axes = "all", remove_labels = "all") +
  23. theme(
  24. axis.text.x = element_text(size = 15, angle = 45, hjust = 1),
  25. axis.text.y = element_text(size = 10),
  26. axis.title.x = element_text(
  27. size = 20,
  28. margin = margin(30, 0, 0, 0)
  29. ),
  30. axis.title.y = element_text(
  31. size = 20,
  32. margin = margin(0, 30, 0, 0)
  33. ),
  34. panel.spacing = unit(1.5, "lines"),
  35. strip.background = element_blank(),
  36. strip.text.x = element_text(size = 15),
  37. plot.margin = margin(1, 1, 1.5, 2, "cm")
  38. ) +
  39. xlab("Method Used") +
  40. ylab("Total Number of publications") +
  41. labs(fill = "Size of Individuals Tagged")

如何在`ggh4x::facet_wrap2()`中重新排列`geom_col`,仅基于其中一个图形。

英文:

One option would be to use a helper column in fct_reorder where you set the values for non &quot;Saltwater&quot; to zero and use .fun=sum:

  1. library(dplyr)
  2. library(ggplot2)
  3. library(stringr)
  4. library(&quot;ggh4x&quot;)
  5. library(forcats)
  6. cols &lt;- c(&quot;#b218d8&quot;, &quot;#7AD151FF&quot;, &quot;#70EBE9&quot;, &quot;#FDE725FF&quot;)
  7. df |&gt;
  8. mutate(Method_Used = fct_reorder(Method_Used,
  9. ifelse(!Water_Type %in% &quot;Saltwater&quot;, 0, total_count),
  10. .fun = sum
  11. )) |&gt;
  12. ggplot(
  13. aes(
  14. x = Method_Used, y = total_count,
  15. fill = factor(Min_group, levels = c(&quot;Small&quot;, &quot;Medium&quot;, &quot;Large&quot;, &quot;Not_Reported&quot;))
  16. )
  17. ) +
  18. scale_fill_manual(values = cols, breaks = c(&quot;Small&quot;, &quot;Medium&quot;, &quot;Large&quot;, &quot;Not_Reported&quot;)) +
  19. geom_col(width = .5) +
  20. scale_y_continuous(expand = c(0, 0)) +
  21. theme_classic() +
  22. # get multiple graphs, ncol puts them in vertical order instead of horizontal
  23. # and axes, remove labels keeps the x borders for all graphs
  24. facet_wrap2(~Water_Type, ncol = 1, axes = &quot;all&quot;, remove_labels = &quot;all&quot;) +
  25. # increase the text size on x and y axis
  26. theme(
  27. axis.text.x = element_text(size = 15, angle = 45, hjust = 1),
  28. axis.text.y = element_text(size = 10),
  29. # change size of axis titles
  30. axis.title.x = element_text(
  31. size = 20,
  32. margin = margin(30, 0, 0, 0)
  33. ),
  34. axis.title.y = element_text(
  35. size = 20,
  36. margin = margin(0, 30, 0, 0)
  37. ),
  38. # increase spacing between facet wrap plots
  39. panel.spacing = unit(1.5, &quot;lines&quot;),
  40. # get rid of border around panel
  41. strip.background = element_blank(),
  42. # increase text size of table
  43. strip.text.x = element_text(size = 15),
  44. # margin outside of graph, top, right, bottom, left
  45. plot.margin = margin(1, 1, 1.5, 2, &quot;cm&quot;)
  46. ) +
  47. # change axis titles
  48. xlab(&quot;Method Used&quot;) +
  49. ylab(&quot;Total Number of publications&quot;) +
  50. labs(fill = &quot;Size of Individuals Tagged&quot;)

如何在`ggh4x::facet_wrap2()`中重新排列`geom_col`,仅基于其中一个图形。

huangapple
  • 本文由 发表于 2023年2月23日 23:32:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75546966.html
匿名

发表评论

匿名网友

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

确定