Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7683673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:50:55+00:00 2026-05-31T18:50:55+00:00

Is it possible to used ggplot to color points based on predefinded standard color

  • 0

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")
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:50:56+00:00Added an answer on May 31, 2026 at 6:50 pm

    The line you claimed didn’t work:

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

    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)
    

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used glade to create some gtk buttons. is it possible to add an
Is it possible to prevent new from being used at certain points in the
I've long been under the impression that goto should never be used if possible.
Possible Duplicate: Singleton: How should it be used Following on from Ewan Makepeace 's
Is it possible to change the font size used in a ContextMenu using the
Possible Duplicate: What are the obj and bin folders (created by Visual Studio) used
Is it possible for the compiler to remove statements used for debugging purposes (such
Is it possible to store encrypted connection string so it can be used from
Is it possible to determine which submit button was used? I have a confirmation
Is it possible to create a Silverlight application which can be used to provide

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.