2016-03-03 33 views
0

我想杆定位在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

回答

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

+0

不错的解决方案! – Jaap