繁体   English   中英

ggplot2:更改条形的顺序,以使值较小的条位于值较大的条的顶部

[英]ggplot2: change the order of bars so that bars with smaller values are on top of bars with larger values

这是我的数据和图表的示例:

m <- c("jun","jun","jul","aug","aug")
a <- c(1,10,2,10,2)
b <- c("x","y","x","x","y")

df <- data.frame(m,a,b)

ggplot(df,
       aes(x=m, y=a, fill=b)) + 
  geom_bar(stat="identity", position = "identity") 

在此处输入图片说明

我希望将x的值显示在jun栏上。 希望这些条形图不能彼此并排放置。

我最终使用了透明性。 但是我仍然很好奇他们是否是另一种方式。

我会做什么:

ggplot(df, aes(x = m, y = a, fill = b)) + 
  geom_bar(stat = "identity", position = position_dodge(preserve = "single")) 

在此处输入图片说明

确保您具有最新版本的ggplot2devtools::install_github("tidyverse/ggplot2")

使用透明度可以得到我想要的:

ggplot(df,
       aes(x=m, y=a, fill=b)) + 
  geom_bar(stat="identity", position = "identity", alpha=0.5) 

暂无
暂无

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

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