按降序将条形分组?

问题描述

我想将我的图表分为两组,并在各组中按降序排列。 我有一个包含变量的文件:

structure(list(Description = c("car","ball","cup","pen","pencil","computer","chair","table","pillow","bed","mattress","scissors","book","spoon","carpet","speaker","frame","curtains","shades","envelope","cellphone","letter","incense","backpack","box","shoes","vacuum","screen","oboe","mask","sanitizer","lights","bottle","vodka","branch","snow"),Measures = c(3481.10542,3101.28475,26.62442,675.59209,408.602,1526.912,23.168,44.508,27.988,31.908,1659.382,15.732,5.662,17.72088,36.65081,1001.16575,33.54646,21.41968,61.98759,122.51581,4208.13103,3060.8029,2685.79742,84.9608,31.988,21.768,1712.338,25.508,291.5282,2987.739,97.258,73.8564,2047.538,501.518,1416.888,2396.078),Time = c("fall","fall","winter","winter")),class = "data.frame",row.names = c(NA,-36L))

以下代码是我现在正在使用的代码。

df$Description <- factor(df$Description,levels=unique(df$Description)) 

df %>% arrange(Time,desc(Measures)) %>% 
mutate(object = factor(Description,unique(Description))) %>%
ggplot() +aes(x=Description,y=Measures,fill=Time)+
geom_bar(stat="identity") +
theme_minimal()+
theme(axis.text.x = element_text(angle = 45,hjust = 1),plot.title = element_text(hjust = 0.5))+ 
scale_fill_manual(name="Legend",values = c("navy","cornflowerblue"),labels=c("timea","time"))

从这段代码中,我得到下图。

enter image description here

解决方法

尝试一下:

library(tidyverse)
df %>% 
  arrange(Time,desc(Measures)) %>% 
  mutate(Description = factor(Description,unique(Description))) %>%
  ggplot() +aes(x=Description,y=Measures,fill=Time)+
  geom_bar(stat="identity") +
  theme_minimal()+
  theme(axis.text.x = element_text(angle = 45,hjust = 1),plot.title = element_text(hjust = 0.5))+ 
  scale_fill_manual(name="Legend",values = c("navy","cornflowerblue"),labels=c("timea","time"))

enter image description here

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...