繁体   English   中英

ggplot2 plot 不带轴、图例等

[英]ggplot2 plot without axes, legends, etc

我想使用 bioconductor 的 hexbin(我可以做到)生成一个 plot 填充整个(png)显示区域 - 没有轴,没有标签,没有背景,没有 nuthin'。

根据我在大通的回答中的评论,您可以使用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.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()

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="")
'opts' is deprecated.

ggplot2 >= 0.9.2使用

p + theme(legend.position = "none") 
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)

使用ggeasy,更简单。

library(ggeasy)
p + theme_classic()+easy_remove_axes() + easy_remove_legend()

晚会迟到了,但可能会引起兴趣...

我发现labsguides规范的组合在许多情况下很有用:

你只需要一个网格和一个背景:

ggplot(diamonds, mapping = aes(x = clarity)) + 
  geom_bar(aes(fill = cut)) + 
  labs(x = NULL, y = NULL) + 
  guides(x = "none", y = "none")

在此处输入图像描述

您只想抑制一个或两个轴的刻度线 label :

ggplot(diamonds, mapping = aes(x = clarity)) + 
  geom_bar(aes(fill = cut)) + 
  guides(x = "none", y = "none")

在此处输入图像描述

我在这里没有找到这个解决方案。 它使用cowplot package 删除所有这些:

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")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM