Question

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?

example bar plot

Was it helpful?

Solution

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)

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top