Is there a method of filtering within ggplot
itself? That is, say I want to do this
p <- ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, species)) +
geom_point(size = 4, shape = 4) +
geom_point(size = 1, shape = 5 # do this only for data that meets some condition. E.g. Species == "setosa")
I know there are hacks I can use like setting the size = 0 if Species != "setosa"
or resetting the data like shown below, but there's all hacks.
p <- ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, species)) +
geom_point(size = 4, shape = 4) +
geom_point(data = iris %>% filter(Species == "setosa"), colour = "red") +
geom_point(data = iris %>% filter(Species == "versicolor"), shape = 5)
Basically, i have a chart where certain things should be displayed only if a certain criteria is met, and right now, I'm using the hack above to accomplish this and it's keeping me up at night, my soul slowly dying from the mess I've created. Needless to say, any help would be very much appreciated!
Edit
I'm afraid my example may have been too simplistic. Basically, given ggplot(data = ...)
, how do I add these layers, all using the data bound to the ggplot obj:
Critera #1 and #2 could be anything. E.g. label only outlier points. Draw in red only those points which are outside a specific range, etc.
I don't want to
ggplot(data=subset(iris, Species=="setosa"),...)
or ggplot(data=filter(iris,Species=="setosa")
.apparently layers now accept a function as data argument, so you could use that
pick <- function(condition){
function(d) d %>% filter_(condition)
}
ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, species)) +
geom_point(size = 4, shape = 4) +
geom_point(data = pick(~Species == "setosa"), colour = "red") +
geom_point(data = pick(~Species == "versicolor"), shape = 5)
You can filter data with an anonymous function using the ~
formula notation:
library(ggplot2)
library(dplyr)
ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, species)) +
geom_point(size = 4, shape = 4) +
geom_point(data = ~filter(.x, Species == "setosa"), colour = "red") +
geom_point(data = ~filter(.x, Species == "versicolor"), shape = 5)
Created on 2021-11-15 by the reprex package (v2.0.0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With