ggplot2条形图,geom底部和x轴之间没有空间保持上面的空间 [英] ggplot2 bar plot, no space between bottom of geom and x axis keep space above
问题描述
当我在ggplot2中绘制条形图时,我想将条形底部和x轴之间的空间缩小到0,但仍保留条形图和绘图框之上的空间。我有一个黑客做下面。它很脏,我想再次清洁。有没有办法实现这种行为没有肮脏的小黑客?
默认(上面希望的空间,但不希望空格低于酒吧):
ggplot(mtcars,aes(x = as.factor(carb))+
geom_bar()
使用展开(上面有不需要的0空间,但是在空格下方有0空格):
$ b $
ggplot(mtcars,aes(x = as.factor(carb))+
geom_bar()+
scale_y_continuous (expand = c(0,0))
脏哈克(我喜欢它,但它..很好,脏):
ggplot(mtcars,aes(x = as.factor(carb))+
geom_bar()+
scale_y_continuous(expand = c(0,0))+
geom_text(aes(x = 1,y = 10.3,label =S trevch it),vjust = -1)
我可能会想念你真的很想要,但是没有使用 geom_text
hack,你仍然可以设置限制
ggplot(mtcars,aes(x = as.factor(carb)))+
geom_bar()+
scale_y_continuous(expand = c(0,0),limits = c(0,10.3))
#边际清洁
When I plot a bar graph in ggplot2 I would like to reduce the space between the bottom of the bars and the x-axis to 0, yet keep the space above the bars and the plot box. I have a hack to do it below. It's dirty and I want to be clean again. Is there a way to achieve this behavior without the dirty little hack?
Default (desired space above but don't want space below bars):
ggplot(mtcars, aes(x=as.factor(carb))) +
geom_bar()
Use expand (undesired 0 space above but got the 0 space below bars):
ggplot(mtcars, aes(x=as.factor(carb))) +
geom_bar() +
scale_y_continuous(expand = c(0,0))
Dirty Hack (I like it but its.. well, dirty):
ggplot(mtcars, aes(x=as.factor(carb))) +
geom_bar() +
scale_y_continuous(expand = c(0,0)) +
geom_text(aes(x=1, y=10.3, label="Stretch it"), vjust=-1)
I might be missing what you really want, but without using geom_text
hack you can still set the limits
ggplot(mtcars, aes(x = as.factor(carb))) +
geom_bar() +
scale_y_continuous(expand = c(0, 0), limits = c(0, 10.3))
# marginally cleaner
这篇关于ggplot2条形图,geom底部和x轴之间没有空间保持上面的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!