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 8463379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:26:17+00:00 2026-06-10T14:26:17+00:00

I am having a hard time getting different colours for my 2 different facet_grids.

  • 0

I am having a hard time getting different colours for my 2 different facet_grids.

Here is my simple example:

df <- data.frame(x = rep(1:20, each=20), y= rep(1:20, 20),
             val = sample(0:1,400, replace=TRUE),
             facet_grid = c(rep("A", 200), rep("B", 200)))
colors <- c(1,2)
ggplot(df, aes(x = x, y = y)) +
    geom_tile(aes(fill = val)) +
    facet_grid(. ~ facet_grid, scales = "free") +
    scale_fill_gradient(limits=c(0,1), low=brewer.pal(11, "RdYlGn")[colors][1],  
                        high="grey30")

I am using the scale_fill_gradient function with the brewer.pal function to override the standard colours used. However, what I am trying to achieve is set in the two different facet_grids for values that are 1 the same colour (grey30) should be used. For values that are 0 in the first facet_grig, for instance, red should be used and in the second facet_grid green should be used.

Can anybody give me a hint on how to achieve that?

I created another example which is now closer to want I want but I do not understand why scale_fill_identity does not work. For that I slightly changed the data frame so that value has now 4 different values:

df <- data.frame(x = rep(1:20, each=20), y= rep(1:20, 20),
             val = c(sample(0:1,200, replace=TRUE), sample(2:3,200, replace=TRUE)),
         facet_grid = c(rep("A", 200), rep("B", 200)))

ggplot(df, aes(x = x, y = y)) +
    geom_tile(aes(fill = val)) +
    facet_grid(. ~ facet_grid, scales = "free")

ggplot(df, aes(x = x, y = y)) +
    geom_tile(aes(fill = val)) +
    facet_grid(. ~ facet_grid, scales = "free")

scale_fill_identity(labels=letters[1:4], breaks=c("red","blue","green","brown")) +

The first ggplot gives me almost what I want. 4 different colours in the two different facet_grids. I tried to use scale_fill_identity to change the 4 colours but what I get in my plot are 4 different colours black/white and red/green and not the ones specified above.

  • 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-06-10T14:26:18+00:00Added an answer on June 10, 2026 at 2:26 pm

    try this

    ggplot(df, aes(x = x, y = y),colour=val) +
      geom_tile(aes(fill = factor(val))) +
      facet_grid(. ~ facet_grid, scales = "free")+
      scale_fill_manual(labels=letters[1:4], values=c("red","blue","green","brown")) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having hard time getting hasErrors to work with indexed properties. For example
I am having a hard time getting the correct data out of an array.
See here for code: http://jsfiddle.net/ecFBK/10/ I'm having a hard time getting this to work
I'm having some hard time getting PHP APC to work. Here's my test code:
I'm having a hard time getting my head around this, and Google just isn't
I'm having a hard time getting my AJAX requests to work on a staging
I'm having a hard time getting SetSystemTime working in my C# code. SetSystemtime is
I am having a hard time getting cxf to validate an xml request that
I'm having a hard time getting this to work, and I'm hopelessly confused with
i'm having a hard time getting this to work, all I need to do

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.