1

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.

enter image description here

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...        
Community
  • 1
  • 1
Kulluk007
  • 902
  • 2
  • 10
  • 24

1 Answers1

2

You want to use line_size_discrete(). You can skip the creation of a line_size column and base the graphics on what you already have.

library(tidyverse)
exps %>%
    ggplot(aes(ind_var_arg, dep_var)) + 
           geom_line(aes(size=var1, line_type=major_version, color=config)) + 
           scale_size_discrete(range = c(1, 2))
B Williams
  • 1,992
  • 12
  • 19
  • Thank you, B Williams -- this worked for me and I'm getting the expected results. Just for my own understanding, what was actually happening in the behavior I was getting before? Were the size values I was supplying not being directly used, but mapped into some preset size array (sort of like how linetype works)? – Kulluk007 Apr 04 '17 at 21:12
  • 1
    you were telling the sizes to vary, but not actually defining how they should vary – B Williams Apr 04 '17 at 21:19