Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Breeze-Hu opened this issue Oct 28, 2024 · 4 comments

Comments

@Breeze-Hu
Copy link

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
Copy link

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
Copy link
Author

@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
Copy link
Collaborator

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants