按降序对条形进行分组? [英] Grouped bars in descending order?

查看:52
本文介绍了按降序对条形进行分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  structure(list(Description = c("car","ball","cup","pen","pencil",计算机",椅子",桌子",枕头",床",床垫",剪刀",书",勺子",地毯",扬声器",框架",窗帘",阴影",信封",手机",字母",香",背包",盒子",鞋",真空",屏幕",双簧管",口罩",消毒剂",灯",瓶",伏特加",分支",雪"),尺寸= 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.88,2396.078),时间= c(下降",下降",下降",下降",下降",下降",冬天",冬天",冬天",冬天",冬天",冬天",冬天",冬天",冬天",冬天",冬天",冬天",冬天",冬天",冬天",冬天",冬天",冬天",冬天",冬天",冬天",冬天",冬天",冬天",冬天",冬天",冬天",冬天","winter","winter")),类="data.frame",row.names = c(NA,-36L)) 

下面的代码是我现在正在使用的代码.

  df $ Description<-factor(df $ Description,level = unique(df $ Description))df%>%安排(时间,desc(度量))%&%mutate(object = factor(Description,unique(Description)))%&%ggplot()+ aes(x =说明,y =度量,填充=时间)+geom_bar(stat ="identity")+theme_minimal()+主题(axis.text.x = element_text(角度= 45,高度= 1),plot.title = element_text(正值= 0.5))+scale_fill_manual(name ="Legend",值= c("navy","cornflowerblue")),标签= c("timea","time")) 

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

I want to organize my graph into two groups and in descending order within each group. I have a file that has variables:

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", "fall", "fall", "fall", "fall", 
"winter", "winter", "winter", "winter", "winter", "winter", "winter", 
"winter", "winter", "winter", "winter", "winter", "winter", "winter", 
"winter", "winter", "winter", "winter", "winter", "winter", "winter", 
"winter", "winter", "winter", "winter", "winter", "winter", "winter", 
"winter", "winter")), class = "data.frame", row.names = c(NA, 
-36L))

The following code is what I am using right now.

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

From this code I get the following graph.

enter image description here

解决方案

Try this :

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

这篇关于按降序对条形进行分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆