将条形图的条形对齐标签上的左侧

时间:2016-03-03 19:29:10

标签: r ggplot2 geom-bar

我想将条形放在geom_bar中,以便x轴标签位于条形图的左侧而不是中心

library(ggplot2)

df <- data.frame(
      x = c(1,1,4,4,8,8),
      y = c("A","B","A","B","A","B"), 
      z = c(10,5,20,2,8,4),
      a = c(1,1,4,4,2,2)
)

ggplot(df,aes(x=x,y=z,fill=y,width=a)) + 
  geom_bar(stat="identity", position="identity", alpha=.6) +    
  scale_x_continuous(breaks=0:10)

enter image description here

1 个答案:

答案 0 :(得分:3)

你可以将条移动宽度的一半:

ggplot(df, aes(x = x + a/2, y = z, fill = y, width = a)) + 
  geom_bar(stat = "identity", position = "identity", alpha = .6) +    
  scale_x_continuous(name = "x", breaks = 0:10)

enter image description here