Art by Allison Horst
# print prettily as_tibble(mtcars)
## # A tibble: 32 x 11## mpg cyl disp hp drat wt qsec vs am## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>## 1 21 6 160 110 3.9 2.62 16.5 0 1## 2 21 6 160 110 3.9 2.88 17.0 0 1## 3 22.8 4 108 93 3.85 2.32 18.6 1 1## 4 21.4 6 258 110 3.08 3.22 19.4 1 0## 5 18.7 8 360 175 3.15 3.44 17.0 0 0## 6 18.1 6 225 105 2.76 3.46 20.2 1 0## 7 14.3 8 360 245 3.21 3.57 15.8 0 0## 8 24.4 4 147. 62 3.69 3.19 20 1 0## 9 22.8 4 141. 95 3.92 3.15 22.9 1 0## 10 19.2 6 168. 123 3.92 3.44 18.3 1 0## # … with 22 more rows, and 2 more variables: gear <dbl>,## # carb <dbl>
ggplot()
ggplot()
ggplot(mtcars, aes(x = mpg, y = hp))
ggplot(mtcars, aes(x = mpg, y = hp)) + geom_point()
ggplot()
ggplot(data = <data>, mapping = aes(<mapping>)) +
<geom_function>()
ggplot()
ggplot(data = <data>, mapping = aes(<mapping>)) +
<geom_function>()
+
ggplot()
ggplot(data = <data>, mapping = aes(<mapping>)) +
<geom_function>()
+
+
at the end of a lineggplot()
ggplot(data = <data>, mapping = aes(<mapping>)) +
<geom_function>()
+
+
at the end of a lineaes()
diabetes
data.ggplot(data = diabetes, mapping = aes(x = weight, y = hip)) + geom_point()
diabetes <- read_csv("diabetes.csv")ggplot(data = diabetes, mapping = aes(x = weight, y = hip)) + geom_point()
aes()
ggplot(data = <data>, mapping = aes(<mapping>)) +
<geom_function>()
aes()
ggplot(data = <data>, mapping = aes(<mapping>)) +
<geom_function>()
aes()
ggplot(mtcars, aes(x = mpg, y = hp, color = cyl)) + geom_point()ggplot(mtcars, aes(x = mpg, y = hp, size = cyl)) + geom_point()ggplot(mtcars, aes(x = mpg, y = hp, alpha = cyl)) + geom_point()ggplot(mtcars, aes(x = mpg, y = hp, shape = cyl)) + geom_point()
color
, size
, alpha
, and shape
aesthetics to your graph using the gender
variable. Experiment.ggplot( data = diabetes, mapping = aes(x = weight, y = hip)) + geom_point()
geom_point()
. Add in any aesthetics you found helpful.ggplot( data = diabetes, mapping = aes(x = weight, y = hip, color = gender)) + geom_point()
ggplot( data = diabetes, mapping = aes(x = weight, y = hip, size = gender)) + geom_point()
ggplot( data = diabetes, mapping = aes(x = weight, y = hip, alpha = gender)) + geom_point()
ggplot( data = diabetes, mapping = aes(x = weight, y = hip, shape = gender)) + geom_point()
ggplot(data = diabetes) + geom_point( mapping = aes( x = weight, y = hip, color = gender, shape = gender ) )
geom_point()
geom_point()
geom_line()
geom_point()
geom_line()
geom_violin()
geom_point()
geom_line()
geom_violin()
ggplot(diabetes, aes(gender, chol)) + geom_point()
ggplot(diabetes, aes(gender, chol)) + geom_boxplot()
glyhb
variable in diabetes
.glyhb
plot as a density plot.ggplot(diabetes, aes(x = glyhb)) + geom_histogram()
ggplot(diabetes, aes(x = glyhb)) + geom_density()
diabetes %>% ggplot(aes(x = frame)) + geom_bar()
diabetes %>% drop_na() %>% ggplot(aes(x = frame)) + geom_bar()
frame
colored by gender
. Then, try it with the fill
aesthetic instead of color
.diabetes %>% drop_na() %>% ______() + ______()
diabetes %>% drop_na() %>% ggplot(aes(x = frame, fill = gender)) + geom_bar()
geom_bar(position = "<POSITION>")
geom_bar(position = "<POSITION>")
geom_bar(position = "<POSITION>")
geom_bar(position = "<POSITION>")
ggplot(mtcars, aes(x = factor(am), y = hp)) + geom_point()
ggplot(mtcars, aes(x = factor(am), y = hp)) + geom_point(position = "jitter")
ggplot(mtcars, aes(x = factor(am), y = hp)) + geom_jitter(width = .1, height = 0)
fill
aesthetic). Experiment with different position
values: "dodge", "fill", "stack"diabetes %>% drop_na() %>% ggplot(aes(x = frame, fill = gender)) + geom_bar(position = "stack")
diabetes %>% drop_na() %>% ggplot(aes(x = frame, fill = gender)) + geom_bar(position = "dodge")
diabetes %>% drop_na() %>% ggplot(aes(x = frame, fill = gender)) + geom_bar(position = "fill")
geom_point(aes(x, y), color = "blue")
geom_bar(aes(x, y), fill = "blue")
geom_point(aes(x, y), color = "blue")
geom_bar(aes(x, y), fill = "blue")
aes()
ggplot(mtcars, aes(x = mpg, y = hp, color = cyl)) + geom_point(color = "blue")
ggplot(mtcars, aes(x = mpg, y = hp, color = cyl)) + geom_point(aes(color = "blue"))
ggplot(mtcars, aes(x = cyl)) + geom_bar(color = "blue")
ggplot(mtcars, aes(x = cyl)) + geom_bar(fill = "blue")
ggplot(mtcars, aes(x = cyl)) + geom_bar(color = "red", fill = "blue")
ggplot(data = <DATA>, mapping = aes(<MAPPINGS>)) + <GEOM_FUNCTION>() + <GEOM_FUNCTION>() + <SCALE_FUNCTION>() + <THEME_FUNCTION>()
linetype
aesthetic for gender
. Run it again.geom_smooth()
to "black"se = FALSE
to the geom_smooth()
alpha = .2
in geom_point()
?position
argument.theme_bw()
. Remember to use +
.ggplot(diabetes, aes(weight, hip)) + geom_point() + geom_smooth()
ggplot(diabetes, aes(weight, hip)) + geom_point() + geom_smooth()
ggplot(diabetes, aes(weight, hip)) + geom_point() + geom_smooth(aes(linetype = gender))
ggplot(diabetes, aes(weight, hip)) + geom_point() + geom_smooth(aes(linetype = gender), col = "black")
ggplot(diabetes, aes(weight, hip)) + geom_point() + geom_smooth(aes(linetype = gender), col = "black", se = FALSE)
ggplot(diabetes, aes(weight, hip)) + geom_point(alpha = .2) + geom_smooth(aes(linetype = gender), col = "black", se = FALSE)
ggplot(diabetes, aes(weight, hip)) + geom_jitter(alpha = .2) + geom_smooth(aes(linetype = gender), col = "black", se = FALSE)
ggplot(diabetes, aes(weight, hip)) + geom_jitter(alpha = .2) + geom_smooth(aes(linetype = gender), col = "black", se = FALSE) + theme_bw()
facet_grid()
facet_wrap()
facet_grid()
facet_wrap()
facet_grid(
rows = vars(x)
,
cols = vars(y)
)
facet_wrap(
vars(x)
)
diamonds %>% ggplot(aes(x = carat, price)) + geom_point() + facet_grid(rows = vars(cut), cols = vars(clarity))
diamonds %>% ggplot(aes(x = carat, price)) + geom_point() + facet_grid(rows = vars(cut), cols = vars(clarity))
gender
and location
ggplot(diabetes, aes(weight, hip)) + geom_point() + geom_smooth()
ggplot(diabetes, aes(weight, hip)) + geom_point() + geom_smooth() + facet_grid(rows = vars(gender), cols = vars(location))
facet_wrap()
diamonds %>% ggplot(aes(x = carat, price)) + geom_point() + facet_wrap(vars(clarity))
facet_wrap()
library(datasauRus)datasaurus_dozen
## # A tibble: 1,846 x 3## dataset x y## <chr> <dbl> <dbl>## 1 dino 55.4 97.2## 2 dino 51.5 96.0## 3 dino 46.2 94.5## 4 dino 42.8 91.4## 5 dino 40.8 88.3## 6 dino 38.7 84.9## 7 dino 35.6 79.9## 8 dino 33.1 77.6## 9 dino 29.0 74.5## 10 dino 26.2 71.4## # … with 1,836 more rows
datasauRus
package. This package includes a data set called datasaurus_dozen
.x
and y
. First, group it by dataset
, and then summarize with the cor()
function. Call the new variable corr
. What's it look like?corr
. Round it to 2 digits. Then, mutate it again (or wrap it around your first change) using: paste("corr:", corr)
corrs
.datasaurus_dozen
to ggplot()
and add a point geomdataset
.data = corrs
. You also need to use aes()
in this call to set label = corr
, x = 50
, and y = 110
.corrs <- datasaurus_dozen %>% group_by(dataset) %>% summarize(corr = cor(x, y)) %>% mutate( corr = round(corr, 2), corr = paste("corr:", corr) )
corrs <- datasaurus_dozen %>% group_by(dataset) %>% summarize(corr = cor(x, y)) %>% mutate( corr = round(corr, 2), corr = paste("corr:", corr) )
corrs <- datasaurus_dozen %>% group_by(dataset) %>% summarize(corr = cor(x, y)) %>% mutate( corr = round(corr, 2), corr = paste("corr:", corr) )
corrs <- datasaurus_dozen %>% group_by(dataset) %>% summarize(corr = cor(x, y)) %>% mutate( corr = round(corr, 2), corr = paste("corr:", corr) )
corrs
## # A tibble: 13 x 2## dataset corr ## <chr> <chr> ## 1 away corr: -0.06## 2 bullseye corr: -0.07## 3 circle corr: -0.07## 4 dino corr: -0.06## 5 dots corr: -0.06## 6 h_lines corr: -0.06## 7 high_lines corr: -0.07## 8 slant_down corr: -0.07## 9 slant_up corr: -0.07## 10 star corr: -0.06## 11 v_lines corr: -0.07## 12 wide_lines corr: -0.07## 13 x_shape corr: -0.07
datasaurus_dozen %>% ggplot(aes(x, y)) + geom_point() + geom_text(data = corrs, aes(label = corr, x = 50, y = 110)) + facet_wrap(vars(dataset))
datasaurus_dozen %>% ggplot(aes(x, y)) + geom_point() + geom_text(data = corrs, aes(label = corr, x = 50, y = 110)) + facet_wrap(vars(dataset))
datasaurus_dozen %>% ggplot(aes(x, y)) + geom_point() + geom_text(data = corrs, aes(label = corr, x = 50, y = 110)) + facet_wrap(vars(dataset))
Art by Allison Horst
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |