【发布时间】:2011-09-25 13:42:06
【问题描述】:
我想使用 bioconductor 的 hexbin(我可以做到)来生成一个填充整个 (png) 显示区域的图 - 没有轴,没有标签,没有背景,没有 nuthin'。
【问题讨论】:
-
在图像编辑器中创建十六进制图并对其进行裁剪不是更容易吗?
-
试试
theme_void()
我想使用 bioconductor 的 hexbin(我可以做到)来生成一个填充整个 (png) 显示区域的图 - 没有轴,没有标签,没有背景,没有 nuthin'。
【问题讨论】:
theme_void()
这是你想要的吗?
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")
【讨论】:
根据我在 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()。)
【讨论】:
theme(axis.ticks=element_blank()) 效果不如theme(axis.ticks.x=element_blank()),可能是某个地方的临时错误(我有自己的主题集,然后我尝试覆盖:只有@ 987654330@ 和 axis.ticks.y 完成这项工作。)
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"
'opts' is deprecated.
在ggplot2 >= 0.9.2使用
p + theme(legend.position = "none")
【讨论】:
Re:将选项更改为主题等(对于懒惰的人):
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 结合使用,您也会丢失构面标签周围的框。如果您不希望这种情况发生,那么这个答案就是您可以使用的答案。
当前的答案要么不完整,要么效率低下。这是(也许)实现结果的最短方法(使用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="")
【讨论】:
ggplot(data = diamonds, mapping = aes(x = clarity)) + geom_bar(aes(fill = cut)) + theme_void() + theme(legend.position="none", panel.background = element_rect(fill="grey80"), plot.background = element_rect(fill="red")) 建议它不是 100% 无效
labs(x="",y="") 会留下轴标题空间,因为实际上有标题,它们只是没有符号。要为它们删除轴标题和空间,最好使用+ theme(axis.title = element_blank())
labs(x = NULL) 或 xlab(NULL) 是其他方式。
聚会迟到了,但可能有兴趣...
我发现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")
【讨论】:
我在这里没有找到这个解决方案。它使用 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)
【讨论】: