How to format line size in ggplot with multiple lines of different lengths?

4.9k views Asked by At

I am able to make the plot correctly, but I would like to increase the line sizes to make the plot more readable. When I try size inside the geom_line, my lines get super fat. I have three time series variables (x,y, z) in the dataframe "data", which I want to plot on the y-axis, and they are of different length, meaning the plots start at different time. How can I change the size of the lines without making them huge?

P_comp <- ggplot(data, aes(x=Date))+
  geom_line(aes(y = x, colour = "green"))+
  geom_line(aes(y = y, colour = "darkred"))+
  geom_line(aes(y = z, colour = "steelblue"))+
  theme_ipsum()+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())+
  theme(text = element_text(family = "serif"))+
  xlab("Time") + ylab("Value") +
  ggtitle("EPU Indices")+
  theme(plot.title = element_text(hjust = 0.5, family = "serif", face = "plain", size = 16))+
  theme(axis.title.x = element_text(hjust = 0.5, family = "serif", size = 12, face = "plain"))+
  theme(axis.title.y = element_text(hjust = 0.5, family = "serif", size = 12, face = "plain"))
P_comp

This is the plot without using size argument

This is the plot when inputting size= 1 in one of the geom_lines

2

There are 2 answers

1
Mick On BEST ANSWER

Your code snippet doesn't show it here, but it sounds like you are setting size = 1 inside the aes() statement. This will add a size aesthetic called "1" and automatically assign a size to it.

Try this instead: geom_line(aes(y = x, colour = "green"), size = 1)

0
Rui Barradas On

The line width can be set with one of the scale_size_* scales. In the examples below I will use scale_size_manual.
The line sizes will be set to one value per level of the categorical variable, group.

In this first example the line size is set to the values 1:3, making the lines thicker.

library(ggplot2)

ggplot(df1, aes(Date, y, color = group)) +
  geom_line(aes(size = group)) +
  scale_size_manual(values = 1:3) +
  theme_bw()

enter image description here

Now make the lines slimmer. The rest of the plot is the same.

ggplot(df1, aes(Date, y, color = group)) +
  geom_line(aes(size = group)) +
  scale_size_manual(values = (1:3)/5) +
  theme_bw()

enter image description here

Data

df1 <- iris[4:5]
df1$Date <- rep(seq(Sys.Date() - 49, Sys.Date(), by = "day"), 3)
names(df1)[1:2] <- c("y", "group")