2012-03-22 7 views
2

データフレームに含まれるあらかじめ定義された標準カラーコードに基づいてカラーポイントにカラー表示することは可能ですか?定義済みのカラーコードに基づいたカラーggplotポイント

以下は、私の質問を明確にするのに役立つサンプルデータとコードです。

tick <- c("GE","APPL","GM","BTU","WMT","JPM","LUV") 
price <- c(22,900,20,22,80,31,35) 
volume<- c(300,500,100,107,400,300,325) 
df1 <- data.frame(ticker=tick, price=price, volume=volume) 

# Here is a sample chart without colors: 
p <- ggplot(df1, aes(volume, price))+ 
    geom_point(); 
p 

# I could use astetics and color_brewer to color points by ticker. 
# But since I want to have my colors uniform across multiple plots 
# outside of this script, I have specified the colors to always 
# be used for certian tickers 

## color speciciations 
## http://wiki.stdout.org/rcookbook/Graphs/Colors%20(ggplot2)/#rcolorbrewer-palette-chart 

tick<-c("GE","APPL","GM","BTU","WMT") 
ccodes<-c("#3399FF", "#FF000", "#CC00FF", "#993300", "#66CC00") 
cnames<-c("blue", "red", "purple", "brown", "green") 
df2=data.frame(ticker=tick, color.codes=ccodes, color.names=cnames) 

## merge color specifcations into data 
df3 <-merge(df1,df2, by=("ticker"), all.x=TRUE, all.y=TRUE) 

# since I wont be able to specify colors for all the data will be 
# be plotting I need to speficy a default color, in this case black. 
# this is where I start to run into trouble. For some reason the 
# following line dosent work as i would have intended as it dosent 
# correctly bring back the defined colors. 
df3$color.code.new <- ifelse(is.na(df3$color.codes), "#000000", df3$color.codes) 

# Once that is corrected, I would like to use the new color codes 
# in df3 as the colors of the points. 
p <- ggplot(df3, aes(volume, price))+ 
    geom_point(); 
p 

ご指摘いただければ幸いです。

##################################################################### 
##### Edit below - to test 
##################################################################### 
ccodes<-c("#990000", "#990000", "#990000", "#990000", "#990000") 
+0

'P + AES(色= color.names)+ scale_colour_identityを()'動作するようです。 – baptiste

答えて

5

あなたが主張行は動作しませんでした:私はあなたの六角色のいずれかで桁を逃したと思いますが、

df3$color.code.new <- ifelse(is.na(df3$color.codes), "#000000", df3$color.codes) 

は、私のために動作します。あなたがまっすぐにすることをしたら、あなたはこのようなものでscale_colour_manualを使用したい:

tick <- c("GE","APPL","GM","BTU","WMT","JPM","LUV") 
price <- c(22,900,20,22,80,31,35) 
volume<- c(300,500,100,107,400,300,325) 
df1 <- data.frame(ticker=tick, price=price, volume=volume) 

tick<-c("GE","APPL","GM","BTU","WMT") 
ccodes<-c("#3399FF", "#FF0000", "#CC00FF", "#993300", "#66CC00") 
cnames<-c("blue", "red", "purple", "brown", "green") 
df2=data.frame(ticker=tick, color.codes=ccodes, color.names=cnames) 

## merge color specifcations into data 
df3 <-merge(df1,df2, by=("ticker"), all.x=TRUE, all.y=TRUE) 
df3$color.code.new <- ifelse(is.na(df3$color.codes), "#000000", df3$color.codes) 

p <- ggplot(df3, aes(volume, price,colour = ticker))+ 
     geom_point() 
p + scale_colour_manual(breaks = df3$ticker,values = df3$color.code.new) 

enter image description here

+0

ありがとうございます。何らかの理由で私がデフォルトを設定するために行を実行すると、私は次のようになります: "5" "3" "1" "4" "#000000" "#000000" "2"デフォルトでは問題ありませんが、 。 – MikeTP

+1

@MikeTPああ、おそらくそれが要因だから。 (私は通常 'options(stringsAsFactors = FALSE)'を設定します)。代わりに 'as.character(df3 $ color.codes)'を試してください。 – joran

+0

ありがとうございます...まだ定義されているのではなく、デフォルトの色を返すように見えるので、期待どおりに動作しません。私は戻って、すべての色のコードを単純なものに変更しました(上の編集を参照してください)。 – MikeTP

関連する問題