Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increase the size of line in geom_line

Tags:

r

This is the code I am using:

ggplot(data, aes(x = Date1, group=1)) + 
  geom_line(aes(y = Wet, colour = "Wet")) + 
  geom_line(aes(y = Dry, colour = "Dry"))

When I use the function size, the lines are too thick and their width is identical from size=0.1 to size=10 or more. Is there a way to control the size of the line?

Dummy data:

Date         Wet    Dry
July        5.65    4.88
September   5.38    3.93
October     4.73    2.42
like image 310
Mr Good News Avatar asked Nov 05 '25 14:11

Mr Good News


2 Answers

One issue that is commonly run into when changing the size using geom_line() is that the size must go OUTSIDE of the aes() commands. If you don't do that, the size argument will always keep the lines at an obnoxious size.

So, instead of:

ggplot(data, aes(x = Date1, group=1)) + 
  geom_line(aes(y = Wet, colour = "Wet", size = 3)) + 
  geom_line(aes(y = Dry, colour = "Dry", size = 3))

Try:

ggplot(data, aes(x = Date1, group=1)) + 
  geom_line(aes(y = Wet, colour = "Wet"), size = 3) + 
  geom_line(aes(y = Dry, colour = "Dry"), size = 3)
like image 91
stratosphere Avatar answered Nov 08 '25 10:11

stratosphere


If you find yourself adding multiple geom_line statements, it's because you need to convert your data from wide to long, i.e. one column for variable (Wet/Dry) and another for its values. Then ggplot takes care of everything itself.

library(tidyverse)

data %>% 
  gather(condition, value, -Date) %>% 
  mutate(Date = factor(Date, 
                       levels = c("July", "September", "October"))) %>%
  ggplot(aes(Date, value)) + 
    geom_line(aes(color = condition, group = condition), size = 3)

enter image description here

like image 40
neilfws Avatar answered Nov 08 '25 12:11

neilfws