2016-09-22 10 views
0

私は2つの散布図を作るために2つのデータフレームを持っています。 1つの列を使ってマーカーのアルファとサイズを設定し、2番目のプロットのスケーリングを最初のものと同じにする必要があります。問題は、プロットAの値の範囲が0から1の間で、プロットBの範囲が0から0.5(Bのスケールも0から1である必要があります)...ggplotで2つの異なるプロットに同じアルファ/サイズスケールを使用

簡単な例:

x=seq(from=1, to=10, by=1) 
y=seq(from=1, to=10, by=1) 
markerA=sample(0:100,10, replace=T)/100 
markerB=sample(0:50,10, replace=T)/100 
dfA=data.frame(x,y,markerA) 
dfB=data.frame(x,y,markerB) 
a<- ggplot(dfA,aes(x=x, y=y)) 
a <- a + geom_point(aes(alpha=dfA$markerA, size=dfA$markerA)) 
a 
b<- ggplot(dfB,aes(x=x, y=y)) 
b <- b + geom_point(aes(alpha=dfB$markerB, size=dfB$markerB)) 
b 

plot A plot B

答えて

3

ちょうどあなたのプロットにscale_sizescale_alphaを追加...私はこれを行う簡単な方法があるべきだと思うが、私はそれを見つけるように見えることはできません。 ggplot2
、ここでaes

$variableを使用しないように覚えているが、一例である:

enter image description here

a = ggplot(dfA,aes(x=x, y=y)) + 
geom_point(aes(alpha=markerA, size=markerA)) + 
scale_size(limits = c(0,1)) + 
scale_alpha(limits = c(0,1)) 

b = ggplot(dfB,aes(x=x, y=y)) + 
geom_point(aes(alpha=markerB, size=markerB)) + 
scale_size(limits = c(0,1)) + 
scale_alpha(limits = c(0,1)) 

grid.arrange(a,b) 
+0

scale_size()とscale_alpha()は私が探していたものです。ありがとうございました :) – user3388408

0

まず、あなたはggplot2内$を使用しないでください。第二に、これはより全体的なアプローチである可能性があります

library(dplyr) 
library(tidyr) 

bind_cols(dfA, select(dfB, markerB)) %>% 
    gather(marker, value, -x, -y) %>% 
    mutate(marker=gsub("marker", "", marker)) -> both 

gg <- ggplot(both, aes(x, y)) 
gg <- gg + geom_point(aes(alpha=value, size=value)) 
gg <- gg + facet_wrap(~marker, ncol=1) 
gg <- gg + scale_alpha_continuous(limits=c(0,1)) 
gg <- gg + scale_size_continuous(limits=c(0,1)) 
gg <- gg + theme_bw() 
gg 

enter image description here

色盲フレンドリー背景や前景の色あなたはアルファより(いずれもアルファグレーオングレーを目立たせるために使用することができますがあります。白いアルファ - グレーもとてもフレンドリーです)。

関連する問題