library(tidyverse) library(edr)
ggplot(data = dmd, mapping = aes(x = carats, y = price))
geom_point()
adds a layer of points.ggplot(data = dmd, mapping = aes(x = carats, y = price)) + geom_point()
ggplot(data = dmd, mapping = aes(x = carats, y = depth)) + geom_point()
ggplot(dmd, mapping = aes(x = carats, y = price, shape = clarity)) + geom_point()
alpha
argument in geom_point()
.ggplot(dmd, mapping = aes(x = carats, y = price, shape = clarity)) + geom_point(alpha = 0.25)
color
inside of geom_point()
.ggplot(dmd, mapping = aes(x = carats, y = price)) + geom_point(color = "gray50")
ggplot(dmd, aes(x = carats, y = price)) + geom_point(mapping = aes(color = cut, shape = clarity))
ggplot(dmd, aes(x = carats, y = price)) + geom_point(mapping = aes(size = depth), alpha = 0.05)
ggplot(dmd, aes(x = carats, y = price)) + geom_point(color = "gray50", fill = "#AAAFEF", shape = 23)
ggplot(dmd, aes(x = carats, y = price)) + geom_point(mapping = aes(color = depth, shape = clarity))
facet_wrap()
gives us a faceted plot.ggplot(dmd, aes(x = carats, y = price)) + geom_point() + facet_wrap(facets = vars(clarity))
cut
and clarity
.ggplot(dmd, aes(x = carats, y = price)) + geom_point() + facet_wrap(facets = vars(cut, clarity))
label_both
to create informative labels for facets.ggplot(dmd, aes(x = carats, y = price)) + geom_point() + facet_wrap(facets = vars(cut, clarity), labeller = label_both)
nrow
argument.ggplot(dmd, aes(x = carats, y = price)) + geom_point() + facet_wrap( facets = vars(cut, clarity), nrow = 1, labeller = label_both )
facet_grid()
provides a slightly different visualization of the faceted plot panels.ggplot(dmd, aes(x = carats, y = price)) + geom_point(alpha = 0.2) + facet_grid(rows = vars(cut), cols = vars(clarity), labeller = label_both)
labs()
function gives us the opportunity to provide our own labels for different plot elements.ggplot(dmd, mapping = aes(x = carats, y = price)) + geom_point(mapping = aes(shape = clarity)) + labs( x = "Weight of the Diamond (carats)", y = "Price (USD)", shape = "Diamond Clarity" )
labs()
as well.ggplot(dmd, mapping = aes(x = carats, y = price)) + geom_point(mapping = aes(shape = clarity)) + labs( title = "The Relationship Between Diamond Weight on Price", subtitle = "Quality of diamond clarity is indicated by shape", caption = "Data taken from the `dmd` dataset", x = "Weight of the Diamond (carats)", y = "Price (USD)", color = "Diamond Cut", shape = "Diamond Clarity" )
legend.position
argument of theme()
to put the legend to the right of the plot.ggplot(dmd, aes(x = carats, y = price)) + geom_point(aes(shape = clarity)) + labs(shape = "Clarity") + theme(legend.position = "right")
legend.position
argument of theme()
to put the legend below the plot.ggplot(dmd, aes(x = carats, y = price)) + geom_point(aes(shape = clarity)) + labs(shape = "Clarity") + theme(legend.position = "bottom")
legend.justification
argument of theme()
to justify the legend toward the top of the visualization.ggplot(dmd, aes(x = carats, y = price)) + geom_point(aes(shape = clarity)) + labs(shape = "Clarity") + theme(legend.justification = "top")
legend.position
argument of theme()
to remove the legend entirely.ggplot(dmd, aes(x = carats, y = price)) + geom_point(aes(shape = clarity)) + labs(shape = "Clarity") + theme(legend.position = "none")
dmd
to obtain two new columns: cpc
and price_class
.dmd_mod <- dmd %>% mutate(cpc = price / carats) %>% mutate(price_class = ifelse(cpc >= 3460, "Above Median", "Below Median"))
mutate()
statement to add the quality
column to our modified dataset (dmd_mod
).dmd_mod <- dmd %>% mutate(cpc = price / carats) %>% mutate(price_class = ifelse(cpc >= 3460, "Above Median", "Below Median")) %>% mutate(quality = ifelse( cut == "The Best" & clarity == "The Best", "Top Drawer", "The Rest") )
dmd
to add three new columns, and, plotting dmd_mod
with the new variables.dmd_mod <- dmd %>% mutate(cpc = price / carats) %>% mutate(price_class = ifelse(cpc >= 3460, "Above Median", "Below Median")) %>% mutate(quality = ifelse( cut == "The Best" & clarity == "The Best", "Top Drawer", "The Rest") ) ggplot(dmd_mod, aes(x = carats, y = price)) + geom_point() + facet_wrap( facets = vars(price_class, quality), labeller = label_both ) + labs(x = "Carats", y = "Price")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.