Skip to content

Instantly share code, notes, and snippets.

@pierreroudier
Last active July 6, 2021 16:49
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pierreroudier/357bd853dd9d0df14a09 to your computer and use it in GitHub Desktop.
Save pierreroudier/357bd853dd9d0df14a09 to your computer and use it in GitHub Desktop.
Varying line width with ggplot2
# 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