I have made a graph with the year on the x-axis and sea level rise on the y-axis. I am trying to make the data from 2025 (predictions) a different colour to that before 2025. I have grouped and labeled the predicted data using this code and have also included the code for my graph
predictions=data[which(data$Year>2024 & data$Year<2121),] plot(data$Sea.Level..cm.~data$Year,xlab="Year",ylab="Sea Level (cm)",pch=21,col=c("Blue"))
How do I go from here in making the predictions red but the previous data blue? Thanks in advance
CodePudding user response:
You can try something like this. Toy data used.
vec <- c( rep(2001,10), rep(2002, 3) )
tf <- (vec < 2002) 1
barplot( 1:length(vec), vec, col=c("red","blue")[tf], names=vec )
CodePudding user response:
You can provide a vector of colours to plot in the col argument. For example, following your existing syntax:
predictions <- rep("red", nrow(data))
predictions[which(data$Year>2024 & data$Year<2121)] <- "blue"
plot(data$Sea.Level..cm.~data$Year,xlab="Year",ylab="Sea Level (cm)",pch=21,col=predictions)