【问题标题】:ggplot2 plot without axes, legends, etcggplot2 没有坐标轴、图例等的绘图
【发布时间】:2011-09-25 13:42:06
【问题描述】:

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

【问题讨论】:

  • 在图像编辑器中创建十六进制图并对其进行裁剪不是更容易吗?
  • 试试theme_void()

标签: r ggplot2


【解决方案1】:

这是你想要的吗?

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

【讨论】:

  • 去掉了图例,但 x 轴和 y 轴以及背景网格仍然存在。
【解决方案2】:

根据我在 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()。)

【讨论】:

  • 非常感谢!我还在groups.google.com/group/ggplot2/browse_thread/thread/… 找到了类似的解决方案
  • 顺便评论:在某些情况下,theme(axis.ticks=element_blank()) 效果不如theme(axis.ticks.x=element_blank()),可能是某个地方的临时错误(我有自己的主题集,然后我尝试覆盖:只有@ 987654330@ 和 axis.ticks.y 完成这项工作。)
【解决方案3】:
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"
  • grid.ls() 显示视口和 grob 对象列表
  • 看来在其他版本的ggplot中我使用的面板名称不同
  • xy
【解决方案4】:
'opts' is deprecated.

ggplot2 &gt;= 0.9.2使用

p + theme(legend.position = "none") 

【讨论】:

  • 我知道您还没有编辑权限,但是如果您发现我的其他 ggplot2 答案需要更新 re:opts() ,请随时提出编辑建议。我会收到通知,并可以自己合并。
【解决方案5】:

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_gridfacet_wrap 结合使用,您也会丢失构面标签周围的框。如果您不希望这种情况发生,那么这个答案就是您可以使用的答案。
【解决方案6】:

当前的答案要么不完整,要么效率低下。这是(也许)实现结果的最短方法(使用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% 无效
  • 实验室 (x="", y="" ) 似乎没有删除轴,只是删除了标签。
  • @miratrix 对不起,我的错误。已更新。
  • @luchonacho 使用labs(x="",y="") 会留下轴标题空间,因为实际上有标题,它们只是没有符号。要为它们删除轴标题和空间,最好使用+ theme(axis.title = element_blank())
  • labs(x = NULL)xlab(NULL) 是其他方式。
【解决方案7】:

聚会迟到了,但可能有兴趣...

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

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

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

【讨论】:

    【解决方案8】:

    我在这里没有找到这个解决方案。它使用 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)
    

    【讨论】:

      猜你喜欢
      • 2020-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-14
      • 2017-02-05
      • 1970-01-01
      • 2020-05-20
      • 1970-01-01
      相关资源
      最近更新 更多