簡體   English   中英

ggplot2 ggsave pdf透明框周圍的實線

[英]ggplot2 ggsave pdf solid lines around transparent boxes

我想比較直方圖的不同峰,因此我將其中兩個峰放在alpha = 0.5的一個圖中。 RStudio中的默認輸出看起來不錯,但是當我使用pdf()或ggsave()或什至CairoPDF()時,結果輸出的方框周圍是實線。 以我為例,在放大pdf顯示時,這些實線有時看起來非常難看。 我想禁用這些實線,或者至少使這些線與填充相同的顏色/ alpha。

最小代碼示例:

library(ggplot2)

p1<-ggplot()+
  geom_histogram(data=diamonds[diamonds$cut == "Premium",],aes(x=depth,fill=cut),alpha=0.5)+
  geom_histogram(data=diamonds[diamonds$cut == "Ideal",],aes(x=depth,fill=cut),alpha=0.5)

ggsave("histoline.pdf",p1)

這是輸出:

在此處輸入圖片說明

我嘗試了各種修改線型和尺寸的方法,但直到現在都沒有成功。

一種解決方法是手動計算直方圖,並使用ggplot僅顯示相應的多邊形。 它看起來像我想要的,但當然那是多余的代碼行,您會損失很多ggplot功能。 注意:hist()和geom_hist()對bin寬度的處理略有不同。

library(ggplot2)


hP <-hist(diamonds[diamonds$cut == "Premium",]$depth,breaks=seq(43,68,1))
hPd<-data.frame(breaks=c(hP$breaks[-1],hP$breaks),counts=c(hP$counts,hP$counts,hP$counts[1]))
hPd<-hPd[order(hPd$breaks),]

hI <- hist(diamonds[diamonds$cut == "Ideal",]$depth,breaks=seq(43,68,1))
hId<-data.frame(breaks=c(hI$breaks[-1],hI$breaks),counts=c(hI$counts,hI$counts,hI$counts[1]))
hId<-hId[order(hId$breaks),]

p1<-ggplot()+
  geom_polygon(data=hPd,aes(x=breaks,y=counts),fill="blue",alpha=0.5)+
  geom_polygon(data=hId,aes(x=breaks,y=counts),fill="red",alpha=0.5)
  #  this should yield the same binning behaviour as hist()
  #  geom_histogram(data=diamonds[diamonds$cut == "Premium",]
                  #,aes(x=depth),fill="green",binwidth=1,center=0.5,alpha=0.5)

print(p1)

ggsave("histoline.pdf",p1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM