0

我坚持将我的箱线图从高到低重新排序。我试过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")
4

1 回答 1

0

也许这就是你要找的。在将数据传递给ggplot之前,我没有使用qplot我切换到并进行重新排序。ggplot

在@statstew 的评论之后,您可能想要映射Changey. 尝试这个:

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))

于 2020-08-16T08:42:13.900 回答