我想使用Bioconductor的hexbin(我可以做到)来生成一个填满整个(png)显示区域的图表 - 没有坐标轴,没有标签,没有背景。
根据我在Chase答案中的评论,你可以使用element_blank
来移除很多这些内容:
dat <- data.frame(x=runif(10),y=runif(10))
p <- ggplot(dat, aes(x=x, y=y)) +
geom_point() +
scale_x_continuous(expand=c(0,0)) +
scale_y_continuous(expand=c(0,0))
p + theme(axis.line=element_blank(),axis.text.x=element_blank(),
axis.text.y=element_blank(),axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),legend.position="none",
panel.background=element_blank(),panel.border=element_blank(),panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),plot.background=element_blank())
保存为.png文件时,似乎仍然在周围有一个小边距。也许有人知道如何去除这一部分。
(历史注:自ggplot2版本0.9.2起,opts已被弃用。请改用theme(),并将theme_blank()替换为element_blank()。)
axis.ticks.x
和axis.ticks.y
有效)。 - PatrickTtheme(plot.margin=grid::unit(c(0,0,0,0), "mm"))
- undefined关于将opts改为theme等(懒人专用)的回复:
theme(axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
legend.position="none",
panel.background=element_blank(),
panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
plot.background=element_blank())
theme_void
是实现OP目标最简单的方法,但如果与facet_grid
或facet_wrap
结合使用,您还将失去围绕分面标签的框。如果您不希望发生这种情况,那么这个答案就是要使用的。 - EcologyTom目前的答案要么不完整,要么效率低下。以下是(也许)使用theme_void()
实现结果的最短方式:
Current answers are either incomplete or inefficient. Here is (perhaps) the shortest way to achieve the outcome (using theme_void()
:
data(diamonds) # Data example
ggplot(data = diamonds, mapping = aes(x = clarity)) + geom_bar(aes(fill = cut)) +
theme_void() + theme(legend.position="none")
结果是:
如果你只想消除标签,labs(x="", y="")
可以解决问题:
ggplot(data = diamonds, mapping = aes(x = clarity)) + geom_bar(aes(fill = cut)) +
labs(x="", y="")
labs(x="", y="")
会在坐标轴标题处留下空白,因为实际上这些标题是存在的,只是没有显示。要移除坐标轴标题及其所占的空间,最好使用 + theme(axis.title = element_blank())
。 - Didzis Elfertslabs(x = NULL)
或 xlab(NULL)
是其他的方式。 - PatrickT'opts' is deprecated.
在ggplot2 >= 0.9.2
版本中使用
p + theme(legend.position = "none")
虽然有点晚,但可能会引起兴趣...
我发现在许多情况下,labs
和 guides
规范的组合非常有用:
你只需要一个网格和一个背景:
ggplot(diamonds, mapping = aes(x = clarity)) +
geom_bar(aes(fill = cut)) +
labs(x = NULL, y = NULL) +
guides(x = "none", y = "none")
您希望仅抑制一个或两个轴的刻度标签:
ggplot(diamonds, mapping = aes(x = clarity)) +
geom_bar(aes(fill = cut)) +
guides(x = "none", y = "none")
使用ggeasy,它更简单。
library(ggeasy)
p + theme_classic()+easy_remove_axes() + easy_remove_legend()
xy <- data.frame(x=1:10, y=10:1)
plot <- ggplot(data = xy)+geom_point(aes(x = x, y = y))
plot
panel = grid.get("panel-3-3")
grid.newpage()
pushViewport(viewport(w=1, h=1, name="layout"))
pushViewport(viewport(w=1, h=1, name="panel-3-3"))
upViewport(1)
upViewport(1)
grid.draw(panel)
Error in UseMethod("grid.draw") : no applicable method for 'grid.draw' applied to an object of class "NULL"
- Roman Luštrik我在这里没有找到这个解决方案。使用cowplot包可以将其全部删除:
library(cowplot)
p + theme_nothing() +
theme(legend.position="none") +
scale_x_continuous(expand=c(0,0)) +
scale_y_continuous(expand=c(0,0)) +
labs(x = NULL, y = NULL)
刚刚注意到可以使用theme.void()来完成同样的事情,像这样:
p + theme_void() +
theme(legend.position="none") +
scale_x_continuous(expand=c(0,0)) +
scale_y_continuous(expand=c(0,0)) +
labs(x = NULL, y = NULL)
这个做你想要的吗?
p <- ggplot(myData, aes(foo, bar)) + geom_whateverGeomYouWant(more = options) +
p + scale_x_continuous(expand=c(0,0)) +
scale_y_continuous(expand=c(0,0)) +
opts(legend.position = "none")
theme_void()
。 - Brian D