2016-07-12 8 views
-1

ggplot2を使用すると、2つのグラフをどのようにブレンドできますか?データ上に2つのセットをグラフ表示すると、第2のデータセットが第1のデータセットをカバーします。両方のグラフをブレンドする方法はありますか?私はすでにアルファ値をできる限り低く抑えています。それより低いと私は個々のポイントを見ることができません。2つのオーバーラップグラフをggplot2とブレンドする方法

demanalyze <- function(infocode, n = 1){ 
    infoname <- filter(infolookup, column_name == infocode)$description 
    infocolumn <- as.vector(as.matrix(mydata[infocode])) 
    ggplot(mydata) + 
    aes(x = infocolumn) + 
    ggtitle(infoname) + 
    xlab(infoname) + 
    ylab("Fraction of votes each canidate recieved") + 
    xlab(infoname) + 
    geom_point(aes(y = sanders_vote_fraction, colour = "Bernie Sanders"), size=I(2)) +#, color = alpha("blue",0.02), size=I(1)) + 
    stat_smooth(aes(y = sanders_vote_fraction), method = "lm", formula = y ~ poly(x, n), size = 1, color = "darkblue", se = F) + 
    geom_point(aes(y = clinton_vote_fraction, colour = "Hillary Clinton"), size=I(2)) +#, color = alpha("red",0.02), size=I(1)) + 
    stat_smooth(aes(y = clinton_vote_fraction), method = "lm", formula = y ~ poly(x, n), size = 1, color = "darkred", se = F) + 
    scale_colour_manual("", 
     values = c("Bernie Sanders" = alpha("blue",0.005), "Hillary Clinton" = alpha("red",0.005)) 
    ) + 
    guides(colour = guide_legend(override.aes = list(alpha = 1))) 
} 

ブレンドすると、同じ場所に赤い点と青い点があり、紫色で表示されます。 enter image description here

+0

不明である何? – 2426021684

+0

あなたのグラフが色を期待しているように見える場合、私はスケールを調べますhttp://docs.ggplot2.org/current/scale_manual.html scale_alpha_manual –

+0

私は赤と青の曲線をプロットし、上部のグラフィックスレイヤーに紫色の点を描画します。 – vincentmajor

答えて

1

プロットを見ると、私の推測によれば、問題は赤色の重なり合いがあり、下の青色が遮られています。グラフ上のレイヤーをランダム化する必要があるかもしれないと思うので、単一のdata.frameを生成する必要があります。あるいは、Hillary + Bernieが常に1に等しい場合、あなたはそれをプロットすることができます。彼らがいなくて、あまりにも多くの情報を失いたくないのであれば、(Hillary)/(Bernie + Hillary)のメトリックをプロットすることができます。

例:

geom_point(aes(y = clinton_vote_fraction/(clinton_vote_fraction + sanders_vote_fraction) 
      , colour = "Clinton Share"), size=I(2)) 

そして、ここでは、溶融アプローチと例です。

library(dplyr) 
library(reshape2) 
df <- 
    data.frame(
    metric = rnorm(1000) 
    , Clinton = rnorm(1000, 48, 10) 
) %>% 
    mutate(Sanders = 100 - Clinton - rnorm(4)) 


meltDF <- 
    melt(df, "metric" 
     , variable.name = "Candidate" 
     , value.name = "Vote Share") 

ggplot(meltDF %>% 
     arrange(sample(1:nrow(.))) 
     , aes(x = metric 
      , y = `Vote Share` 
      , col = Candidate)) + 
geom_point(size = 2, alpha = 0.2) + 
geom_smooth(se = FALSE, alpha = 1, show.legend = FALSE) + 
scale_colour_manual("", 
        values = c("Clinton" = "darkblue" 
           , "Sanders" = "red3") 
) + 
theme_minimal() 

enter image description here

関連する問題