[英]How to make a range of points on a graph i n r a different colour
I have made a graph with the year on the x-axis and sea level rise on the y-axis.我制作了一张图表,x 轴为年份,y 轴为海平面上升。 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
我正在尝试使 2025 年的数据(预测)与 2025 年之前的数据颜色不同。我已使用此代码对预测数据进行分组和标记,并且还包含了我的图表的代码
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"))
predictions=data[which(data$Year>2024 & data$Year<2121),] plot(data$Sea.Level..cm.~data$Year,xlab="Year",ylab="海平面 (cm) ",pch=21,col=c("蓝色"))
How do I go from here in making the predictions red but the previous data blue?我如何从这里 go 将预测变为红色但之前的数据变为蓝色? Thanks in advance
提前致谢
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 )
You can provide a vector of colours to plot in the col argument.您可以在col参数中为 plot 提供颜色向量。 eg 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.