── 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.4.4 ✔ 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
nrow(penguins)
[1] 344
ncol(penguins)
[1] 8
344;8
What does the bill_depth_mm variable in the penguins data frame describe? Read the help for ?penguins to find out.
The culmen is the upper ridge of a bird’s bill. In the simplified penguins data, culmen length and depth are renamed as variables bill_length_mm and bill_depth_mm to be more intuitive.
Make a scatterplot of bill_depth_mm vs. bill_length_mm. That is, make a scatterplot with bill_depth_mm on the y-axis and bill_length_mm on the x-axis. Describe the relationship between these two variables.
What does the na.rm argument do in geom_point()? What is the default value of the argument? Create a scatterplot where you successfully use this argument set to TRUE.
There are missing data points (either on the x or the y) so gives a warning in the console along with the plotted chart. Adding na.rm = TRUE into the geom_point() removes these missing value data poins and so there is no longer a warning printed in the console.
Add the following caption to the plot you made in the previous exercise: “Data come from the palmerpenguins package.” Hint: Take a look at the documentation for labs().
penguins |>ggplot(mapping =aes(x = bill_depth_mm,y = body_mass_g)) +geom_point(na.rm =TRUE) +labs(caption ="Data come from the palmerpenguins package.")
Recreate the following visualization. What aesthetic should bill_depth_mm be mapped to? And should it be mapped at the global level or at the geom level?
The chart will have dots with flipper length along the bottom and body mass along the side. The dots will be coloured by the islands and there will be as many curvy lines as there are distinct islands but with no grey extensions/bits to the lines.
Will these two graphs look different? Why/why not?
They are the same because the first has the data and aes() which will be available to the geom() functions. The second is a repetition of the code leaving the global ggplot() empty but giving the same chart.
Exercise 1.4.3
Make a bar plot of species of penguins, where you assign species to the y aesthetic. How is this plot different?
The plot appears to be ‘flipped’ so the species are appearing on the left side (y axis).
Make a histogram of the carat variable in the diamonds dataset that is available when you load the tidyverse package. Experiment with different binwidths. What binwidth reveals the most interesting patterns?
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Bins changes the “buckets” or the number of cuts made to the data
Exercise 1.5.5
The mpg data frame that is bundled with the ggplot2 package contains 234 observations collected by the US Environmental Protection Agency on 38 car models. Which variables in mpg are categorical? Which variables are numerical? (Hint: Type ?mpg to read the documentation for the dataset.) How can you see this information when you run mpg?
Categorical would be manufacture, model, trans, drv, fl and class. Numerical would be displ, year, cyl, cty and hwy.
Make a scatterplot of hwy vs. displ using the mpg data frame. Next, map a third, numerical variable to color, then size, then both color and size, then shape. How do these aesthetics behave differently for categorical vs. numerical variables?
ggplot(mpg, aes(x = displ, y = hwy)) +geom_point()
# third numerical variable, colorggplot(mpg, aes(x = displ, y = hwy)) +geom_point(aes(color = cyl))
Warning: The shape palette can deal with a maximum of 6 discrete values because more
than 6 becomes difficult to discriminate
ℹ you have requested 7 values. Consider specifying shapes manually if you need
that many have them.
When numerical variables are mapped to color or size aesthetics, the values of the numerical variable determine the color or size of points. Higher numerical values often correspond to lighter colors or larger sizes.
When categorical variables are mapped to color, each category is assigned a unique color. This aids in distinguishing between different categories
When categorical variables are mapped to shape, each category is represented by a distinct shape. This approach is valuable for differentiation, particularly if color isn’t distinguishable.
In the scatterplot of hwy vs. displ, what happens if you map a third variable to linewidth?
Mapping a third variable to the linewidth aesthetic in a scatterplot of hwy vs. displ using the mpg dataset would not have any impact, because linewidth aesthetic is typically used for specifying the width of lines in line plots, not for points in scatterplots
What happens if you map the same variable to multiple aesthetics?
It can enhance understanding if each conveys distinct information.
Make a scatterplot of bill_depth_mm vs. bill_length_mm and color the points by species. What does adding coloring by species reveal about the relationship between these two variables? What about faceting by species?
It reveals the relationship of bill depth and length in different species.
Create the two following stacked bar plots. Which question can you answer with the first one? Which question can you answer with the second one?
The first displays the distribution of penguin species on different islands, and answers the question: “What is the distribution of penguin species on each island?”. The second shows the distribution of penguin islands for each species, and snswers the question: “What is the distribution of penguin islands for each species?”
Exercise 1.6.1
Run the following lines of code. Which of the two plots is saved as mpg-plot.png? Why?
The second code is saved because it was the last code run.
What do you need to change in the code above to save the plot as a PDF instead of a PNG? How could you find out what types of image files would work in ggsave()?
To save as a pdf the code needs to be changed from .png to .pdf