ggplot2-按重量值排序图 [英] ggplot2 - Ordering graph by weight value

查看:91
本文介绍了ggplot2-按重量值排序图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试生成条形图(使用ggplot2),其条形图的估算数据沿y轴,基本计数标度沿x轴。我的数据的结构如下:

 数据<-data.frame(locations = c( A, B , C, D ...),估计= c(200,300,400,200 ...)

然后我使用dplyr根据估算来排列数据

 库(dplyr)
数据<-数据%>%安排(估计)

然后我运行ggplot2代码

 库(ggplot2)
ggplot(数据,aes(位置,权重=估计值))+
geom_bar ()+
coord_flip()

但是结果图是这样的,条形图没有




I'm trying to generate a bar chart (using ggplot2) with location estimates data along the y axis and a basic count scale along the x axis. My data is structured like so:

  Data <- data.frame(locations = c("A","B","C","D"...), estimates = c(200, 300, 400, 200...)

I then used dplyr to arrange my data according to estimates

  library(dplyr)
  Data <- Data %>% arrange(estimates)

So then I run my ggplot2 code

  library(ggplot2)
  ggplot(Data, aes(locations, weight = estimates))+
         geom_bar()+
         coord_flip()

But the resulting plot is such, the bars have not been ordered according to estimates.

解决方案

There's no point using dplyr. All you have to do is to order estimates, extract corresponding locations and pass it to scale_x_discrete, for example: scale_x_discrete(limits = Data$locations[order(Data$estimates)])

library(ggplot2)

 ggplot(Data, aes(locations, weight = estimates))+
         geom_bar()+
         coord_flip() +
         scale_x_discrete(limits = Data$locations[order(Data$estimates)])

这篇关于ggplot2-按重量值排序图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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