Back to Browse

Draw ggplot2 Plot with Different Background Colors by Region in R (Example) | geom_rect() Function

1.5K views
May 25, 2022
4:34

How to create a ggplot2 graph with different background colors by region in the R programming language. More details: https://statisticsglobe.com/draw-ggplot2-plot-different-background-colors-region-r R code of this video: data <- data.frame(x = 1:10, # Create example data y = 1:10) install.packages("ggplot2") # Install & load ggplot2 library("ggplot2") ggplot(data, aes(x, y)) + # Draw default plot geom_point() data_breaks <- data.frame(start = c(0, 2, 5, 9), # Create data with breaks end = c(2, 5, 9, 10), colors = factor(1:4)) data_breaks # Print data with breaks ggplot() + # Add background colors to plot geom_rect(data = data_breaks, aes(xmin = start, xmax = end, ymin = - Inf, ymax = Inf, fill = colors), alpha = 0.5) + geom_point(data = data, aes(x, y)) Follow me on Social Media: Facebook – Statistics Globe Page: https://www.facebook.com/statisticsglobecom/ Facebook – Group for Discussions & Questions: https://www.facebook.com/groups/statisticsglobe LinkedIn – Statistics Globe Page: https://www.linkedin.com/company/statisticsglobe/ LinkedIn – Group for Discussions & Questions: https://www.linkedin.com/groups/12555223/ Twitter: https://twitter.com/JoachimSchork Music by bensound.com

Download

0 formats

No download links available.

Draw ggplot2 Plot with Different Background Colors by Region in R (Example) | geom_rect() Function | NatokHD