### Install and Use viridis Color Palette in Base R Plots Source: https://cran.r-project.org/web/packages/viridis/vignettes/intro-to-viridis.html Demonstrates how to install the viridis package and use its default color palette for creating visualizations in base R. It generates a contour plot using the viridis color scale. ```r install.packages("viridis") library(viridis) x <- y <- seq(-8*pi, 8*pi, len = 40) r <- sqrt(outer(x^2, y^2, "+")) filled.contour(cos(r^2)*exp(-r/(2*pi)), axes=FALSE, color.palette=viridis, asp=1) ``` -------------------------------- ### Apply viridis Color Scales in ggplot2 Source: https://cran.r-project.org/web/packages/viridis/vignettes/intro-to-viridis.html Shows how to integrate viridis color scales into ggplot2 visualizations using `scale_color_viridis()` and `scale_fill_viridis()`. This example creates a hexbin plot with a viridis fill scale. ```r library(ggplot2) ggplot(data.frame(x = rnorm(10000), y = rnorm(10000)), aes(x = x, y = y)) + geom_hex() + coord_fixed() + scale_fill_viridis() + theme_bw() ``` -------------------------------- ### Apply Discrete Viridis Color Scale to Scatter Plot (R) Source: https://cran.r-project.org/web/packages/viridis/vignettes/intro-to-viridis.html This example shows how to apply a discrete viridis color scale to a scatter plot using ggplot2. It maps the number of cylinders to color. Requires ggplot2 and viridis. ```r p <- ggplot(mtcars, aes(wt, mpg)) p + geom_point(size=4, aes(colour = factor(cyl))) + scale_color_viridis(discrete=TRUE) + theme_bw() ``` -------------------------------- ### Generate Inferno Color Scale for Raster Image (R) Source: https://cran.r-project.org/web/packages/viridis/vignettes/intro-to-viridis.html This snippet demonstrates how to load a raster image, project it, and display it using the 'inferno' color scale from the viridis package. It requires the 'terra' and 'httr' libraries. ```r library(terra) library(httr) par(mfrow=c(1,1), mar=rep(0.5, 4)) temp_raster <- "http://ftp.cpc.ncep.noaa.gov/GIS/GRADS_GIS/GeoTIFF/TEMP/us_tmax/us.tmax_nohads_ll_20150219_float.tif" try(GET(temp_raster, write_disk("us.tmax_nohads_ll_20150219_float.tif")), silent=TRUE) us <- rast("us.tmax_nohads_ll_20150219_float.tif") us <- project(us, y="+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=37.5 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs") image(us, col=inferno(256), asp=1, axes=FALSE, xaxs="i", xaxt='n', yaxt='n', ann=FALSE) ``` -------------------------------- ### Create Choropleth Map with Magma Scale using ggplot2 (R) Source: https://cran.r-project.org/web/packages/viridis/vignettes/intro-to-viridis.html This code generates a choropleth map of US unemployment rates by county using ggplot2 and the 'magma' color scale from viridis. It requires the 'maps', 'mapproj', and 'viridis' packages. ```r library(maps) library(mapproj) data(unemp, package = "viridis") county_df <- map_data("county", projection = "albers", parameters = c(39, 45)) names(county_df) <- c("long", "lat", "group", "order", "state_name", "county") county_df$state <- state.abb[match(county_df$state_name, tolower(state.name))] county_df$state_name <- NULL state_df <- map_data("state", projection = "albers", parameters = c(39, 45)) choropleth <- merge(county_df, unemp, by = c("state", "county")) choropleth <- choropleth[order(choropleth$order), ] ggplot(choropleth, aes(long, lat, group = group)) + geom_polygon(aes(fill = rate), colour = alpha("white", 1 / 2), linewidth = 0.2) + geom_polygon(data = state_df, colour = "white", fill = NA) + coord_fixed() + theme_minimal() + ggtitle("US unemployment rate by county") + theme(axis.line = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(), axis.title = element_blank()) + scale_fill_viridis(option="magma") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.