Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

conditionally set both fill and color in geom_point ggplot R

what is wrong my code that does not change the filling color?

cars %>%
  ggplot() + 
  geom_point(aes(x = speed, y = dist,
                 color= I(ifelse(dist >50, 'red', 'black')),
                 fill= I(ifelse(dist >50, 'pink', 'gray'))
                 
                 )
             
             )

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to have a point shape that allows both fill and colour.

library(ggplot2)
cars %>%
    ggplot() + 
    geom_point(
        aes(x = speed, y = dist,
            color= I(ifelse(dist >50, 'red', 'black')),
            fill= I(ifelse(dist >50, 'pink', 'gray')), 
        ), 
        shape = 21, 
        size = 4 # changing size so it's easy to visualise
    )

enter image description here

To check point shapes that allow both fill and colour use help(points) and refer to the ‘pch’ values section

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading