Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Mapping Variables to Aesthetics | section
Hands-On Data Visualization with ggplot2 in R
Osio 1. Luku 2
single

single

bookMapping Variables to Aesthetics

Pyyhkäise näyttääksesi valikon

When using ggplot2 in R, aesthetic mappings are the way you connect your data variables to visual properties of your plot. These properties, called aesthetics, include features like position on the x- and y-axes, color, size, shape, and more. By mapping variables to these aesthetics, you make your data visually meaningful: for example, mapping a variable to color can help you distinguish groups, while mapping to size can show magnitude. Aesthetic mappings are set inside the aes() function, either globally in ggplot() or locally within a specific geom layer. This approach allows you to quickly explore relationships and patterns in your data, making your plots both informative and visually appealing.

12345678910111213
library(ggplot2) # Sample data df <- data.frame( height = c(150, 160, 170, 180, 190), weight = c(55, 65, 72, 80, 90), gender = c("Female", "Male", "Female", "Male", "Male"), age = c(23, 35, 29, 41, 33) ) # Scatter plot mapping variables to aesthetics ggplot(df, aes(x = height, y = weight, color = gender, size = age)) + geom_point()
copy

Changing which variables are mapped to which aesthetics can dramatically alter both the appearance of your plot and the insights you can draw from it. For instance, mapping gender to color allows you to easily compare groups, while mapping age to size adds an extra dimension, showing how another variable relates to the others. If you swap these mappings - say, mapping age to color and gender to size - the visual emphasis and the story your plot tells will change. Thoughtful aesthetic mapping helps you highlight patterns, identify outliers, and communicate your findings more effectively.

Tehtävä

Pyyhkäise aloittaaksesi koodauksen

Use the provided data data frame to create a scatter plot with ggplot2.

  • Map hours_studied to the x-axis.
  • Map exam_score to the y-axis.
  • Map student_group to the color aesthetic.
  • Map assignments_completed to the size aesthetic.
  • Add a point layer to display the data.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 2
single

single

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

some-alt