2017-04-14 111 views
2

我試圖擺脫geom_label在輸出爲pdf時創建的邊框。設置lable.size = 0似乎在輸出到png時或在R會話中的繪圖窗口中工作,但當輸出爲pdf時,邊界仍然存在。我喜歡geom_text上的geom_label,因爲背景使得在劇情中有線條時易於閱讀。由於我對後面的情節做了些什麼,所以如果有人知道如何擺脫邊界將會很棒,那麼我可以更容易地獲得pdf輸出。在R輸出爲ggplot2 geom_label的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() 
+0

嗯,當我做R環境中的情節和PNG文件都有一個圍繞它們的框。我認爲這是'ggplot'不能讓你指定0的問題。一個解決方法(雖然它提供了很多警告消息)正在使用'label.size = unit(0,「mm」)'。 –

回答

5

編輯:我看着源代碼ggplot2,它實際上是一個grid問題,而不是ggplot2。由於某種原因,它看起來像grid覆蓋lwd。例如,運行下面的代碼,即使我們指定了lwd = 0,我們仍然看到仍然存在線寬。

library(grid) 
grid.newpage();grid.draw(roundrectGrob(gp = gpar(lwd = 0))) 

看起來你需要指定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() 

enter image description here

+0

非常好找!工作得很好,謝謝! –