Skip to content

Instantly share code, notes, and snippets.

@maptracker
Created November 30, 2015 21:01
Show Gist options
  • Save maptracker/5d4e0bc729a27cab2b04 to your computer and use it in GitHub Desktop.
Save maptracker/5d4e0bc729a27cab2b04 to your computer and use it in GitHub Desktop.
#R - UAH data set using #ggplot functions #geom_smooth and #facet_wrap
## Using UAH satellite data to demonstrate faceting and smoothing
library(ggplot2)
## There are a bunch of comments at the end of the file, take first
## 443 rows (up to Oct 2015)
numRow <- 443
url <- "http://vortex.nsstc.uah.edu/data/msu/t2lt/uahncdc_lt_5.6.txt"
uah <- read.delim(url, sep = "", nrows = numRow)
## Assign month names as a factor
uah$Month <- factor( month.abb[ uah$Mo ], levels = month.abb )
## Basic scatter plot grouped by month
## http://docs.ggplot2.org/current/geom_point.html
## http://docs.ggplot2.org/current/labs.html
plot <- ggplot( data = uah, aes(Year, Globe)) +
labs(x = "Year", y = "Global Average Temperature Anomaly ('Globe')",
title = "University of Alabama in Huntsville\nSatellite Temperature Dataset") +
geom_point( aes(color = Month) )
## All months overlaid (kind of messy). Using geom_smooth() to project
## the fit to the linear model. Need to lower alpha to prevent
## confidence intervals from obscuring each other.
## http://docs.ggplot2.org/current/geom_smooth.html
allAtOnce <- plot +
geom_smooth(method = "lm", formula = y ~ x, alpha = 0.05,
aes( color = Month) )
ggsave("AllAtOnce.png", allAtOnce)
## Facetted into individual month panels
## Turn off the color legend since it's broken out in the facets
## http://docs.ggplot2.org/current/facet_wrap.html
byMonth <- plot +
geom_smooth(method = "lm", formula = y ~ x, aes(color = Month) ) +
facet_wrap( ~ Month) +
guides(color=FALSE)
ggsave("ByMonth.png", byMonth)
@maptracker
Copy link
Author

bymonth

allatonce

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment