从高到低重新排序箱线图
Reorder a boxplot from high to low
我无法将箱线图从高到低重新排序。我已经尝试使用 fct_reorder
很多次了,但我想我没听懂
我的数据:
dat <- structure(list(Pesticide = c(
"Mancozeb", "Mancozeb", "Benomyl",
"DDT", "Glyphosate", "Carbofuran", "Carbofuran", "Aldicarb",
"Chlorsulfuron", "Neem", "Oxadiazon", "Oxyfluorfen", "Phorate",
"Phorate", "Fenvalerate", "Fenvalerate", "BHC", "BHC", "Diallate",
"Cycloate", "PCA", "Lenacyl", "Phenmedipham", "Aldrin"
), Change = c(
-11.2,
-5.6, 33.9, -40, 36.4, -5, -38, -94.6, -16, -49.5, 32.3, 37.5,
15.9, 22.2, 3.8, 17.6, 27.7, 28.2, 66.3, 33.5, 36, 10.3, 139.8,
18
)), class = "data.frame", row.names = c(NA, -24L))
我的代码:
library(ggplot2)
library(tidyverse)
library(ggExtra)
library(forcats)
theme_set(theme_bw())
dat<-read.delim("clipboard")
summary(dat)
q<-qplot (fct_reorder(Pesticide, Change,data=dat, geom=c("boxplot"),
fill=Pesticide, xlab="Pesticide", ylab="% Change in Nitrification"))
r<-q + theme(axis.text.x = element_text(angle = 45, hjust = 1))
r+geom_hline(yintercept =0,linetype="dashed",size=0.75,color="red")
也许这就是您要找的。我没有使用 qplot
,而是切换到 ggplot
并在将数据传递给 ggplot 之前进行重新排序。
根据@statstew 的评论,您可能想将 Change
映射到 y
。试试这个:
library(ggplot2)
library(dplyr)
library(ggExtra)
library(forcats)
theme_set(theme_bw())
dat %>%
mutate(Pesticide = fct_reorder(Pesticide, Change)) %>%
ggplot(aes(x = Pesticide, y = Change, fill = Pesticide)) +
geom_boxplot() +
geom_hline(yintercept =0,linetype="dashed",size=0.75,color="red") +
labs(x="Pesticide", y="% Change in Nitrification") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
我无法将箱线图从高到低重新排序。我已经尝试使用 fct_reorder
很多次了,但我想我没听懂
我的数据:
dat <- structure(list(Pesticide = c(
"Mancozeb", "Mancozeb", "Benomyl",
"DDT", "Glyphosate", "Carbofuran", "Carbofuran", "Aldicarb",
"Chlorsulfuron", "Neem", "Oxadiazon", "Oxyfluorfen", "Phorate",
"Phorate", "Fenvalerate", "Fenvalerate", "BHC", "BHC", "Diallate",
"Cycloate", "PCA", "Lenacyl", "Phenmedipham", "Aldrin"
), Change = c(
-11.2,
-5.6, 33.9, -40, 36.4, -5, -38, -94.6, -16, -49.5, 32.3, 37.5,
15.9, 22.2, 3.8, 17.6, 27.7, 28.2, 66.3, 33.5, 36, 10.3, 139.8,
18
)), class = "data.frame", row.names = c(NA, -24L))
我的代码:
library(ggplot2)
library(tidyverse)
library(ggExtra)
library(forcats)
theme_set(theme_bw())
dat<-read.delim("clipboard")
summary(dat)
q<-qplot (fct_reorder(Pesticide, Change,data=dat, geom=c("boxplot"),
fill=Pesticide, xlab="Pesticide", ylab="% Change in Nitrification"))
r<-q + theme(axis.text.x = element_text(angle = 45, hjust = 1))
r+geom_hline(yintercept =0,linetype="dashed",size=0.75,color="red")
也许这就是您要找的。我没有使用 qplot
,而是切换到 ggplot
并在将数据传递给 ggplot 之前进行重新排序。
根据@statstew 的评论,您可能想将 Change
映射到 y
。试试这个:
library(ggplot2)
library(dplyr)
library(ggExtra)
library(forcats)
theme_set(theme_bw())
dat %>%
mutate(Pesticide = fct_reorder(Pesticide, Change)) %>%
ggplot(aes(x = Pesticide, y = Change, fill = Pesticide)) +
geom_boxplot() +
geom_hline(yintercept =0,linetype="dashed",size=0.75,color="red") +
labs(x="Pesticide", y="% Change in Nitrification") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))