2017-10-20 12 views
1

Iは、クロス集計表をプロットhereから変更されたRで次のコードを持っている:可視クロス集計表 - 変更色

#load ggplot2 
library(ggplot2)       

# Set up the vectors       
xaxis <- c("A", "B") 
yaxis <- c("A","B") 

# Create the data frame 
df <- expand.grid(xaxis, yaxis) 
df$value <- c(120,5,30,200)  

#Plot the Data 
g <- <- ggplot(df, aes(Var1, Var2)) + geom_point(aes(size = value), colour = "lightblue") + theme_bw() + xlab("") + ylab("") 
g + scale_size_continuous(range=c(10,30)) + geom_text(aes(label = value)) 

それが大きい右図を生成し、私は4つのドットをカスタムカラーにすることを望んでいました。理想的には、左上と右下がそれぞれ1つの色で、右上と左下が別の色です。

私が使用しようとしました:

+ scale_color_manual(values=c("blue","red","blue","red")) 

それが動作するようには思えません。何か案は?

答えて

0

データフレーム内のベクトルで色を付けることをお勧めします。これを与える列がないので、作成するか、既存の列に基づいてルールを作成することができます以下):

g <- ggplot(df, aes(Var1, Var2)) + geom_point(aes(size = value, colour = (Var2!=Var1))) + theme_bw() + xlab("") + ylab("") 
g + scale_size_continuous(range=c(10,30)) + geom_text(aes(label = value)) 

重要な部分は、次のとおりです!色=(VAR2 = VAR1)は、geom_point

編集のために(AES)私は美的内にこれを置くことに注意してください:あなたがしたい場合凡例を削除してください(合計でグラフに注釈を付けるので、実際には必要ないと思います)。g + theme(legend.position="none")をremoに追加できますve it

+0

これは、2つの別個の凡例 – PoGibas

関連する問題