My goal is to scale the line width of a geom_line in a ggplot based on data. I've read a number of related posts, but am still struggling to achieve my goal. Related posts include:
This post is very close to my question, although I'm confused about where Freq
is initialized in that example: Different size for lines in ggplot2's geom_line
Here's my code. Note that I'm using mutate to initialize a new variable, line_size, that I then use in my geom_line:
suppressWarnings(suppressMessages(library(dplyr)))
library(ggplot2)
exps <- mutate(exps, line_size = ifelse( var1 == 'foo', 2, 1 ))
# (this is in a function, so I'm using aes_string to interpret ind_var_arg, which is a function argument)
ggplot(exps, aes_string(x="ind_var_arg", y=dep_var)) +
geom_line(aes(size=line_size, linetype=major_version,
color=config), alpha=alpha_level)
So, I'm trying to make some particular lines (the ones that match var1 == 'foo') of thickness 2, and the rest of thickness 1. Because I want the size to vary based on the data, I'm using aes in geom_line. The resulting image is not what I'm expecting -- the thicker line is massive and the other lines are thicker than the default. When I change the values in line_size to smaller values, like 0.1 and 0.05, the line thickness doesn't change. I checked the contents of the data frame that I'm plotting (exps) and line_size is initialized as I expect (a row of doubles with no NAs).
Clearly I'm not using the line_size correctly here. What am I doing wrong? I want to use the contents of line_size explicitly as the line size for that particular row.
Here's the structure of the data frame that I'm plotting:
glimpse(exps)
$ dep_var <int> 1000000, 1000000, 1000000, 1000000...
$ config <chr> "config1", "config1", "config2", ...
$ ind_var_arg <dbl> 5198283352, 420183942, 125450482, ...
$ var1 <chr> "bar", "foo", "bar", "bar", "bar",...
$ major_version <chr> "ONE", "TWO", "ONE", "ONE", "ONE",...
$ line_size <dbl> 0.005, 20.000, 0.005, 0.005, 0.005...