

Mutate(female = ifelse(female %in% 0, "Male", "Female")) |> To change this, make female a character variable, either temporarily in a pipe (as below) or permanently by re-assigning the result back to the dataframe. The facet rows are labeled with the values of female, 0 and 1, which is not very informative. This would also work if you use the formula age ~ edu, but this is not advised. ggplot(acs_small, aes(x = age, y = income)) +Īlthough female is a numeric variable, it was turned into a factor for the faceting. Give facet_grid() a formula, where the left side will become the rows, and the right side the columns. More variables can be supplied by lengthening the formula: ~ edu + race + female, but where two intersecting variables are used, facet_grid() is useful. The numbers of columns and rows can be modified with the nrow or ncol argument: ggplot(acs_small, aes(x = age, y = income)) + It is assumed that the left side of our formula is the rest of our selected data, so the formula can be read “age and income by education.” And that is what we see: ggplot(acs_small, aes(x = age, y = income)) + We can supply facet_wrap() with the formula ~ edu. Facets split our plot into several smaller plots along a categorical variable. Ggplot(acs_small, aes(x = age, y = income, shape = edu)) +įacets are a better way to visualize categorical variables with many categories.

ggplot(acs_small, aes(x = age, y = income, color = edu)) +

However, shapes and colors quickly become a mess as we increase the number of categories. ggplot(acs_small, aes(x = age, y = income, shape = as.factor(female))) + We can also use a variable to modify the shape aesthetic handled by geom_point(). ggplot(acs_small, aes(x = age, y = income, color = as.factor(female))) + See this online color picker application. ggplot(acs_small, aes(x = age, y = income, color = as.factor(female))) +Ĭolors can also be manually specified with names, hex codes, and other methods. To correct this, we can either change our dataframe to make female a character or factor vector, or we can temporarily specify it as such when we create our plot. Note that since female is numeric, ggplot created a legend with a continuous color scale. Ggplot(acs_small, aes(x = age, y = income, color = female)) + ggplot(acs_small, aes(x = edu, y = income, color = age)) + In this plot, older individuals are plotted with a lighter shade of blue. Colors can be useful, especially for continuous variables.
