条形图绘制顺序取决于值(ggplot2)

时间:2014-08-04 08:30:16

标签: r ggplot2 bar-chart

考虑两个系列的值rr1rr2

rr1 <- c(-1, -0.6, 1.7, 6.3, 9, 10, 8.8, 6.2, 4.5, 4, 3.4)
rr2 <- c(-2.3, -1.8, -4, -5.7, -7.2, -5.6, -2.3, 0.2, -0.3, -1.4, -1.3)

图1: enter image description here

rr1主要是正数(除了x = -5和x = -4),而rr2是负数(x = 2除外)。

使用ggplot2rr1rr2分组到同一个条形图中(图2):

dat <- data.frame(
  group = rep(c("rr1", "rr2"), each=11),
  x = rep(-5:5, 2),
  y = c(rr1, rr2)
)
ggplot(dat, aes(x=x, y=y, fill=group)) + 
  geom_bar(stat="identity", position="identity", width=0.25) +
  scale_x_continuous(breaks=-5:5) +
  scale_y_continuous(breaks=seq(-10,10,2.5), limits=c(-10,10)) +
  theme(axis.text.x=element_text(size=14), axis.text.y=element_text(size=14), legend.text=element_text(size=14))

图2: enter image description here

当x等于-5和-4时,是否有办法将rr1绘制在rr2上?

1 个答案:

答案 0 :(得分:1)

问题是rr2值打印超过rr1,因此有手动解决方案 - 首先打印所有正rr1然后打印所有负rr2,负rr1和正rr2。

rr1 <- c(-1, -0.6, 1.7, 6.3, 9, 10, 8.8, 6.2, 4.5, 4, 3.4)
rr2 <- c(-2.3, -1.8, -4, -5.7, -7.2, -5.6, -2.3, 0.2, -0.3, -1.4, -1.3)

dat <- data.frame(
  group = rep(c("rr1", "rr2"), each=11),
  x = rep(-5:5, 2),
  y = c(rr1, rr2)
)

positive_rr1 <- subset(dat,group=="rr1" & y >=0)
negative_rr1 <- subset(dat,group=="rr1" & y <0)
positive_rr2 <- subset(dat,group=="rr2" & y >=0)
negative_rr2 <- subset(dat,group=="rr2" & y <0)

ggplot(dat, aes(x=x, y=y, fill=group)) + 
  geom_bar(data=positive_rr1,stat="identity", position="identity", width=0.25) +
  geom_bar(data=negative_rr2,stat="identity", position="identity", width=0.25) +
  geom_bar(data=negative_rr1,stat="identity", position="identity", width=0.25) +
  geom_bar(data=positive_rr2,stat="identity", position="identity", width=0.25) +
  scale_x_continuous(breaks=-5:5) +
  scale_y_continuous(breaks=seq(-10,10,2.5), limits=c(-10,10)) +
  theme(axis.text.x=element_text(size=14), axis.text.y=element_text(size=14), legend.text=element_text(size=14))

enter image description here

上面的图表不是堆积图表,因为rr1打印在rr2上,为了得到堆积图表,我们可以修改dat

for(i in -5:5){
  if(dat[dat$x==i & dat$group=="rr1","y"] < 0 & dat[dat$x==i & dat$group=="rr2","y"] < 0){
    dat[dat$x==i & dat$group=="rr2","y"] <- dat[dat$x==i & dat$group=="rr2","y"]+
      dat[dat$x==i & dat$group=="rr1","y"]
  }
  if(dat[dat$x==i & dat$group=="rr1","y"] > 0 & dat[dat$x==i & dat$group=="rr2","y"] > 0){
    dat[dat$x==i & dat$group=="rr1","y"] <- dat[dat$x==i & dat$group=="rr2","y"]+
      dat[dat$x==i & dat$group=="rr1","y"]
  }
}

positive_rr1 <- subset(dat,group=="rr1" & y >=0)
negative_rr1 <- subset(dat,group=="rr1" & y <0)
positive_rr2 <- subset(dat,group=="rr2" & y >=0)
negative_rr2 <- subset(dat,group=="rr2" & y <0)

ggplot(dat, aes(x=x, y=y, fill=group)) + 
  geom_bar(data=positive_rr1,stat="identity", position="identity", width=0.25) +
  geom_bar(data=negative_rr2,stat="identity", position="identity", width=0.25) +
  geom_bar(data=negative_rr1,stat="identity", position="identity", width=0.25) +
  geom_bar(data=positive_rr2,stat="identity", position="identity", width=0.25) +
  scale_x_continuous(breaks=-5:5) +
  scale_y_continuous(breaks=seq(-10,10,2.5), limits=c(-10,10)) +
  theme(axis.text.x=element_text(size=14), axis.text.y=element_text(size=14), legend.text=element_text(size=14))

enter image description here