Add ticks without labels on the top of a bar plot in ggplot2

送分小仙女□ 提交于 2020-01-29 09:44:09

问题


As the title says, I would like to add ticks to the TOP part of the bar plot in ggplot2. The output would look something like this: (hiding the actual plot due to confidential information). Is there a function in ggplot2 that does this?


回答1:


I have adapted Baptiste's solution at link Display y-axis for each subplot when faceting. Idea (i think) is to extract the x-axis grob and add it to the top of the plot.

library(ggplot2)
library(gtable)

# plot
p1 <- ggplot(mtcars, aes(factor(cyl))) + geom_bar() + theme_bw()


gg <- ggplotGrob(p1)

axis <- gtable_filter(gg ,"axis-b")[["grobs"]][[1]][["children"]][["axis"]][1,]
panels <- subset(gg$layout, name == "panel")
gg <- gtable_add_grob(gg, grobs=axis, name="ticks", t = panels$t-1, l=panels$l)

grid.newpage()
grid.draw(gg)



来源:https://stackoverflow.com/questions/22318186/add-ticks-without-labels-on-the-top-of-a-bar-plot-in-ggplot2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!