Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to add ggplot aesthetic without re-creating or modifying a geom layer?

Tags:

r

ggplot2

I would like to dynamically modify/create an aesthetic without recreating a geom layer. Following is my code.

library("ggplot2")
dat <- data.frame(A = c(0,1,2,5,4,3),
                  B = c(4,1,3,2,0,5),
                  )
ggplot(data = dat) + geom_point(mapping = aes(x = A, y = B))

Now, I want to add a color aesthetic without recreating a geom_point layer.

ggplot(data = dat) + geom_point(mapping = aes(x = A, y = B, color = A>3)) 

Here I have to modify the geom_point to add an aesthetic. The idea is to have the dots always plotted and dynamically modify the colours. Is it possible do this without recreating or modifying a geom layer?

like image 609
penguin Avatar asked Nov 25 '25 09:11

penguin


1 Answers

I believe you can simply do:

ggplot(data = dat) + geom_point(mapping = aes(x = A, y = B)) + aes(color = A>3)

enter image description here

like image 64
Mike H. Avatar answered Nov 26 '25 23:11

Mike H.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!