ggplot2: Shift the baseline of barplot (geom_bar) to the minimum data value

无人久伴 提交于 2020-01-07 09:15:29

问题


I'm trying to generate a bar plot using geom_bar. My bars have both negative and positive values:

set.seed(1)
df <- data.frame(y=log(c(runif(6,0,1),runif(6,1,10))),se=runif(12,0.05,0.1),name=factor(rep(c("a","a","b","b","c","c"),2),levels=c("a","b","c")),side=factor(rep(1:2,6),levels=1:2),group=factor(c(rep("x",6),rep("y",6)),levels=c("x","y")),stringsAsFactors=F)

This plot command plots the positive bars to face up and the negative ones to face down:

library(ggplot2)
dodge <- position_dodge(width=0.9)
limits <- aes(ymax=y+se,ymin=y-se)
ggplot(df,aes(x=name,y=y,group=interaction(side,name),col=group,fill=group))+facet_wrap(~group)+geom_bar(width=0.6,position=position_dodge(width=1),stat="identity")+
geom_bar(position=dodge,stat="identity")+geom_errorbar(limits,position=dodge,width=0.25)

My question is how do I set the base line to the minimum of all bars instead of at 0 and therefre have the red bars facing up?


回答1:


This simple hack also works:

m <- min(df$y) # find min
df$y <- df$y - m
ggplot(df,aes(x=name,y=y,group=interaction(side,name),col=group,fill=group))+
  facet_wrap(~group)+
  geom_bar(width=0.6,position=position_dodge(width=1),stat="identity")+
  geom_bar(position=dodge,stat="identity")+
  geom_errorbar(limits,position=dodge,width=0.25) +
  scale_y_continuous(breaks=seq(min(df$y), max(df$y), length=5),labels=as.character(round(seq(m, max(df$y+m), length=5),2))) # relabel




回答2:


You can subtract min(df$y) from each value so that the data are shifted to a baseline of zero, but then relabel the y-axis to the actual values of the points. The code to do it is below, but I wouldn't recommend this. It seems confusing to have bars emanating from a non-zero baseline, as the lengths of the bars no longer encode the magnitudes of the y values.

ggplot(df, aes(x=name,y=y - min(y),group=interaction(side, name), col=group, fill=group)) + 
  facet_wrap(~group) +
  geom_bar(position=dodge, stat="identity", width=0.8) +
  geom_errorbar(aes(ymin=y-se-min(y), ymax=y+se-min(y)), 
                position=dodge, width=0.25, colour="black") +
  scale_y_continuous(breaks=0:4, labels=round(0:4 + min(df$y), 1)) +
  geom_hline(aes(yintercept=0))

Another option is to use geom_linerange which avoids having to shift the y-values and relabel the y-axis. But this suffers from the same distortions as the bar plot above:

ggplot(df, aes(x=name, group=interaction(side, name), col=group, fill=group)) + 
  facet_wrap(~group) +
  geom_linerange(aes(ymin=min(y), ymax=y, x=name, xend=name), position=dodge, size=10) +
  geom_errorbar(aes(ymin=y-se, ymax=y+se), position=dodge, width=0.25, colour="black") +
  geom_hline(aes(yintercept=min(y)))

Instead, it seems to me points would be more intuitive and natural than bars here:

ggplot(df, aes(x=name,y=y,group=interaction(side, name), col=group, fill=group)) + 
  facet_wrap(~group) +
  geom_hline(yintercept=0, lwd=0.4, colour="grey50") +
  geom_errorbar(limits, position=dodge, width=0.25) +
  geom_point(position=dodge)




回答3:


I ran into the same problem and discovered you can also easily do this using geom_crossbar.

As long as color and fill are the same you don't see the break in the crossbar (set with y aesthetic) so they look exactly like bars.

library(ggplot2)

dodge <- position_dodge(width=0.9)
limits <- aes(ymax = y+se, ymin = y-se)
df$ymin <- min(df$y)

ggplot(df, aes(x = name, ymax = y, y = y, ymin = ymin, group = interaction(side,name), col = group, fill = group)) + 
facet_wrap(~group) + 
geom_crossbar(width=0.6,position=position_dodge(width=1),stat="identity") +
geom_errorbar(limits, color = 'black', position = dodge, width=0.25)

ggplot output



来源:https://stackoverflow.com/questions/40298209/ggplot2-shift-the-baseline-of-barplot-geom-bar-to-the-minimum-data-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!