0
votes

I have some data that I want to visualise using geom_line in ggplot. There are two categories in the dataset, so I use manual colors black and red for them. I also have moving average lines for each, which means that I have four lines in total.

The problem I am facing is that moving average lines are not very clear in the plot and I want to distinguish between them (using color preferrably). Currently, I can either set the same color for both moving average lines (e.g. yellow) or get the color of the original data (red and black).

What I want to do is set two new colors for the moving averages and also show them in the legend. Can you please help?

Here is an example code.

# rm(list = ls())
library(ggplot2)
X <- as.numeric(1:500)
df <- data.frame(x=X,y=rnorm(n=500,sd=.2)+sin(X*pi/110),gp=1)
for (i in 2) df <- rbind(df,data.frame(x=X,y=rnorm(n = 500,sd = .1)+sin(X*i*pi/200),gp=i))

### Classify as character ###
df$gp <- as.character(df$gp)

### Plot ###
ggplot(data = df,aes(x=x,y=y,group=gp,color=gp)) + 
  geom_line() + 
  geom_line(aes(y=rollmean(y, 15, na.pad=TRUE)), size=1, color="yellow") +
  theme_bw() +
  scale_colour_manual("", values = c("red", "black")) +
  guides(colour=guide_legend(title="Model"))

enter image description here

In this plot, I want the two yellow lines to be shown by two new colors. Other suggestions which can help in distinguishing between the two lines in such large datasets are also welcome.

1

1 Answers

0
votes

You can create new column with moving average, get the data in long format and then plot so that you can get legend for all the 4 colors.

library(tidyverse)
library(zoo)

df %>%
  group_by(gp) %>%
  mutate(moving_avg = rollmean(y, 15, na.pad=TRUE)) %>%
  ungroup %>%
  pivot_longer(cols = c(y, moving_avg)) %>%
  unite(col, name, gp) %>%
  ggplot(aes(x=x,y=value,color=col)) + 
  geom_line() + 
  theme_bw() +
  guides(colour=guide_legend(title="Model"))

enter image description here