堆叠的横跨x轴的barplot [英] Stacked barplot crossing the x-axis

查看:115
本文介绍了堆叠的横跨x轴的barplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友,
如何在x轴的两侧(最好在ggplot2中)创建堆叠的barplot?

示例:
http://s23.postimg.org/3lbgicb3f/Example.png



我四处搜寻,但一直未找到任何好的例子。
数据由两个位置(1和2)组成,其中包含5个不同类别(A,B,C,R和S)的值(权重)。 A,B和C应位于x轴的上方,而R和S应在下方进行绘制。请注意x轴两侧的正值。



示例数据:

 类型= c(A,B,C,R,S,A,B,C,R,S)
位置= c (1,1,1,1,1,2,2,2,2,2)
Value = c(2,6,5,3,2.5,6,3,2,4,1.5)
df = data.frame(Type,Location,Value)
df $ Location< - as.factor(df $ Location)

任何指针都会非常感谢,
Nordenskiold

解决方案

另一种非常类似于@BrodieG的方法,它不需要创建任何新的数据框。

  library(plyr)
库(ggplot2)
ggplot(df,aes(x = Location,fill = Type))+
geom_bar(subset =。(%c(A,B,C )),aes(y = Value))+
geom_bar(subset =。(%c(R,S)),aes(y = -Value))+
geom_hline(yintercept = 0,linetype = 2)+
scale_y_continuous(labels = abs)


Friends, How do I create a stacked barplot on both sides of the x-axis (preferably in ggplot2) ?

Example: http://s23.postimg.org/3lbgicb3f/Example.png

I've searched around, but haven't been able to find any good examples. The data consists of two locations (1 and 2), with values (weight) for 5 different categories (A, B, C, R and S). A, B and C should on top of the x-axis, while R and S should be plotted below. Note the positive values on both sides of the x-axis. Never mind the error-bars.

Example data:

Type=c("A","B","C","R","S","A","B","C","R","S")
Location=c(1,1,1,1,1,2,2,2,2,2)
Value=c(2,6,5,3,2.5,6,3,2,4,1.5)
df=data.frame(Type, Location, Value)
df$Location <- as.factor(df$Location)

Any pointers would be much appreciated, Nordenskiold

解决方案

Here's another approach very similar to @BrodieG which does not require the creation of any new dataframes.

library(plyr)
library(ggplot2)
ggplot(df, aes(x=Location, fill=Type))+
  geom_bar(subset=.(Type %in% c("A","B","C")), aes(y=Value))+
  geom_bar(subset=.(Type %in% c("R","S")), aes(y=-Value))+
  geom_hline(yintercept=0, linetype=2)+
  scale_y_continuous(labels=abs)

这篇关于堆叠的横跨x轴的barplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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