R 将条形图的条形图对齐到标签的左侧

R 将条形图的条形图对齐到标签的左侧,r,ggplot2,geom-bar,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(s

我想在
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)
库(ggplot2)

df您只需将钢筋移动一半宽度即可:

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)