library(tidyverse)
ggplot(iris, aes(x = Petal.Length, y = Petal.Width)) +
geom_point()
Start with just data and coordinates
ggplot(iris, aes(x = Petal.Length, y = Petal.Width))
Add in points to show data, along with color and a linear model
ggplot(iris, aes(x = Petal.Length, y = Petal.Width)) +
geom_point()
ggplot(iris, aes(x = Petal.Length, y = Petal.Width, color = Species)) +
geom_point()
ggplot(iris, aes(x = Petal.Length, y = Petal.Width, color = Species)) +
geom_point() +
geom_smooth(method = "lm")
ggplot(iris, aes(x = Petal.Length, y = Petal.Width)) +
geom_point(aes(color = Species)) +
geom_smooth(method = "lm")
diamonds
ggplot(diamonds, aes(x = carat)) +
geom_histogram()
ggplot(diamonds, aes(x = carat)) +
geom_histogram(bins = 40)
ggplot(diamonds, aes(x = carat)) +
geom_histogram(bins = 10)
ggplot(diamonds, aes(x = carat)) +
geom_histogram(bins = 15)
Color is different from Fill
ggplot(diamonds, aes(x = carat)) +
geom_histogram(bins = 15, color = "black", fill = "steelblue")
ggplot(diamonds, aes(x = cut)) +
geom_bar()
ggplot(diamonds, aes(x = cut)) +
geom_bar(stat = "count")
ggplot(diamonds, aes(x = cut, y = price)) +
geom_boxplot()
ggplot(diamonds, aes(x = cut, y = price)) +
geom_violin()
ggplot(diamonds, aes(x = carat, y = price)) +
geom_point()
ggplot(diamonds, aes(x = carat, y = price)) +
geom_point() +
geom_smooth()
ggplot(diamonds, aes(x = carat, y = price)) +
geom_smooth() +
geom_point()
ggplot(diamonds, aes(x = carat, y = price, color = cut)) +
geom_point() +
geom_smooth()
ggplot(diamonds, aes(x = carat, y = price)) +
geom_point(color = cut) +
geom_smooth()
ggplot(diamonds, aes(x = carat, y = price)) +
geom_point(aes(color = cut)) +
geom_smooth()
ggplot(diamonds, aes(x = carat, y = price)) +
geom_point(color = "orange") +
geom_smooth()
ggplot(diamonds, aes(x = carat, y = price)) +
geom_point(aes(color = "orange")) +
geom_smooth()
ggplot(diamonds, aes(x = carat, y = price)) +
geom_point(aes(color = table))
ggplot(diamonds, aes(x = carat, y = price)) +
geom_point(aes(color = table)) +
labs(
x = "Carat",
y = "Price (USD)",
title = "Price vs. Carat",
subtitle = "Data from ggplot2",
caption = "This is a caption",
color = "Table of Diamond"
)
We can customize the colors used in our plots. For instance, scale_color_continuous allows us to set high and low values
ggplot(diamonds, aes(x = carat, y = price)) +
geom_point(aes(color = table)) +
labs(
x = "Carat",
y = "Price (USD)",
title = "Price vs. Carat",
subtitle = "Data from ggplot2",
caption = "This is a caption",
color = "Table of Diamond"
) +
scale_color_continuous(
low = "yellow",
high = "blue"
)
ggplot(diamonds, aes(x = carat, y = price)) +
geom_point(aes(color = table)) +
labs(
x = "Carat",
y = "Price (USD)",
title = "Price vs. Carat",
subtitle = "Data from ggplot2",
caption = "This is a caption",
color = "Table of Diamond"
) +
scale_color_continuous(
low = "yellow",
high = "blue"
) +
theme_dark()
ggplot(diamonds, aes(x = carat, y = price)) +
geom_point(aes(color = table)) +
labs(
x = "Carat",
y = "Price (USD)",
title = "Price vs. Carat",
subtitle = "Data from ggplot2",
caption = "This is a caption",
color = "Table of Diamond"
) +
scale_color_continuous(
low = "yellow",
high = "blue"
) +
theme_bw()
facet_wrap()ggplot(diamonds, aes(x = carat, y = price)) +
geom_point(aes(color = table)) +
labs(
x = "Carat",
y = "Price (USD)",
title = "Price vs. Carat",
subtitle = "Data from ggplot2",
caption = "This is a caption",
color = "Table of Diamond"
) +
scale_color_continuous(
low = "yellow",
high = "blue"
) +
theme_bw() +
facet_wrap( ~ clarity)
facet_grid()ggplot(diamonds, aes(x = carat, y = price)) +
geom_point(aes(color = table)) +
labs(
x = "Carat",
y = "Price (USD)",
title = "Price vs. Carat",
subtitle = "Data from ggplot2",
caption = "This is a caption",
color = "Table of Diamond"
) +
scale_color_continuous(
low = "yellow",
high = "blue"
) +
theme_bw() +
facet_grid(cut ~ .)
ggplot(diamonds, aes(x = carat, y = price)) +
geom_point(aes(color = table)) +
labs(
x = "Carat",
y = "Price (USD)",
title = "Price vs. Carat",
subtitle = "Data from ggplot2",
caption = "This is a caption",
color = "Table of Diamond"
) +
scale_color_continuous(
low = "yellow",
high = "blue"
) +
theme_bw() +
facet_grid(. ~ cut)
ggplot(diamonds, aes(x = carat, y = price)) +
geom_point(aes(color = table)) +
labs(
x = "Carat",
y = "Price (USD)",
title = "Price vs. Carat",
subtitle = "Data from ggplot2",
caption = "This is a caption",
color = "Table of Diamond"
) +
scale_color_continuous(
low = "yellow",
high = "blue"
) +
theme_bw() +
facet_grid(color ~ cut)
It’s easy to save a plot, especially once you’ve stored it in a variable (like p).
p <- ggplot(diamonds, aes(x = carat, y = price)) +
geom_point(aes(color = table)) +
labs(
x = "Carat",
y = "Price (USD)",
title = "Price vs. Carat",
subtitle = "Data from ggplot2",
caption = "This is a caption",
color = "Table of Diamond"
) +
scale_color_continuous(
low = "yellow",
high = "blue"
) +
theme_bw() +
facet_grid(color ~ cut)
ggplot2 can save in many file formats without difficulty. Here are three:
ggsave(plot = p, filename = "./images/diamonds.png")
ggsave(plot = p, filename = "./images/diamonds.svg")
ggsave(plot = p, filename = "./images/diamonds.eps")
We can define further options, like dpi, within the ggsave() command. Remember that width and height are defined in inches by default.
ggsave(plot = p, filename = "./images/diamonds.png", dpi = 1200, width = 8, height = 6)