contact@a2zlearners.com

2.6.7. Colors in ggplot2

1. Introduction

Color is a powerful tool in data visualization. In ggplot2, you can use color to highlight patterns, distinguish groups, and make your plots more engaging and informative. This section covers how to use and customize color in your ggplot2 plots.


2. Using Color in ggplot2

  • Color can be mapped to a variable (inside aes()) or set manually (outside aes()).
  • Mapping color to a variable adds a legend and helps distinguish groups.
  • Use color for outlines/points and fill for filled areas (bars, boxes).

3. Color in Scatterplots

R Code:

library(ggplot2)
adam <- data.frame(
  USUBJID = paste0("SUBJ", 1:100),
  AGE = sample(18:80, 100, replace = TRUE),
  HEIGHT = round(rnorm(100, mean = 165, sd = 10), 1),
  WEIGHT = round(rnorm(100, mean = 70, sd = 12), 1),
  SEX = sample(c("M", "F"), 100, replace = TRUE),
  TRT = sample(c("Placebo", "Active"), 100, replace = TRUE)
)
ggplot(adam) +
  geom_point(aes(x = AGE, y = WEIGHT, color = SEX))
  • Description:
    • Points are colored by subject sex.
    • A legend is added automatically.

Expected Outcome:

2.6.7.Color-in-Scatterplots.png

A scatterplot with points colored by sex.


4. Color in Barplots

R Code:

ggplot(adam) +
  geom_bar(aes(x = TRT))
  • Description:

2.6.7.Color-in-Barplots-1.png

  • Barplot of subject counts by treatment arm.
  • All bars are the same color by default.

R Code:

ggplot(adam) +
  geom_bar(aes(x = TRT, color = TRT))
  • Description:

2.6.7.Color-in-Barplots-2.png

  • Colors the outline of each bar by treatment arm.

R Code:

ggplot(adam) +
  geom_bar(aes(x = TRT, fill = TRT))
  • Description:
    • Fills each bar with a different color for each treatment arm.

Expected Outcome:

2.6.7.Color-in-Barplots-3.png

A barplot with bars filled by treatment arm, making group differences clear.


5. Mapping a Different Variable to Fill

R Code:

ggplot(adam) +
  geom_bar(aes(x = TRT, fill = SEX))
  • Description:
    • Stacked barplot showing the breakdown of sex within each treatment arm.

Expected Outcome:

2.6.7.Color-in-Stacked-barplot.png

A stacked barplot with each segment colored by sex.


6. Manually Setting Colors

  • Use scale_fill_manual() to set custom colors for fills.
  • Use scale_color_manual() to set custom colors for outlines/points.

R Code:

ggplot(adam) +
  geom_bar(aes(x = TRT, fill = SEX)) +
  scale_fill_manual(values = c("pink", "lightblue"))
  • Description:
    • Manually assigns colors to each sex group.

Expected Outcome:

2.6.7.Color-Manually-Setting-Colors.png

A barplot with custom colors for each sex.


7. Position Adjustments with Color

  • position = "fill": Bars are scaled to 100% height, showing proportions.
  • position = "dodge": Bars for each group are placed side by side.

R Code:

# Proportion barplot
ggplot(adam) +
  geom_bar(aes(x = TRT, fill = SEX), position = "fill") +
  scale_fill_manual(values = c("pink", "lightblue"))

2.6.7.Position-Adjustments-with-Color.png

R Code:

# Grouped barplot
ggplot(adam) +
  geom_bar(aes(x = TRT, fill = SEX), position = "dodge") +
  scale_fill_manual(values = c("pink", "lightblue"))

2.6.7.Position-Adjustments-with-Color-2.png

  • Description:
    • "fill" allows comparison of proportions across groups.
    • "dodge" allows comparison of values within each group.

8. Exploring Beyond Basic Colors

  • Use color palettes from RColorBrewer, viridis, or ggsci for better accessibility and aesthetics.
  • Use scale_fill_brewer() or scale_fill_viridis_d() for built-in palettes.
  • Adjust transparency with alpha for overlapping points.
  • Explore colorblind-friendly palettes for accessibility.

R Code Example:

library(RColorBrewer)
ggplot(adam) +
  geom_bar(aes(x = TRT, fill = SEX)) +
  scale_fill_brewer(palette = "Set2")

2.6.7.RColorBrewer.png


9. Input and Output Table for Color Examples

R Code Example Input Data Output (Plot/Description)
color = SEX adam Scatterplot, color by sex
fill = TRT adam Barplot, fill by treatment arm
fill = SEX + manual colors adam Barplot, custom colors by sex
position = "fill" adam Proportion barplot

10. Practice Problems

  1. Create a barplot of subject sex, filled by treatment arm.
  2. Make a scatterplot of weight vs. age, colored by sex.
  3. Use a colorblind-friendly palette for a barplot.
  4. Adjust the transparency of points in a scatterplot.
  5. Manually set the colors for a grouped barplot.

11. Further Reading and Resources


**Resource download links**

2.6.7.-Colors-in-ggplot2.zip