── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.5
✔ forcats 1.0.0 ✔ stringr 1.5.1
✔ ggplot2 3.5.1 ✔ tibble 3.2.1
✔ lubridate 1.9.3 ✔ tidyr 1.3.1
✔ purrr 1.0.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library (modeldata)
?ggplot
starting httpd help server ... done
?crickets
View (crickets)
ggplot (crickets,aes (x= temp,
y= rate,
color= species))+
geom_point ()+
labs (x= "Temperature" ,
y= "Chirp rate" ,
caption= "McDonald (2009)" )
# The basics
ggplot (crickets, aes (x = temp,
y = rate)) +
geom_point () +
labs (x = "Temperature" ,
y = "Chirp rate" ,
title = "Cricket chirps" ,
caption = "Source: McDonald (2009)" )
ggplot (crickets, aes (x = temp,
y = rate,
color = species)) +
geom_point () +
labs (x = "Temperature" ,
y = "Chirp rate" ,
color = "Species" ,
title = "Cricket chirps" ,
caption = "Source: McDonald (2009)" ) +
scale_color_brewer (palette = "Dark2" )
# Modifiying basic properties of the plot
ggplot (crickets, aes (x = temp,
y = rate)) +
geom_point (color = "red" ,
size = 2 ,
alpha = .3 ,
shape = "square" ) +
labs (x = "Temperature" ,
y = "Chirp rate" ,
title = "Cricket chirps" ,
caption = "Source: McDonald (2009)" )
# Learn more about the options for the geom_abline()
# with ?geom_point
# Adding another layer
ggplot (crickets, aes (x = temp,
y = rate)) +
geom_point () +
geom_smooth (method = "lm" ,
se = FALSE ) +
labs (x = "Temperature" ,
y = "Chirp rate" ,
title = "Cricket chirps" ,
caption = "Source: McDonald (2009)" )
`geom_smooth()` using formula = 'y ~ x'
ggplot (crickets, aes (x = temp,
y = rate,
color = species)) +
geom_point () +
geom_smooth (method = "lm" ,
se = FALSE ) +
labs (x = "Temperature" ,
y = "Chirp rate" ,
color = "Species" ,
title = "Cricket chirps" ,
caption = "Source: McDonald (2009)" ) +
scale_color_brewer (palette = "Dark2" )
`geom_smooth()` using formula = 'y ~ x'
# Other plots
ggplot (crickets, aes (x = rate)) +
geom_histogram (bins = 15 ) # one quantitative variable
ggplot (crickets, aes (x = rate)) +
geom_freqpoly (bins = 15 )
ggplot (crickets, aes (x = species)) +
geom_bar (color = "black" ,
fill = "lightblue" )
ggplot (crickets, aes (x = species,
fill = species)) +
geom_bar (show.legend = FALSE ) +
scale_fill_brewer (palette = "Dark2" )
ggplot (crickets, aes (x = species,
y = rate,
color = species)) +
geom_boxplot (show.legend = FALSE ) +
scale_color_brewer (palette = "Dark2" ) +
theme_minimal ()
?theme_minimal ()
# faceting
# not great:
ggplot (crickets, aes (x = rate,
fill = species)) +
geom_histogram (bins = 15 ) +
scale_fill_brewer (palette = "Dark2" )
ggplot (crickets, aes (x = rate,
fill = species)) +
geom_histogram (bins = 15 ,
show.legend = FALSE ) +
facet_wrap (~ species) +
scale_fill_brewer (palette = "Dark2" )
?facet_wrap
ggplot (crickets, aes (x = rate,
fill = species)) +
geom_histogram (bins = 15 ,
show.legend = FALSE ) +
facet_wrap (~ species,
ncol = 1 ) +
scale_fill_brewer (palette = "Dark2" ) +
theme_minimal ()