tidyverse / ggplot2

An implementation of the Grammar of Graphics in R
https://ggplot2.tidyverse.org
Other
6.52k stars 2.03k forks source link

Adjusting scatter edge colors when using scatter and line plot at the same time #6164

Closed Breeze-Hu closed 2 weeks ago

Breeze-Hu commented 2 weeks ago

I'm having some problems when I'm approximating the reproduction of the image below.

scale_color_... This command adjusts both the color of the line and the color of the edge of the point (shape = 21).

image

To achieve a similar effect, I made some combinations of type points. The code is as follows:

library(tidyverse)

data <- tibble(lat = c(38:46),
               tot = 20+runif(9),
               week = 10+runif(9,min = 0,max = 5))

data_tidy <- data |> 
  pivot_longer(cols = !lat,names_to = 'class',values_to = 'value')

ggplot(data = data_tidy, aes(x = value, y = lat))+
  geom_line(aes(color = class),orientation = 'y')+
  geom_point(aes(fill = class),color = 'NA', size =3, shape =21)+
  geom_point(aes(color = class))+
  scale_color_manual(values = c('red','blue'))+
  scale_fill_manual(values = c('yellow','green'))

But I'm not sure there is a simpler and more direct way to achieve this effect.

I would like to achieve that the color of points (fill color) and lines is determined by a certain type of attribute, but the color of the edges of the points depends on this attribute but is at the same time different from the fill.

smouksassi commented 2 weeks ago

I don't see a problem with this approach

just for your info there is a way to have a new color scale ( if you want to map something different for the line layer versus the point layer https://eliocamp.github.io/ggnewscale/ https://teunbrand.github.io/ggh4x/articles/Miscellaneous.html

library(tidyverse)
set.seed(165465)
data <- tibble(lat = c(38:46),
               tot = 20+runif(9),
               week = 10+runif(9,min = 0,max = 5))

data_tidy <- data |> 
  pivot_longer(cols = !lat,names_to = 'class',values_to = 'value')

ggplot(data = data_tidy, aes(x = value, y = lat))+
  geom_line(aes(color = class),orientation = 'y',linewidth = 6)+
  geom_point(aes(fill = class),color = "transparent", size = 9, shape =21)+
  geom_point(aes(color = class),fill = "transparent", size = 8, shape =21)+
  scale_color_manual(values = c('red','blue'))+
  scale_fill_manual(values = c('yellow','green'))

Created on 2024-10-28 with reprex v2.1.1

Breeze-Hu commented 2 weeks ago

@smouksassi Thank you for the information, this is indeed a solution to my problem!

https://eliocamp.github.io/ggnewscale/
This provides another solution path to adjust the color of the 'ring' around the point.

teunbrand commented 2 weeks ago

Yes, I'd also say that ggnewscale is the way to go for this type of problem. As this isn't a bug report or feature request, I'm going to close this issue. I'll also leave a kind reminder that stackoverflow and the posit community are better venues for getting regular usage questions answered.

Breeze-Hu commented 2 weeks ago

I'm sorry. As an R newbie to this kind of problem I face, I'm sometimes not quite sure if it's a new feature that needs to be improved. Again, I apologize for any interruptions this may cause.