ggplot2 - ggplot - Inline Subsetting for categorical variables - ggplot2 tutorial - ggplot tutorial
ggplot(iris[iris$Species == "setosa",],aes(Sepal.Width)) +
geom_density()Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ggplot2 tutorial - ggplot tutorial - ggplot2 - ggplot - learn ggplot2 - ggplot2 examples - team
Here, we are subsetting the dataframe before passing it to ggplot. It is a very useful tool derived from the data frame data structure.
- To make the code more readable, one can also use dplyr's filter:
library(dplyr)
iris %>% filter(Species == "setosa") %>% ggplot(aes(Sepal.Width)) +
geom_density()