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

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

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”。

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

library(dplyr)
library(ggplot2)
library(stringr)
library("ggh4x")
library(forcats)

cols = c("#b218d8", "#7AD151FF","#70EBE9", "#FDE725FF")

Taxa_Plot = ggplot(
  data = df,
  aes(x = Method_Used, y = total_count, 
      fill = factor(Min_group, levels=c('Small', 'Medium', 'Large', "Not_Reported"))) +
  scale_fill_manual(values = cols, breaks = c('Small', 'Medium', 'Large', "Not_Reported")) +
  geom_col(width=.5) +
  scale_y_continuous(expand = c(0,0)) +
  theme_classic() +
  facet_wrap2(~Water_Type, ncol=1, axes = "all", remove_labels = "all",
              ~fct_reorder(Method_Used, -total_count, .desc = TRUE)) +
  theme( axis.text.x = element_text(size = 15, angle = 45, hjust = 1),
        axis.text.y = element_text(size = 10),
        axis.title.x = element_text(
          size = 20, 
          margin = margin(30, 0, 0, 0)
        ),
        axis.title.y = element_text(
          size = 20, 
          margin = margin(0, 30, 0, 0)),
        panel.spacing = unit(1.5, "lines"),
        strip.background = element_blank(),
        strip.text.x = element_text(size = 15),
        plot.margin = margin(1,1,1.5,2, "cm")) +
  xlab("Method Used") +
  ylab("Total Number of publications") +
  labs(fill='Size of Individuals Tagged')
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

df = structure(list(Method_Used = c("AnimalBorne_Acoustic", "AnimalBorne_Acoustic_Archival", 
"AnimalBorne_Archical", "AnimalBorne_Archical", "AnimalBorne_Archical", 
"AnimalBorne_Archical", "AnimalBorne_Archical", "AnimalBorne_Archical", 
"AnimalBorne_Archical", "AnimalBorne_Archical", "AnimalBorne_Archical", 
"AnimalBorne_Archical", "AnimalBorne_Archical", "AnimalBorne_Archival_PIT", 
"AnimalBorne_Archival_Satellite", "AnimalBorne_Satellite", "AnimalBorne_Satellite_Archival", 
"AnimalBorne_Satellite_Archival", "BRUV_Acoustic", "BRUV_Acoustic", 
"BRUV_Acoustic", "BRUV_Acoustic_Satellite", "BRUV_Acoustic_Satellite", 
"Controlled_Acoustic", "Controlled_Acoustic", "Controlled_Acoustic", 
"Controlled_Acoustic", "Controlled_Acoustic", "Controlled_Acoustic", 
"Controlled_Archical", "Controlled_Satellite", "Stationary_Acousitc_PIT", 
"Stationary_Acoustic", "Stationary_Acoustic", "Stationary_Acoustic", 
"Stationary_Acoustic", "Stationary_Acoustic", "Stationary_Acoustic", 
"Stationary_Acoustic", "Stationary_Acoustic", "Stationary_Acoustic", 
"Stationary_Acoustic_Archival", "Stationary_Acoustic_Archival", 
"Stationary_Acoustic_Archival", "Stationary_Acoustic_Archival", 
"Stationary_Acoustic_PIT", "Stationary_Acoustic_Radio", "Stationary_Acoustic_Radio_PIT", 
"Stationary_Archical", "Stationary_Archical", "Stationary_Archical", 
"Stationary_PIT", "Stationary_Radio", "Stationary_Radio", "Stationary_Radio_PIT", 
"Stationary_Satellite", "Stationary_Satellite"), Water_Type = c("Saltwater", 
"Saltwater", "Freshwater", "Saltwater", "Saltwater", "Saltwater", 
"Saltwater", "Saltwater", "Saltwater", "Saltwater", "Saltwater", 
"Saltwater", "Saltwater", "Saltwater", "Saltwater", "Saltwater", 
"Saltwater", "Saltwater", "Saltwater", "Saltwater", "Saltwater", 
"Saltwater", "Saltwater", "Freshwater", "Saltwater", "Saltwater", 
"Saltwater", "Saltwater", "Saltwater", "Saltwater", "Saltwater", 
"Freshwater", "Brackish", "Brackish", "Freshwater", "Freshwater", 
"Saltwater", "Saltwater", "Saltwater", "Saltwater", "Saltwater", 
"Saltwater", "Saltwater", "Saltwater", "Saltwater", "Freshwater", 
"Freshwater", "Freshwater", "Saltwater", "Saltwater", "Saltwater", 
"Freshwater", "Freshwater", "Freshwater", "Freshwater", "Saltwater", 
"Saltwater"), Taxa = c("Reptilia", "Chondrichthyes", "Phocidae", 
"Cetacea", "Cetacea", "Chondrichthyes", "Chondrichthyes", "Mollusca", 
"Phocidae", "Reptilia", "Reptilia", "Reptilia", "Teleostei", 
"Reptilia", "Reptilia", "Chondrichthyes", "Chondrichthyes", "Phocidae", 
"Chondrichthyes", "Teleostei", "Teleostei", "Chondrichthyes", 
"Chondrichthyes", "Chondrostei", "Chondrichthyes", "Teleostei", 
"Teleostei", "Teleostei", "Teleostei", "Cetacea", "Phocidae", 
"Teleostei", "Teleostei", "Teleostei", "Teleostei", "Teleostei", 
"Chondrichthyes", "Decapoda", "Teleostei", "Teleostei", "Teleostei", 
"Chondrichthyes", "Decapoda", "Gastropoda", "Teleostei", "Teleostei", 
"Teleostei", "Teleostei", "Gastropoda", "Reptilia", "Teleostei", 
"Teleostei", "Teleostei", "Teleostei", "Teleostei", "Cetacea", 
"Phocidae"), Min_group = c("Medium", "Large", "Not_Reported", 
"Large", "Not_Reported", "Large", "Not_Reported", "Medium", "Not_Reported", 
"Medium", "Not_Reported", "Small", "Large", "Not_Reported", "Large", 
"Large", "Large", "Not_Reported", "Medium", "Large", "Medium", 
"Large", "Medium", "Not_Reported", "Large", "Large", "Medium", 
"Not_Reported", "Small", "Large", "Not_Reported", "Not_Reported", 
"Not_Reported", "Small", "Medium", "Not_Reported", "Not_Reported", 
"Small", "Medium", "Not_Reported", "Small", "Large", "Small", 
"Not_Reported", "Medium", "Small", "Small", "Not_Reported", "Medium", 
"Not_Reported", "Medium", "Not_Reported", "Medium", "Not_Reported", 
"Medium", "Large", "Not_Reported"), total_count = c(1L, 1L, 1L, 
2L, 5L, 5L, 1L, 1L, 2L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 
1L, 1L, 1L, 5L, 4L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L)), class = c("tbl_df", "tbl", "data.frame"
), 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

library(dplyr)
library(ggplot2)
library(stringr)
library("ggh4x")
library(forcats)
cols = c("#b218d8", "#7AD151FF","#70EBE9", "#FDE725FF")
Taxa_Plot = ggplot(
data = df,
aes(x = Method_Used, y = total_count, 
fill = factor(Min_group, levels=c('Small', 'Medium', 'Large', "Not_Reported" )))) +
scale_fill_manual(values = cols, breaks = c('Small', 'Medium', 'Large', "Not_Reported")) +
geom_col(width=.5) +
scale_y_continuous(expand = c(0,0)) +
theme_classic() +
#get multiple graphs, ncol puts them in vertical order instead of horizontal
#and axes, remove labels keeps the x borders for all graphs
facet_wrap2(~Water_Type, ncol=1, axes = "all", remove_labels = "all",
~fct_reorder(Method_Used, -total_count, .desc = TRUE)) +
# increase the text size on x and y axis
theme( axis.text.x = element_text(size = 15, angle = 45, hjust = 1),
axis.text.y = element_text(size = 10),
#change size of axis titles
axis.title.x = element_text(
size = 20, 
margin = margin(30, 0, 0, 0)
),
axis.title.y = element_text(
size = 20, 
margin = margin(0, 30, 0, 0)),
#increase spacing between facet wrap plots
panel.spacing = unit(1.5, "lines"),
#get rid of border around panel
strip.background = element_blank(),
#increase text size of table
strip.text.x = element_text(size = 15),
#margin outside of graph, top, right, bottom, left
plot.margin = margin(1,1,1.5,2, "cm")) +
# change axis titles
xlab("Method Used") +
ylab("Total Number of publications") +
labs(fill='Size of Individuals Tagged')
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

library(dplyr)
library(ggplot2)
library(stringr)
library("ggh4x")
library(forcats)

cols <- c("#b218d8", "#7AD151FF", "#70EBE9", "#FDE725FF")

df %>%
  mutate(Method_Used = fct_reorder(Method_Used,
    ifelse(!Water_Type %in% "Saltwater", 0, total_count),
    .fun = sum
  )) %>%
  ggplot(
    aes(
      x = Method_Used, y = total_count,
      fill = factor(Min_group, levels = c("Small", "Medium", "Large", "Not_Reported"))
    )
  ) +
  scale_fill_manual(values = cols, breaks = c("Small", "Medium", "Large", "Not_Reported")) +
  geom_col(width = .5) +
  scale_y_continuous(expand = c(0, 0)) +
  theme_classic() +
  facet_wrap2(~Water_Type, ncol = 1, axes = "all", remove_labels = "all") +
  theme(
    axis.text.x = element_text(size = 15, angle = 45, hjust = 1),
    axis.text.y = element_text(size = 10),
    axis.title.x = element_text(
      size = 20,
      margin = margin(30, 0, 0, 0)
    ),
    axis.title.y = element_text(
      size = 20,
      margin = margin(0, 30, 0, 0)
    ),
    panel.spacing = unit(1.5, "lines"),
    strip.background = element_blank(),
    strip.text.x = element_text(size = 15),
    plot.margin = margin(1, 1, 1.5, 2, "cm")
  ) +
  xlab("Method Used") +
  ylab("Total Number of publications") +
  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:

library(dplyr)
library(ggplot2)
library(stringr)
library(&quot;ggh4x&quot;)
library(forcats)
cols &lt;- c(&quot;#b218d8&quot;, &quot;#7AD151FF&quot;, &quot;#70EBE9&quot;, &quot;#FDE725FF&quot;)
df |&gt;
mutate(Method_Used = fct_reorder(Method_Used,
ifelse(!Water_Type %in% &quot;Saltwater&quot;, 0, total_count),
.fun = sum
)) |&gt;
ggplot(
aes(
x = Method_Used, y = total_count,
fill = factor(Min_group, levels = c(&quot;Small&quot;, &quot;Medium&quot;, &quot;Large&quot;, &quot;Not_Reported&quot;))
)
) +
scale_fill_manual(values = cols, breaks = c(&quot;Small&quot;, &quot;Medium&quot;, &quot;Large&quot;, &quot;Not_Reported&quot;)) +
geom_col(width = .5) +
scale_y_continuous(expand = c(0, 0)) +
theme_classic() +
# get multiple graphs, ncol puts them in vertical order instead of horizontal
# and axes, remove labels keeps the x borders for all graphs
facet_wrap2(~Water_Type, ncol = 1, axes = &quot;all&quot;, remove_labels = &quot;all&quot;) +
# increase the text size on x and y axis
theme(
axis.text.x = element_text(size = 15, angle = 45, hjust = 1),
axis.text.y = element_text(size = 10),
# change size of axis titles
axis.title.x = element_text(
size = 20,
margin = margin(30, 0, 0, 0)
),
axis.title.y = element_text(
size = 20,
margin = margin(0, 30, 0, 0)
),
# increase spacing between facet wrap plots
panel.spacing = unit(1.5, &quot;lines&quot;),
# get rid of border around panel
strip.background = element_blank(),
# increase text size of table
strip.text.x = element_text(size = 15),
# margin outside of graph, top, right, bottom, left
plot.margin = margin(1, 1, 1.5, 2, &quot;cm&quot;)
) +
# change axis titles
xlab(&quot;Method Used&quot;) +
ylab(&quot;Total Number of publications&quot;) +
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:

确定