ASSESSMENT 3

NIYATI
Assignment3.docx

Assignment 3

Annotation with ggplot2

1 – What’s gone wrong with this code? Why are the points not blue?

ggplot(data = mpg) +

geom_point(mapping = aes(x = displ, y = hwy, color = "blue"))

2 – Which variables in mpg are categorical? Which variables are continuous? (Hint: type ?mpg to read the documentation for the dataset). How can you see this information when you run mpg?

We can type ?mpg and deduce which variables are categorical and which are continuous based on the descriptions. Or we can use str() to get the types of the variables.

3 – Map a continuous variable to colorsize, and shape. How do these aesthetics behave differently for categorical vs. continuous variables?

When mapping a continuous variable, displ, to color, ggplot creats a gradient color scale to represent the values of the continous variable. By default, ggplot creates a color gradient scale from light blue to dark blue, where light blue reresents lower values and dark blue represents higher values.

Similiarly, when mapping a continuous variable to shape, ggplot displays larger values with circles with larger area.

ggplot(data = mpg) +

geom_point(mapping = aes(x = displ, y = hwy, size = displ))

4 – What happens if you map the same variable to multiple aesthetics?

We can map the same variable to multiple aesthetics, as long as the the aesethetics are compatiable with the type of the variables (categorical/continuous). For example, we can map drv, which is a categorical variable, to both color and shape.

5 – What does the stroke aesthetic do? What shapes does it work with? (Hint: use ?geom_point)

stroke only works with shapes 21 – 24, which also have a fill argument, which controls the color of the fill. size argument controls the size of the fill part, stroke controls the size of the stroke, and color contools the color of the stroke. For example:

6 – What happens if you map an aesthetic to something other than a variable name, like aes(colour = displ < 5)?

ggplot(data = mpg) +

geom_point(mapping = aes(x = displ, y = hwy, color = displ < 5))