Home > Mobile >  Confusion on defining data for trendline in a graph
Confusion on defining data for trendline in a graph

Time:09-16

So I set up my graph to be:

ten <- c(0.2,0.16,0.17,0.188)
fifty <- c(0.1,0.24,0.17,0.166)
hundred <- c(0.3,0.16,0.16,0.14)
fiveh <- c(0,0.02,0.04,0.022)

plot(c(10,10,10,10,50,50,50,50,100,100,100,100,500,500,500,500),c(ten,fifty,hundred, fiveh), 
    ylab= "% of Impaired Rivers", xlab= "Number of Observations", main= "samples from all rivers")

and I'm trying to use abline function to create a trendline in my graph but got confused on how to define my values.

Help on how to make a proper trend line would be greatly appreciated.

CodePudding user response:

By basic R,(also, you can use rep( ., each = ) to state c(10,10,.....))

df <- c(ten,fifty,hundred, fiveh)
x <- rep(c(10,50,100,500), each = 4)

plot(x,df , 
     ylab= "% of Impaired Rivers", xlab= "Number of Observations", main= "samples from all rivers")
model <- lm(df ~ x)
abline(model, col = "red")

enter image description here

CodePudding user response:

If you want to try a tidyverse approach, here is how:

Packages

library(ggplot2)
library(dplyr)

Create a data.frame

ten <- c(0.2,0.16,0.17,0.188)
fifty <- c(0.1,0.24,0.17,0.166)
hundred <- c(0.3,0.16,0.16,0.14)
fiveh <- c(0,0.02,0.04,0.022)

df <-
  tibble(
    x = c(10,10,10,10,50,50,50,50,100,100,100,100,500,500,500,500),
    y = c(ten,fifty,hundred, fiveh)
  )

How to

df %>% 
  ggplot(aes(x,y)) 
  geom_point() 
  labs(
    y = "% of Impaired Rivers",
    x = "Number of Observations",
    title = "samples from all rivers"
  ) 
  geom_smooth(method = "lm",se = FALSE)

enter image description here

  •  Tags:  
  • r
  • Related