Im extremely new to R-studio, I have to use this program for class. My group is trying to produce a linear line of-regression for all years of our data and we want the years to be different colors but no line is showing up
You want Year to be treated as a discrete label but R thinks it is a continuous numeric value. You can make it discrete by making it a factor. The code below reproduces your error and then fixes it with the factor() function.
library(ggplot2)
DF <- data.frame(X = rep(1:5,2), Y = c(11:15, 21:25),
Year = rep(c(2020, 2021), each = 5))
ggplot(DF, aes(X, Y, color = Year)) + geom_point() +
scale_color_manual(values = c("2020" = "red", "2021" = "blue"))
#> Error: Continuous value supplied to discrete scale
DF$Year = factor(DF$Year)
ggplot(DF, aes(X, Y, color = Year)) + geom_point() +
scale_color_manual(values = c("2020" = "red", "2021" = "blue"))
Can you help with our dataframe? We have very uneven data 2017 has 31, 2018 has 88, 2019 has 214, and 2021 has 139 and we’re confused on how to make them even/bypass the error u get when u try and make that data frame for y.
There is no need for you to use the rep() function, you can use the data you already have. The data frame I made was just for use in my example code, since I do not have your data. I am unsure which data frame you want to use with ggplot but let's say it is ticks and it has a column named Year. You can make a column that is a factor and is based on the Year column with code like this:
ticks$Year_Factor <- factor(ticks$Year)
In ggplot, make the color depend on the column Year_Factor.