2012-03-22 101 views
2

是否可以使用ggplot根據數據框中包含的預定義標準顏色代碼對顏色點進行着色?基於定義顏色代碼的顏色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 2012-03-22 22:35:04

回答

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 2012-03-22 17:47:52

+1

@MikeTP啊,可能是因爲這是一個因素。 (我通常設置'options(stringsAsFactors = FALSE)')。試試'as.character(df3 $ color.codes)'代替。 – joran 2012-03-22 17:50:34

+0

謝謝...仍然沒有按預期工作,因爲它似乎返回默認顏色,而不是定義。我回去並將所有顏色代碼更改爲簡單(請參閱上面的編輯),以確保我沒有任何錯誤在我的十六進制中。 – MikeTP 2012-03-22 18:05:51