假设我有一个带有蓝色网格线的条形图,如下所示。我想要的是蓝线不会切入条形图,而是仅在整个宽度结束后开始。这个是可能的吗?
ggplot(mpg, aes(class)) + geom_bar(aes(weight = displ)) +
theme(panel.grid.major.x = element_line(colour="blue", size=0.5))
图中的箭头显示了我希望网格线对齐的位置,而不是切入条形图的位置。
发布于 2020-05-08 19:34:52
您会满足于使用geom_vline
吗
ggplot(mpg, aes(class)) + geom_bar(aes(weight = displ)) +
geom_vline(xintercept= seq_along(unique(mpg$class)) + 0.45, color = "blue") +
theme(panel.grid.major.x = element_blank())
另一种选择可能是使用position_nudge
,但是您必须担心axis.text.x
位置和填充x限制。
ggplot(mpg, aes(x = class)) + geom_bar(aes(weight = displ), position = position_nudge(-0.45)) +
scale_x_discrete(expand = c(0.1,0.4)) +
theme(panel.grid.major.x = element_line(colour="blue", size=0.5),
axis.text.x = element_text(hjust = 1))
https://stackoverflow.com/questions/61686428
复制