Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在rstudio中重新排序箱线图_R_Ggplot2_Boxplot - Fatal编程技术网

在rstudio中重新排序箱线图

在rstudio中重新排序箱线图,r,ggplot2,boxplot,R,Ggplot2,Boxplot,我正在尝试根据中间值对箱线图重新排序 mymonths <- c("Jan","Feb","Mar", "Apr","Mei","Jun", "Jul","Aug","Sep", "Okt","Nov

我正在尝试根据中间值对箱线图重新排序

mymonths <- c("Jan","Feb","Mar",
              "Apr","Mei","Jun",
              "Jul","Aug","Sep",
              "Okt","Nov","Dec")
df1 <- nycflights13::flights %>% 
  filter(dep_delay > 10) %>%
  select(month, arr_delay) %>% 
  filter(arr_delay < 480) %>%
  mutate(Maand  = mymonths[month])

ggplot(data = df1, mapping = aes(x = arr_delay, y = Maand)) +
  geom_boxplot(outlier.colour = 'red', outlier.alpha = 0.1) +
  labs(x = "Vertraging bij aankomst", caption = "Vluchten die uit New York vertrekken") +
  ggtitle("Vertraging van vluchten per maand (in min)")
mymonths 10)%
选择(月、到货延迟)%>%
滤波器(arr_延迟<480)%>%
突变(Maand=mymonths[月])
ggplot(数据=df1,映射=aes(x=arr_延迟,y=Maand))+
geom_箱线图(outlier.color='red',outlier.alpha=0.1)+
实验室(x=“Vertraining bij aankomst”,caption=“Vluchten die uit New York vertrekken”)+
ggtitle(“每平方米(分钟)”)
现在我想我可以使用函数reorder()或fct_reorder(), 我会在x的aes()中填入类似于
fct\u reorder(Maand,arr\u delay,fun=median)的内容。
(尝试切换“Maand”和“arr_delay”,但也不起作用)
没有重新排序的箱线图看起来很完美,但一旦我使用了重新排序,它就给了我一些非常奇怪的东西

任何帮助都会很好

试试看:

library(ggplot2)
ggplot(data = df1, mapping = aes(x = arr_delay, 
       y = forcats::fct_reorder(Maand, arr_delay))) +
  geom_boxplot(outlier.colour = 'red', outlier.alpha = 0.1) +
  labs(x = "Vertraging bij aankomst", 
       caption = "Vluchten die uit New York vertrekken") +
  ggtitle("Vertraging van vluchten per maand (in min)")
如果你想让条形图按相反的顺序排列,请使用
y=forcats::fct\u reorder(Maand,-arr\u delay)
我试过
fct\u reorder(Maand,arr\u delay,.fun=median)
,效果不错

ggplot(data = df1, mapping = aes(x = arr_delay, y = fct_reorder(Maand, arr_delay, .fun = median))) +
  geom_boxplot(outlier.colour = 'red', outlier.alpha = 0.1) +
  labs(x = "Vertraging bij aankomst", caption = "Vluchten die uit New York vertrekken") +
  ggtitle("Vertraging van vluchten per maand (in min)")

我自己找到了答案<代码>ggplot(数据=df1,映射=aes(x=重新排序(月,延迟,中位数),y=延迟))+geom箱线图(outlier.color='red',outlier.alpha=0.1)+实验室(y=“Vertraining bij aankomst”,caption=“Vluchten die uit New York vertrekken”)+ggtitle(“Vertraining van Vluchten per maand(min)”+coord_flip()