Danielle
Danielle

Reputation: 795

Plot timeseries and regression line for two groups of data

I have data from two sites across years (note the differences in sampling years). A sample is below:

df<- data.frame( year= c(seq(1997,2016,1), seq(2001,2017,1)), 
             site= c(rep("cr", 20),rep("ec", 17)),
             mean= sample(1:50,37))

I would like to make a time series-like graph of mean for each year. Each data point would be connected (in the typical zig-zag fashion of time-series graphs) and then a regression line is superimposed to indicate the trend. I have created a time series-like plot using ggplot (I do not mind a solution from base package), but I am having trouble superimposing a dashed-regression line for each site without error.

Here is the code I have tried:

f1 <- ggplot(data = df, aes(x = year, y = mean, group= site, color= 
 site))+
geom_line(aes(color=site)) + 
geom_point( aes(color=site),size=0.5)+
geom_smooth(method = "lm", se = FALSE, size= 0.5, aes(fill=site, 
linetype= 2 ))+
scale_linetype_manual(values=c("solid", "solid"))+
scale_color_manual(values=c("#CC0000", "#000000"))+
theme_minimal()+  
scale_x_continuous("Year",limits = c(1997, 2020), breaks = 
seq(1995,2020,5)) +
scale_y_continuous("Mean Monthly Abundance", limits = c(0, 1500), 
breaks=seq(0, 1500, by = 100)) +
theme_bw()+
theme(axis.line = element_line(colour = "black"),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank()) 

f1

A few details I would like this graph to illustrate:

Upvotes: 0

Views: 227

Answers (1)

csgroen
csgroen

Reputation: 2541

As @kath stated, adding linetype = "dashed" would fix it. I've made some minor modifications to the code as well:

ggplot(data = df, aes(x = year, y = mean, group= site, color = site))+
    geom_line() + 
    geom_point(size=0.5)+
    geom_smooth(method = "lm", se = FALSE, size= 0.5, linetype = "dashed")+
    scale_color_manual(values=c("#CC0000", "#000000"))+
    theme_minimal()+  
    scale_x_continuous("Year",limits = c(1997, 2020), breaks = 
                           seq(1995,2020,5)) +
    scale_y_continuous("Mean Monthly Abundance", limits = c(0, 1500), 
                       breaks=seq(0, 1500, by = 100)) +
    theme_bw()+
    theme(axis.line = element_line(colour = "black"),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          panel.background = element_blank()) 

Upvotes: 1

Related Questions