如何使用 ggplot 订购这些条形图?为什么 forcat 方法不起作用?
How can I order these bars using ggplot? Why is the forcat method not working?
我正在尝试重新排列条形图上的条形,以便它们从大到小排列,按 log(Max_N) 而非字母顺序排列。我试过使用 forcats 但它会显示一条错误消息。到目前为止,这是我的代码,出了什么问题?我正在尝试自学如何使用 ggplot,所以请指出我犯的任何错误,因为我是自学成才的!
library("ggplot2")
library (forcats)
test<-ggplot(all, aes(x=all$Species, y=log(all$Max_N+1))) +
geom_bar(stat = "identity") +
coord_flip()
test <- test + labs(title = "",
subtitle = "",
caption = "",
y = "Log(MaxN)", x = "Species",
tag = "")
test %>%
mutate(name = fct_reorder(Species, log(Max_N+1)) %>%
Original plot
a) 这是请求的可重现示例(我希望这就是你的意思?)
structure(list(Site = c("Mylor", "Mylor", "Mylor", "Mylor", "Mylor",
"Mylor", "Mylor", "Mylor", "Mylor", "Mylor"), Trial = c(1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L), Location = c("centre", "edge",
"edge", "edge", "edge", "edge", "edge", "centre", "centre", "centre"
), Bait = c("whitefish", "whitefish", "whitefish", "whitefish",
"whitefish", "whitefish", "whitefish", "whitefish", "whitefish",
"whitefish"), ECP = c("yes", "yes", "yes", "yes", "yes", "yes",
"yes", "no", "no", "no"), SSP = c("no", "no", "no", "no", "no",
"no", "no", "no", "no", "no"), KP = c("no", "no", "no", "no",
"no", "no", "no", "no", "no", "no"), OSP = c("no", "no", "no",
"no", "no", "no", "no", "no", "no", "no"), Density =
c("dense_seagrass",
"dense_seagrass", "dense_seagrass", "dense_seagrass",
"dense_seagrass",
"dense_seagrass", "dense_seagrass", "dense_seagrass",
"dense_seagrass",
"dense_seagrass"), Viz = c(1.75, 1.75, 1.75, 1.75, 1.75, 1.75,
1.75, 4.5, 4.5, 4.5), Species = c("sea_sprat", "corkwing_wrasse",
"pollack", "sand_smelt", "unknown_wrasse", "two_spotted_goby",
"unknown_wrasse", "mackerel", "sand_smelt", "mullet"), AJ = c("juv",
"juv", "juv", "adu", "adu", "adu", "adu", "adu", "adu", "adu"
), FG = c("pelagic_neritic", "reef_associated", "pelagic_neritic",
"pelagic_neritic", "reef_associated", "reef_associated",
"reef_associated",
"pelagic_neritic", "pelagic_neritic", "pelagic_neritic"), Max_N =
c(1L,
1L, 2L, 2L, 2L, 2L, 1L, 1L, 26L, 1L)), row.names = c(NA, 10L), class
= "data.frame")
b) 所以我按照建议将 Species 更改为名称,它有一些命令但没有其他命令?将代码更改为此。
test<-ggplot(all2, aes(x=name, y=log(Max_N+1))) +
geom_bar(stat = "identity") +
coord_flip()
plot after changing code to above
实际上,如果我是你,我只会在 ggplot(aes())
中添加一个 reorder
,然后你不需要更改任何数据。
如果您想绘制此条形图总结一个物种内的所有 Max_N
,您可以在进入 ggplot
之前执行 group_by
和 summarise
。此 不会 更改您的原始 all
数据框。
library(tidyverse)
all %>% group_by(Species) %>% summarize(Max_N = sum(Max_N)) %>%
ggplot(all, aes(x = reorder(Species, Max_N), y =log(Max_N+1))) +
geom_bar(stat = "identity") +
coord_flip() + labs(x = "Species", y = "Log(MaxN)")
我正在尝试重新排列条形图上的条形,以便它们从大到小排列,按 log(Max_N) 而非字母顺序排列。我试过使用 forcats 但它会显示一条错误消息。到目前为止,这是我的代码,出了什么问题?我正在尝试自学如何使用 ggplot,所以请指出我犯的任何错误,因为我是自学成才的!
library("ggplot2")
library (forcats)
test<-ggplot(all, aes(x=all$Species, y=log(all$Max_N+1))) +
geom_bar(stat = "identity") +
coord_flip()
test <- test + labs(title = "",
subtitle = "",
caption = "",
y = "Log(MaxN)", x = "Species",
tag = "")
test %>%
mutate(name = fct_reorder(Species, log(Max_N+1)) %>%
Original plot
a) 这是请求的可重现示例(我希望这就是你的意思?)
structure(list(Site = c("Mylor", "Mylor", "Mylor", "Mylor", "Mylor",
"Mylor", "Mylor", "Mylor", "Mylor", "Mylor"), Trial = c(1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L), Location = c("centre", "edge",
"edge", "edge", "edge", "edge", "edge", "centre", "centre", "centre"
), Bait = c("whitefish", "whitefish", "whitefish", "whitefish",
"whitefish", "whitefish", "whitefish", "whitefish", "whitefish",
"whitefish"), ECP = c("yes", "yes", "yes", "yes", "yes", "yes",
"yes", "no", "no", "no"), SSP = c("no", "no", "no", "no", "no",
"no", "no", "no", "no", "no"), KP = c("no", "no", "no", "no",
"no", "no", "no", "no", "no", "no"), OSP = c("no", "no", "no",
"no", "no", "no", "no", "no", "no", "no"), Density =
c("dense_seagrass",
"dense_seagrass", "dense_seagrass", "dense_seagrass",
"dense_seagrass",
"dense_seagrass", "dense_seagrass", "dense_seagrass",
"dense_seagrass",
"dense_seagrass"), Viz = c(1.75, 1.75, 1.75, 1.75, 1.75, 1.75,
1.75, 4.5, 4.5, 4.5), Species = c("sea_sprat", "corkwing_wrasse",
"pollack", "sand_smelt", "unknown_wrasse", "two_spotted_goby",
"unknown_wrasse", "mackerel", "sand_smelt", "mullet"), AJ = c("juv",
"juv", "juv", "adu", "adu", "adu", "adu", "adu", "adu", "adu"
), FG = c("pelagic_neritic", "reef_associated", "pelagic_neritic",
"pelagic_neritic", "reef_associated", "reef_associated",
"reef_associated",
"pelagic_neritic", "pelagic_neritic", "pelagic_neritic"), Max_N =
c(1L,
1L, 2L, 2L, 2L, 2L, 1L, 1L, 26L, 1L)), row.names = c(NA, 10L), class
= "data.frame")
b) 所以我按照建议将 Species 更改为名称,它有一些命令但没有其他命令?将代码更改为此。
test<-ggplot(all2, aes(x=name, y=log(Max_N+1))) +
geom_bar(stat = "identity") +
coord_flip()
plot after changing code to above
实际上,如果我是你,我只会在 ggplot(aes())
中添加一个 reorder
,然后你不需要更改任何数据。
如果您想绘制此条形图总结一个物种内的所有 Max_N
,您可以在进入 ggplot
之前执行 group_by
和 summarise
。此 不会 更改您的原始 all
数据框。
library(tidyverse)
all %>% group_by(Species) %>% summarize(Max_N = sum(Max_N)) %>%
ggplot(all, aes(x = reorder(Species, Max_N), y =log(Max_N+1))) +
geom_bar(stat = "identity") +
coord_flip() + labs(x = "Species", y = "Log(MaxN)")