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

标签 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

最佳答案

您只需将条形移动一半宽度即可:

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

关于r - 将条形图的条形与标签上的左侧对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35781278/

相关文章:

r - 如何使用facet和margin = TRUE更改ggplot中的strip.text标签

r - 在 geom_bar 中使用多个变量与 ggplot 在同一 X (R)

r - ggplot用geom_bar中的百分比替换计数

r - 将数据框列提供给 xyplot 面板函数

r - 转换列表输出以在 dplyr 管道中创建 data.frame

r - 在 R 中绘制具有置信区间的时间序列

r - 对齐单个ggrepel标签内的文本行

r - ggplot 分组 geom_bar - 将因子类别的标签添加到 x 轴标签

mysql - RMySQL 在 Fedora 28 上安装错误

javascript - R Shiny : conditionalPanel not working if used several widgets