2017-06-06 8 views
0

ヒストグラムの異なるピークを比較したいので、2つをアルファ= 0.5の1つのプロットに入れます。 RStudioのデフォルト出力はうまく見えますが、pdf()やggsave()、さらにはCairoPDF()を使用しても出力結果にはボックスの周囲に実線が表示されます。私の場合、これらの実線は時々pdfの表示を拡大するときに非常に醜いものに見えます。私は、これらの実線を無効にするか、少なくとも線が充填と同じ色/アルファになるようにします。ggplot2 ggsave pdf透明箱の周りの実線

最小限のコード例:私は線種やサイズを変更するためのさまざまな方法を試してみましたが、今まで成功していない

enter image description here

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) 

これが出力されます。

+0

あなたは[サイズ](https://stackoverflow.com/questions/34878848/control-bar-border-color-thickness-with-ggplot2-stroke)を試したことがありますか? –

+0

同じ結果をsize = 0、noに出力すると、サイズ= -1、サイズ= NA =( – fulz

+0

)は表示されません。 – fulz

答えて

-1

ヒストグラムを手動で計算し、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) 
+0

???これはあなたの質問や新しい質問の答えですか? –

+0

私は持っているもののように見えますが、あまりエレガントではない方法です – fulz

関連する問題