dlab-berkeley / R-Data-Visualization-Legacy

D-Lab's 3 hour introduction to data visualization with R. Learn how to create histograms, bar plots, box plots, scatter plots, compound figures, and more using ggplot2 and cowplot.
28 stars 20 forks source link

Possible intro to using palettes, viridis_theme to adjust for colorblindness. #5

Closed Averysaurus closed 2 years ago

Averysaurus commented 3 years ago

The reprex below builds on the plots already put together in the main .RMD while adding a few color palette libraries, functions and arguments.

    library(colorspace)

    # the palettes colorspace has bundled. 
    colorspace::hcl_palettes(plot = TRUE)

   # "_Error in plot.new() : figure margins too large_" return?
    # pull your plot window to the left, and/or click "Zoom" button to view all the palettes together. 

    dyn_pal <- qualitative_hcl(5, palette = "Dynamic")
    # function is explict about what kind of scale:
    # see qualitative_hcl(), sequential_hcl(), diverging_hcl().
    # first argument specifies the n of colors, second is the palette name from chart. 
    dyn_pal

    # assigning colors explicitly. 
    boxplot(gap$lifeExp ~ gap$continent, 
        # Give each box its own color
     col = c("orange", "blue", "green", "red", "purple"))

    # using a palette assign colors with base R. 
    boxplot(gap$lifeExp ~ gap$continent, 
        # Draw colors from a special palette 
        col = dyn_pal)

    # ggplot without palette function added. 
    ggplot(data = gap, aes(x = gdpPercap, y = lifeExp, color = continent)) + 
    geom_point() + 
    theme(legend.position = "right")

    # using a palette.
    ggplot(data = gap, aes(x = gdpPercap, y = lifeExp, color = continent)) + 
    geom_point() + 
    # we can add a palette layer to our ggplot chunk from the names availble in hcl_palettes(). 
       colorspace::scale_color_discrete_sequential(palette = "Red-Blue")
    # unless an nmax() argument is specified, color assignments are automatically calculated from the distribution of the data. 

    install.packages("viridis")
    library(viridis)

     # It's estimated that, "_globally, 1 in 12 males and 1 in 200 females are colorblind._" The most common form of color 
    blindness is red-green color blindness. **Viridis** is a color palette with an emphasis on accessibility for those of us who 
    experience colorblindness.

    [Viridis](https://cran.r-project.org/web/packages/viridis/vignettes/intro-to-viridis.html)

    [Learn More about colorblindness](https://en.wikipedia.org/wiki/Color_blindness)

    # plots with viridis theme added.
    ggplot(data = gap, aes(x = continent, y = lifeExp, fill = continent)) + 
      geom_boxplot() + 
      viridis::scale_fill_viridis(discrete=TRUE)

   ggplot(data = gap, aes(x = gdpPercap, y = lifeExp, color = continent)) + 
      geom_point() + 
      theme(legend.position = "right") +
    viridis::scale_color_viridis(discrete=TRUE) 

    # Base R plotting with viridis. 
    # create a palette
    vir_pal <- sequential_hcl(5, palette = "Viridis")

    #plot with base. 
    boxplot(gap$lifeExp ~ gap$continent, 
        # Draw colors from a special palette 
        col = vir_pal)

# Note: Palettes and color theory are a whole other domain of art and design that R is super articulate about. With color palettes,  a focus on accessibility and avoidance of the "headache factor" are good principles to live by. 
EastBayEv commented 3 years ago

Thanks Avery, this is a great point. We will be sure to incorporate this into the other workshops at our first review at the end of the month!

pssachdeva commented 2 years ago

Closed by #12.