R ggplot2 gg在透明框周围保存pdf实线

R ggplot2 gg在透明框周围保存pdf实线,r,pdf,ggplot2,R,Pdf,Ggplot2,我想比较直方图中的不同峰值,所以我把其中两个放在一个图中,alpha=0.5。RStudio中的默认输出看起来不错,但是当我使用pdf()或ggsave()甚至CairoPDF()时,结果输出框周围有实线。在我的例子中,当缩放pdf显示时,这些实线有时看起来非常难看。我想禁用这些实线,或者至少使这些线与填充线的颜色/alpha相同 最小代码示例: library(ggplot2) p1<-ggplot()+ geom_histogram(data=diamonds[diamonds$

我想比较直方图中的不同峰值,所以我把其中两个放在一个图中,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)
库(ggplot2)

p1一种解决方法是手动计算直方图,并使用ggplot仅显示相应的多边形。这看起来像我想要的,但当然,这是一些额外的代码行,你会失去很多ggplot功能。注意:hist()和geom_hist()对垃圾箱宽度的处理略有不同

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)
库(ggplot2)

hP您尝试过吗?大小=0的结果相同,大小=-1和大小=NA=(linetype=“blank”对我也不起作用。嗯,也许这些条有一点重叠?当我放大到6400%时,间隙变得明显。让我们称之为解决方法。这看起来像我想要的,但方式不是很优雅