Data Visualization (flash cards)

Question 1

What three things does every ggplot() need?

  • data, the data frame to plot
  • mapping, which columns correspond to which parts of the plot
  • a geom_ to represent the data

Question 2

What function do you use to build the mapping argument?

aes()

aes() stands for “aesthetic”

Question 3

df is short for Data Frame and represents the data table in the remaining cards

How do you make a scatter plot of col_name_1 on the x axis and col_name_2 on the y axis?

ggplot(data = df, mapping = aes(x = col_name_1, y = col_name_2)) +
  geom_point()

Question 4

What symbol combines the pieces of a plot together?

+

geoms, scales, and facets are added to the plot with +.

Question 5

How do you set the color of all points for a layer?

Set color as an argument to the geom

ggplot(data = df, mapping = aes(x = col_name_1, y = col_name_2)) +
  geom_point(color = "blue")

Question 6

How do you set the color of points based on the value of a column in the data frame?

Set color = column_name in the mapping

ggplot(df, mapping = aes(x = col_name_1, y = col_name_2, color = col_name_3)) +
  geom_point()

Question 7

How do you split the data into a separate subplot for each value in a column?

Use facets with either the facet_wrap() or facet_grid() function

ggplot(df, mapping = aes(x = col_name_1, y = col_name_2)) +
  geom_point() +
  facet_wrap(vars(col_name_3))

Question 8

How do you make a bar plot showing how many rows there are for each value of col_name?

ggplot(df, mapping = aes(x = col_name)) +
  geom_bar()

Since the geom uses the count statistical transformation, only x is included in the mapping.

Question 9

How do you make a histogram of col_name with red bars and 15 bins?

ggplot(df, mapping = aes(x = col_name)) +
  geom_histogram(fill = "red", bins = 15)

Question 10

How do you make a histogram of col_name_1 with the bars colored based on col_name_2 and 15 bins?

ggplot(df, mapping = aes(x = col_name_1, fill = col_name_2)) +
  geom_histogram(bins = 15)

Question 11

How do you make an unstacked histogram of col_name_1 with the bars colored based on col_name_2?

Use position = "identity", with alpha so the overlap is visible.

ggplot(df, mapping = aes(x = col_name_1, fill = col_name_2)) +
  geom_histogram(position = "identity", alpha = 0.5)

Question 12

How do you show the points and a linear best fit line on the same plot?

Add more than one geom to get more than one layer of data.

ggplot(df, mapping = aes(x = col_name_1, y = col_name_2)) +
  geom_point() +
  geom_smooth(method = "lm")

Question 13

How do you color the points by col_name_3, but still fit a single smooth to all of the points?

An aesthetic set in a geom instead of in ggplot() only applies to that layer.

ggplot(df, mapping = aes(x = col_name_1, y = col_name_2)) +
  geom_point(mapping = aes(color = col_name_3)) +
  geom_smooth(method = "lm")

Question 14

How do you save the plot you just made to a file?

ggsave("name_for_file.jpg")

The file extension (the letters after the .) determines what image format is used.