简体   繁体   English

ggplot2 中的 reorder() 图问题,带有分组变量

[英]Problem with reorder() graph in ggplot2, with grouped variable

I have a set of data with several values for different regions and different programs, when I try to order my graph from higher to lower accumulated values it works perfectly.我有一组数据,其中包含不同区域和不同程序的多个值,当我尝试将我的图表从较高的累积值排序到较低的累积值时,它可以完美地工作。

datos = read.csv("https://raw.githubusercontent.com/jaimeyanez/varios/master/datos.csv")

datos %>% 
  group_by(Region) %>% 
  summarise(value= sum(Monto)) %>% 
  ggplot() +
  geom_col(aes(x=reorder(Region,-value), y=value), alpha=.9) +
  labs(title = "Value per group") 

Graph without classification无分类图

But if i try to do the same and add a fill aesthetic, it fails但是如果我尝试做同样的事情并添加填充美感,它会失败

datos %>% 
  group_by(Region, Programa) %>% 
  summarise(value= sum(Monto)) %>% 
  ggplot() +
  geom_col(aes(x=reorder(Region,-value), y=value, fill=Programa), alpha=.9) +
  labs(title = "Value per group") 

Graph with classification带分类的图

I can change the order manually, but this data is an example and I have to do this whit different grouped variables, so i need to do this in an automatic way.我可以手动更改顺序,但此数据是一个示例,我必须使用不同的分组变量来执行此操作,因此我需要以自动方式执行此操作。

Thanks,谢谢,

reorder uses the mean value by default, but your bars show the sum. reorder默认使用平均值,但您的条形图显示总和。 Change to x = reorder(Region, -value, FUN = sum)更改为x = reorder(Region, -value, FUN = sum)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM