Plotting background data for groups
https://drsimonj.svbtle.com/plotting-background-data-for-groups-with-ggplot2
library(ggplot2)
ggplot(iris, aes(x = Sepal.Width)) +
geom_histogram()
ggplot(iris, aes(x = Sepal.Width, fill = Species)) +
geom_histogram()
ggplot(iris, aes(x = Sepal.Width)) +
geom_histogram() +
facet_wrap(~ Species)
#----
d <- iris # Full data set
d_bg <- d[, -5] # Background Data - full without the 5th column (Species)
ggplot(d, aes(x = Sepal.Width)) +
geom_histogram(data = d_bg, fill = "grey") +
geom_histogram() +
facet_wrap(~ Species)
d <- iris # Full data set
d_bg <- d[, -5] # Background Data - full without the 5th column (Species)
ggplot(d, aes(x = Sepal.Width, fill = Species)) +
geom_histogram(data = d_bg, fill = "grey", alpha = .5) +
geom_histogram(colour = "black") +
facet_wrap(~ Species) +
guides(fill = FALSE) + # to remove the legend
theme_bw()
ggplot(d, aes(x = Sepal.Width, y = Sepal.Length, colour = Species)) +
geom_point(data = d_bg, colour = "grey", alpha = .2) +
geom_point() +
facet_wrap(~ Species) +
guides(colour = FALSE) +
theme_bw()
Quick plot of all variables
https://drsimonj.svbtle.com/quick-plot-of-all-variables
library(purrr) #install.packages("purrr")
library(tidyr)
library(ggplot2)
mtcars %>%
keep(is.numeric) %>%
gather() %>%
ggplot(aes(value)) +
facet_wrap(~ key, scales = "free") +
geom_histogram()
#---
d <- mtcars
d$vs <- factor(d$vs)
d$am <- factor(d$am)
library(tidyr)
d %>%
keep(is.numeric) %>% # Keep only numeric columns
gather() %>% # Convert to key-value pairs
ggplot(aes(value)) + # Plot the values
facet_wrap(~ key, scales = "free") + # In separate panels
geom_density()
Plot some variables against many others
https://drsimonj.svbtle.com/plot-some-variables-against-many-others
library(dplyr)
library(tidyr)
mtcars %>%
gather(-mpg, -hp, key = "var", value = "value") %>%
ggplot(aes(x = value, y = mpg, color = hp)) +
geom_point() +
facet_wrap(~ var, scales = "free") +
theme_bw()
mtcars %>%
gather(-mpg, -hp, -cyl, key = "var", value = "value") %>%
ggplot(aes(x = value, y = mpg, color = hp, shape = factor(cyl))) +
geom_point() +
facet_wrap(~ var, scales = "free") +
theme_bw()
mtcars %>%
gather(-mpg, key = "var", value = "value") %>%
ggplot(aes(x = value, y = mpg)) +
geom_point() +
stat_smooth() +
facet_wrap(~ var, scales = "free") +
theme_bw()