Created
June 20, 2011 04:53
-
-
Save 1beb/1035147 to your computer and use it in GitHub Desktop.
Part IV: ggplot2 Introduction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Title: ggplot2 Introduction: Part IV | |
## Description: This line by line analysis, provides an introduction to ggplot2. Time series. | |
## Created by: Brandon Bertelsen: Research Manager, Credo Consulting Inc. | |
# Review the economics data set from the ggplot2 intro | |
e <- economics | |
str(e) | |
# Let's look at geom_line | |
ggplot(e, aes(date, uempmed)) + geom_line() | |
# Let's look at changes in savings rate and unemployment over time. | |
ggplot(e, aes(date, uempmed)) + geom_line() + geom_line(aes(date,psavert)) | |
# We should probably add some color | |
ggplot(e, aes(date, uempmed)) + geom_line(color="Red") + geom_line(color="Blue", aes(date,psavert)) | |
# We can fake a legend, but it's not the best option. To create it properly, we must reformat the data | |
ggplot(e, aes(date, uempmed, color="Rate")) + geom_line(aes(color="Unemployment")) + geom_line(data=e, aes(date, psavert, color="Savings")) | |
# Melting | |
e.melt <- melt(e, id.vars="date") | |
head(e.melt) | |
e.melt <- subset(e.melt, variable == c("uempmed","psavert")) | |
str(e.melt) | |
# Are we missing anything? Don't forget droplevels() | |
e.melt <- droplevels(e.melt) | |
ggplot(e.melt, aes(date, value, color=variable)) + geom_line() | |
# Smoothing | |
ggplot(e, aes(date, uempmed)) + geom_line() + geom_smooth() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment