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

  • Home
  • SEARCH
  • 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 9172847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:27:04+00:00 2026-06-17T16:27:04+00:00

First question from me, hope it’s phrased correctly. I couldn’t find anything on this

  • 0

First question from me, hope it’s phrased correctly. I couldn’t find anything on this so I’ll bite the bullet.

I’d like to create a geom_tile plot in which for each combination of x and y, I have 5 variables for the z that all sum to one. I’d like the colour of the square for each x-y combination to reflect which z number is largest. For example, imagine I have 5 z variables a, b, c, d and e. If a=1 and b=c=d=e=0, the square would be blue, if b=1 and a=c=d=e=0 it would be red, etc. If a=b=0.5 and the others are zero, it would be an interpolated colour (i.e. purple). Perhaps even better, a crosshatch of red and blue, but I don’t know if that is possible in ggplot.

I am not sure how best to go ahead. I thought about plotting 5 geom_tile plots on top of each other, where each one is a different colour and the alpha is scaled by e.g. the value of a (so the cell is transparent when a = 0 and strongly coloured when a = 1). There might also be a way to do it using colour ramps, but I drew a blank there too.

If anyone can see how to do this off the top of their head, I’d be much obliged. Here’s a dataset of random numbers in the same format as my actual data.

rand <- matrix(runif(50*50*5), ncol=5, nrow=50*50)
rand <- rand / apply(rand, 1, sum) # Make sure the numbers sum to one in each row
d <- data.frame(a = rand[,1],
                b = rand[,2],
                c = rand[,3],
                d = rand[,4],
                e = rand[,5],
                expand.grid(x = 1:50, y = 1:50))

Update: By using the first answer from sebastian-c and my own dataset, I was able to make this figure. I obtained the colours using the function brewer.pal(5,”Spectral”) in the package RColorBrewer. Needs a custom legend, but otherwise I am pretty happy with it. The solid colours show pure “Evolutionarily stable strategies” (ESS; i.e. strategies that cannot be beaten by a mutant strategy). Areas of overlapping colours show mixed ESS, in which the equilibrium is a stable balance of two or more strategies.

Overlapped geom_tile plots ESS

  • 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-17T16:27:05+00:00Added an answer on June 17, 2026 at 4:27 pm

    I took the liberty of simplifying your example a bit down to 3 columns. Honestly, I don’t think this works well with any more than 2, but here’s how I’ve gone about it. I’ve also abused alpha channels so if you lay down the tiles in a different order, the colours change. This should hopefully work as a starting point for someone else. First, we’ll generate data:

    set.seed(100)
    
    rand <- matrix(runif(100*3), ncol=3, nrow=100)
    rand <- rand / apply(rand, 1, sum) # Make sure the numbers sum to one in each row
    d <- data.frame(a = rand[,1],
                    b = rand[,2],
                    c = rand[,3],
                    expand.grid(x=1:10, y=1:10))
    

    Now for the plot:

    ggplot(d, aes(x=x, y=y))+
      theme_bw() + #For a clearer background
      geom_tile(alpha=d$a, fill="red")+
      geom_tile(alpha=d$b, fill="blue")+
      geom_tile(alpha=d$c, fill="green")+
      theme(panel.grid.major=element_blank())
    

    enter image description here

    I was thinking about, for 3 columns, using amounts of red, green and blue, but this doesn’t work for anything greater than 3 columns.

    EDIT: A specific solution for up to 3 columns (I mixed the blue and green up):

    d2 <- data.frame(col=rgb(rand), expand.grid(x=1:10, y=1:10))
    
    ggplot(d2, aes(x=x, y=y)) +
      theme_bw()+
      geom_tile(aes(fill=col))+
      scale_fill_identity()
    

    enter image description here

    You could do this with 2 colours if you set one of red, green or blue to 0.

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

Sidebar

Related Questions

This question arose from the discussion in the comments of this answer . First,
This is my first question here and I hope it is simple enough to
This is my first question here, so I hope that I am providing enough
Since this is my first question here on stackoverflow I hope my question is
this my first question on here. I hope someone can help. It is to
This is my first post on stackoverflow, I hope one of many! My question
This is my first question, so I tried to be clear (hope its not
This is my first question here on stackoverflow, so I hope that I am
Question from a first-time Jenkins user. So I hope you wouldn't mind if the
This is my first question on this forum and I hope u don't blame

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.