2017-11-30 10 views
1

以下の疑問についてコメントしたいと思います。私は、次のグラフを作ったggplot2の1つのグラフの中に2つの凡例をカスタマイズします

Plot<-data.frame(Age=c(0,0,0,0,0),Density=c(0,0,0,0,0),Sensitivity=c(0,0,0,0,0),inf=c(0,0,0,0,0),sup=c(0,0,0,0,0),tde=c(0,0,0,0,0))  
Plot[1,]<-c(1,1,0.857,0.793,0.904,0.00209834) 
Plot[2,]<-c(1,2,0.771 ,0.74,0.799,0.00348286) 
Plot[3,]<-c(1,3,0.763 ,0.717,0.804,0.00577784) 
Plot[4,]<-c(1,4,0.724 ,0.653,0.785,0.00504161) 
Plot[5,]<-c(2,1,0.906,0.866,0.934,0.00365742) 
Plot[6,]<-c(2,2,0.785 ,0.754,0.813,0.00440399) 
Plot[7,]<-c(2,3,0.660,0.593,0.722,0.00542849) 
Plot[8,]<-c(2,4,0.544,0.425,0.658,0.00433052) 

    names(Plot)<-c("Age","Mammographyc density","Sensitivity","inf","sup","tde") 
    Plot$Age<-c("50-59","50-59","50-59","50-59","60-69","60-69","60-69","60-69") 
    Plot$Density<-c("Almost entirely fat","Scattered fibroglandular density","Heterogeneously dense","Extremely dense","Almost entirely fat","Scattered fibroglandular density","Heterogeneously dense","Extremely dense")  
levels(Plot$Age)<-c("50-59","60-69") 
levels(Plot$Density)<-c("Almost entirely fat","Scattered fibroglandular density","Heterogeneously dense","Extremely dense") 
pd <- position_dodge(0.2) # 
    Plot$Density <- reorder(Plot$Density, 1-Plot$Sensitivity) 

ggplot(Plot, aes(x = Density, y = 100*Sensitivity, colour=Age)) + 
    geom_errorbar(aes(ymin = 100*inf, ymax = 100*sup), width = .1, position = pd) + 
    geom_line(position = pd, aes(group = Age), linetype = c("dashed")) + 
    geom_point(position = pd, size = 4)+ 
    scale_y_continuous(expand = c(0, 0),name = 'Sensitivity (%)',sec.axis = sec_axis(~./5, name = 'Breast cancer detection rate (per 1000 mammograms)', breaks = c(0,5,10,15,20), 
                   labels = c('0‰',"5‰", '10‰', '15‰', '20‰')), limits = c(0,100)) + 
    geom_line(position = pd, aes(x = Density, y = tde * 5000, colour = Age, group = Age), linetype = c("dashed"), data = Plot) + 
    geom_point(shape=18,aes(x = Density, y = tde * 5000, colour = Age, group = Age), position = pd, size = 4) + 
    theme_light() + 
scale_color_manual(name="Age (years)",values = c("50-59"= "grey55", "60-69" = "grey15")) + 
    theme(legend.position="bottom") + guides(colour = guide_legend(), size = guide_legend(), 
             shape = guide_legend()) 

Graph

左の軸は、円の規模と軸が右側である:

このコードを使用ダイヤモンドのスケールです。実際には、私は約このような伝説がしたいということです。

Legend

をしかし、それは私のために不可能である、私はguidesscale_shapeと異なるコマンドのように、他のスレッドの提案を試してみましたが、私は持っていません成功。私はちょうど形と色がどのようなものであるかの違いを明確にしたい。

誰かが私を助ける方法を知っていますか?

敬具、あなたは、二重軸の混乱を避けるために、パネルプロットんですべきこと

答えて

2

:あなたが求めて何をするのではなく、

library(dplyr) 
library(tidyr) 
Plot %>% 
    gather(measure, Result, Sensitivity, tde) %>% 
    ggplot(aes(x = Density, y = Result, colour=Age)) + 
    geom_errorbar(aes(ymin = inf, ymax = sup), width = .1, position = pd, 
       data = . %>% filter(measure == "Sensitivity")) + 
    geom_line(aes(group = Age), position = pd, linetype = "dashed") + 
    geom_point(position = pd, size = 4)+ 
    # scale_y_continuous(expand = c(0, 0), limits = c(0, 1)) + 
    scale_y_continuous(labels = scales::percent) + 
    facet_wrap(~measure, ncol = 1, scales = "free_y") + 
    theme_light() + 
    scale_color_manual(name="Age (years)",values = c("50-59"= "grey55", "60-69" = "grey15")) + 
    theme(legend.position="bottom") 

enter image description here

、あなたは問題あなたは1つだけの非定位の美学がマッピングされているので、複数の凡例を取得することはできません。 2番目の凡例を強制するには、2番目のマッピングを追加する必要があります。それは効果がないダミーのマッピングになることがあります。alphaをマップしますが、両方のレベルを手動で100%にスケールします。このソリューションは、はお勧めできません。希望の凡例の例で行ったように、マッピングが混ざりやすく、どの点が感度であり、検出率が間違っているかによって嘘をつきます。

ggplot(Plot, aes(x = Density, y = 100*Sensitivity, colour=Age, alpha = Age)) + 
    geom_errorbar(aes(ymin = 100*inf, ymax = 100*sup), width = .1, position = pd) + 
    geom_line(position = pd, aes(group = Age), linetype = c("dashed")) + 
    geom_point(position = pd, size = 4)+ 
    scale_y_continuous(expand = c(0, 0),name = 'Sensitivity (%)',sec.axis = sec_axis(~./5, name = 'Breast cancer detection rate (per 1000 mammograms)', breaks = c(0,5,10,15,20), 
                        labels = c('0‰',"5‰", '10‰', '15‰', '20‰')), limits = c(0,100)) + 
    geom_line(position = pd, aes(x = Density, y = tde * 5000, colour = Age, group = Age), linetype = c("dashed"), data = Plot) + 
    geom_point(shape=18,aes(x = Density, y = tde * 5000, colour = Age, group = Age), position = pd, size = 4) + 
    theme_light() + 
    scale_color_manual(name="Age (years)",values = c("50-59"= "grey55", "60-69" = "grey15")) + 
    scale_alpha_manual(values = c(1, 1)) + 
    guides(alpha = guide_legend("Sensitivity"), 
     color = guide_legend("Detection Rate", override.aes = list(shape = 18))) + 
    theme(legend.position="bottom") 

enter image description here

+0

私はどうもありがとうございましたあなたは提案するソリューションは、はるかに優れていると思います。ちょうど1つの質問ですが、どうすればその "tde"のラベルを変更できますか? 「検出率」を置くのが本当に好きなので、そのような変数を呼び出すことはできません。 –

+0

ファセットラベルをカスタマイズするのは少し複雑です。私が最も頻繁に行うことは、ggplotに渡す前にデータフレーム内の変数の名前を変更することです。プロットでそれを行うには、この引数を 'facet_wrap'呼び出しに追加します:' labeller = labeller(measure = c( "検出率" = "tde"、 "Sensitivity" = "Sensitivity")) –

関連する問題