Grouped bars in descending order?

耗尽温柔 提交于 2021-01-05 07:43:18

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/64638958/grouped-bars-in-descending-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!