-1

For some reason rgb(0,0,0) is returning red for me. The Mac color picker utility says it's shade rgb(248, 118, 109). I have my maxColorValue properly set, but that shouldn't matter for rgb(0,0,0). I'm stumped as to what's going wrong. The only thing I can think of is that somehow there is a k channel that is messed up but I haven't found an option for it in the rgb() function.

library(tidyverse)

cars %>%
  filter(speed < 5) %>%
  mutate(time = dist / speed) %>%
  group_by(time, dist, speed) %>%
  ggplot(aes(x = dist, y = speed, fill = rgb(0,0,0))) +
  geom_tile() +
  facet_grid(~time)

Screenshot of the resulting graph

  • 1
    What code are you running exactly? Where are you using the color value? It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. – MrFlick Jul 12 '20 at 01:04
  • @MrFlick Sorry about that. I'm still getting the hang of StackOverflow. I added a reproducible sample. – MeepMorpRobotVoice Jul 12 '20 at 02:44
  • If you want to hard code a value to a property, you need to do it outside of the aes(). Move it to `geom_tile(fill=rgb(0,0,0))`. The aes() is for mapping values to your data. – MrFlick Jul 12 '20 at 02:51
  • Thanks. I just figured that out myself and wrote up my own answer. Is there a reason why some functions use color inside aes and others use fill outside or is it arbitrary? I'm mainly thinking of geom_point which lets me place color inside of ggplot(aes()) – MeepMorpRobotVoice Jul 12 '20 at 02:58
  • It’s not arbitrary. If you just want to set a value that’s not tired to data, do it outside the aes(). If you are mapping data to a value, do it inside the aes(). How values from the mapping are determined by the scales of the plot. – MrFlick Jul 12 '20 at 03:00
  • Thank you for helping me understand. I'm confused because in my case I needed geom_tile(fill = rgb) but if I wanted to chart cars %>% ggplot(aes(x = speed, y = dist)) + geom_point(aes(color = speed)) Then fill doesn't work and I have to use color which does go in aes(). Is there a resource where I can learn more about when to use color vs fill? – MeepMorpRobotVoice Jul 12 '20 at 03:06

1 Answers1

1

The solution is to move fill = rgb() from ggplot(aes(rgb())) to geom_tile() without an aes. The resulting code looks like this:

library(tidyverse)

cars %>%
  filter(speed < 5) %>%
  mutate(time = dist / speed) %>%
  group_by(time, dist, speed) %>%
  ggplot(aes(x = speed, y = speed)) +
  geom_tile(fill = rgb(0,0,0)) +
  facet_grid(~time)

If anyone is able to help me understand why my original code didn't work and why I shouldn't use aes(fill) in geom_tile would be greatly appreciated.