gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:12:39 25 4
gpt4 key购买 nike

我想将条形图放置在 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/

25 4 0