简体   繁体   English

如何使 r 中图表上的一系列点具有不同的颜色

[英]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.

相关问题 使用R将图表和颜色添加到图表上的不同点 - adding labels and colour to different points on a graph using R 如何用颜色填充折线图中的点而不是r中的线? - How to fill the points in a line graph with colour but not the lines in r? 我如何使用 R 在 R 中随机使用 3 个不同的 colors 为图形点着色 - How can i color points of a graph randomly with 3 different colors in R using R basic only 我在 3 个不同时间点收集了 7 个不同的病毒浓度数据点。 我如何在 R 中用误差线绘制这个图? - I have 7 different data points for virus concentration collected at 3 different time points. How do I graph this with error bars in R? 如何在图形上将1000个点绘制成不同颜色的点? - How Do I Graph 1000 Points Onto A Graph, With Different Points Being Different Colors? R:textplot()如何更改点的颜色? - R: textplot() how change the colour of points? 如何在 R 中绘制图形,第一个值是一种颜色,下一个值是另一种颜色? - How do I plot a graph in R, with the first values in one colour and the next values in another colour? 如何 plot 一条回归线,但在 ggplot2 R 中用不同的因素对点进行着色? - How to plot a single regression line but colour points by a different factor in ggplot2 R? 如何在 R 的散点图上对数据集中范围内的点进行着色 - How can I color points in a range in a dataset on a scatterplot in R 如何在 R 中制作这样的图表? - How can I make a graph like this in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM