在ggplot2的条形图顶部添加没有标签的刻度 [英] Add ticks without labels on the top of a bar plot in ggplot2
本文介绍了在ggplot2的条形图顶部添加没有标签的刻度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
解决方案
我已经调整了Baptiste的解决方案链接在切面时显示每个子图的y轴。 Idea(我认为)是提取x轴grob并将其添加到图的顶部。
library(ggplot2 )
library(gtable)
#plot
p1 < - ggplot(mtcars,aes(factor(cyl)))+ geom_bar()+ theme_bw()
gg < - ggplotGrob(p1)
轴< - gtable_filter(gg,axis-b)[[grobs]] [[1] ] [[children]] [[[axis]]] [1,]
面板< - subset(gg $ layout,name ==panel)
gg< - gtable_add_grob gg,grobs = axis,name =ticks,t = panel $ t-1,l = panels $ l)
grid.newpage()
grid.draw(gg)
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?
解决方案
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)
这篇关于在ggplot2的条形图顶部添加没有标签的刻度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文