There is something called “code-link” which is supposed to include hyperlinks to online functions within the code; however, I was unable to successfully implement this code for some reason.
Now we will change the way the scatterplot above looks by using fig-width and fig-height, as seen in the code.
#| label: fig-scatterplot
#| fig-cap: “City and highway mileage for 38 popular models of cars.”
#| fig-alt: “Scatterplot of city vs. highway mileage for cars, where points are colored by the number of cylinders. The plot displays a positive, linear, and strong relationship between city and highway mileage, and mileage increases as the number of cylinders decreases.”
#| fig-width: 6
#| fig-height: 3.5
Code
ggplot(mpg, aes(x = hwy, y = cty, color = cyl)) +geom_point(alpha =0.5, size =2) +scale_color_viridis_c() +theme_minimal()ggplot(mpg, aes(x = hwy, y = cty, color = displ)) +geom_point(alpha =0.5, size =2) +scale_color_viridis_c(option ="E") +theme_minimal()
(a) Color by number of cylinders
(b) Color by engine displacement, in liters
Figure 1: City and highway mileage for 38 popular models of cars.
Week 3 Data - 16 October 2024:
Code
library(tidyverse)
Today we are working with the diamonds dataset.
Code
view(diamonds) #this allows us to view the table of the diamonds dataset str(diamonds) #this allows us to view the structure of the diamonds dataset. The "structure" is the type of data being used; for example, numerical, letter characters, boolean (True or False), categorical,etc.
#For fun, let's see if we can see the structure of the palmerpenguins dataset. library(palmerpenguins)view(penguins)ggplot(penguins, aes(x = flipper_length_mm, y = bill_length_mm)) +geom_point(aes(color = species, shape = species)) +scale_color_manual(values =c("darkorange","purple","cyan4")) +labs(title ="Flipper and bill length",subtitle ="Dimensions for penguins at Palmer Station LTER",x ="Flipper length (mm)", y ="Bill length (mm)",color ="Penguin species", shape ="Penguin species" ) +theme_minimal()
Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).
Code
str(penguins) #After we created the plot from the data, we were able to see the structure of the data.
tibble [344 × 8] (S3: tbl_df/tbl/data.frame)
$ species : Factor w/ 3 levels "Adelie","Chinstrap",..: 1 1 1 1 1 1 1 1 1 1 ...
$ island : Factor w/ 3 levels "Biscoe","Dream",..: 3 3 3 3 3 3 3 3 3 3 ...
$ bill_length_mm : num [1:344] 39.1 39.5 40.3 NA 36.7 39.3 38.9 39.2 34.1 42 ...
$ bill_depth_mm : num [1:344] 18.7 17.4 18 NA 19.3 20.6 17.8 19.6 18.1 20.2 ...
$ flipper_length_mm: int [1:344] 181 186 195 NA 193 190 181 195 193 190 ...
$ body_mass_g : int [1:344] 3750 3800 3250 NA 3450 3650 3625 4675 3475 4250 ...
$ sex : Factor w/ 2 levels "female","male": 2 1 1 NA 1 2 1 2 NA NA ...
$ year : int [1:344] 2007 2007 2007 2007 2007 2007 2007 2007 2007 2007 ...