在R的ggplot2的geom_label的pdf输出中去掉边界

Nic*_*way 5 r ggplot2

我在输出到pdf时试图摆脱geom_label创建的边框.设置lable.size = 0似乎在输出到png时或仅在R会话中的绘图窗口中有效但在输出到pdf时边界仍然存在.我喜欢geom_label over geom_text,因为背景使得当绘图上有线条时更容易阅读.由于我正在使用后者的情节,所以我更容易获得pdf输出,所以如果有人知道如何摆脱那将是伟大的边界.

我目前在Mac OS Sierra上使用ggplot 2.2.1和R 3.3.3.下面是一些示例代码.

require(ggplot2)
#no border
ggplot(data.frame(nums = 1:10), aes(x =nums, y = nums) ) +
  geom_point() +
  geom_label(label = "italic(R) ^ 2 == .87", label.size = 0, x = 2, y = 8, vjust = "inward", hjust = "inward", parse = T) + 
  theme_bw()


pdf("testPlot.pdf")
#border appears in pdf 
print(ggplot(data.frame(nums = 1:10), aes(x =nums, y = nums) ) +
        geom_point() +
        geom_label(label = "italic(R) ^ 2 == .87", label.size = 0, x = 2, y = 8, vjust = "inward", hjust = "inward", parse = T) + 
        theme_bw())
dev.off()


png("testPlot.png")
#no border with png
print(ggplot(data.frame(nums = 1:10), aes(x =nums, y = nums) ) +
        geom_point() +
        geom_label(label = "italic(R) ^ 2 == .87", label.size = 0, x = 2, y = 8, vjust = "inward", hjust = "inward", parse = T) + 
        theme_bw())
dev.off()
Run Code Online (Sandbox Code Playgroud)

Mik*_* H. 9

编辑:我查看了源代码ggplot2,它实际上是一个grid问题,而不是ggplot2.由于某种原因,它似乎grid会覆盖它lwd.例如,运行下面的代码,我们看到即使我们指定,仍然有一个线宽lwd = 0.

library(grid)
grid.newpage();grid.draw(roundrectGrob(gp = gpar(lwd = 0)))
Run Code Online (Sandbox Code Playgroud)

看起来你需要指定lwd = NA.例如,这不会给你边框:grid.newpage();grid.draw(roundrectGrob(gp = gpar(lwd = NA)))

话虽如此,如果您将ggplot代码更改为:它应该可以工作(没有警告消息):

ggplot(data.frame(nums = 1:10), aes(x =nums, y = nums) ) +
  geom_point() +
  geom_label(label = "italic(R) ^ 2 == .87", label.size = NA, x = 2, y = 8, vjust = "inward", hjust = "inward", parse = T) + 
  theme_bw()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述