3

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
Nakx
  • 1,460
  • 1
  • 23
  • 32
Mr Good News
  • 121
  • 1
  • 3
  • 15
  • Possible duplicate of [How the change line width in ggplot?](https://stackoverflow.com/questions/14794599/how-the-change-line-width-in-ggplot) – Adam Quek Nov 07 '17 at 02:58

2 Answers2

4

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)
2

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

neilfws
  • 32,751
  • 5
  • 50
  • 63