Last active
July 6, 2021 16:49
-
-
Save pierreroudier/357bd853dd9d0df14a09 to your computer and use it in GitHub Desktop.
Varying line width with ggplot2
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
# First, let's make up some dummy data | |
# I assume your data is 3 columns: year, the x scale between 0 and 1, and some values | |
library(plyr) | |
df <- ldply(1995:2015, function(x) data.frame(year = x, x = seq(0,1, by = 0.1), values = rnorm(11))) | |
# You can check if my assumptions about your data are correct: | |
head(df) | |
# The plotting code begins here | |
# (assuming your data.frame is named "df") | |
library(ggplot2) | |
p <- ggplot(df) + geom_line(aes(x = x, y = year, group = year, size = values)) | |
print(p) | |
# We can even get a bit fancy and use colour to represent year | |
p <- ggplot(df) + geom_line(aes(x = x, y = year, group = year, colour = year, size = values)) | |
print(p) | |
# For the smoothing, there's probably something to do with `geom_smooth` | |
# Also, I think I would use faceting to do that | |
p <- ggplot(df) + | |
geom_line(aes(x = x, y = values, group = year, colour = year, size = values)) + | |
geom_smooth(aes(x = x, y = values, group = year, size = values), method = "loess") + facet_wrap(~year) | |
print(p) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment