基于定义的颜色代码的颜色ggplot点 [英] Color ggplot points based on defined color codes

查看:2125
本文介绍了基于定义的颜色代码的颜色ggplot点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以根据数据框中包含的预定义标准颜色代码使用ggplot来绘制颜色点?

Is it possible to used ggplot to color points based on predefinded standard color codes contained in the data frame?

下面是一些示例数据和代码,

Below is some sample data and code to help articulate my question.

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

任何指导都将非常感激。

Any guidance would be greatly appreciated.

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


推荐答案

工作:

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

适用于我,虽然我想你错过了一个数字在您的十六进制颜色之一。一旦你弄清楚了,你只需要使用 scale_colour_manual

works for me, although I think you missed a digit in one of your hex colors. Once you have that straightened out, you just want to use scale_colour_manual with something like this:

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)

这篇关于基于定义的颜色代码的颜色ggplot点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆