Colour Points in a Plot Differently Depending on a Vector of Values

Colour points in a plot differently depending on a vector of values

Here's a solution using base R graphics:

#Some sample data
x <- runif(100)
dat <- data.frame(x = x,y = x^2 + 1)

#Create a function to generate a continuous color palette
rbPal <- colorRampPalette(c('red','blue'))

#This adds a column of color values
# based on the y values
dat$Col <- rbPal(10)[as.numeric(cut(dat$y,breaks = 10))]

plot(dat$x,dat$y,pch = 20,col = dat$Col)

Sample Image

R color scatter plot points based on values

Best thing to do here is to add a column to the data object to represent the point colour. Then update sections of it by filtering.

data<- read.table('sample_data.txtt', header=TRUE, row.name=1)
# Create new column filled with default colour
data$Colour="black"
# Set new column values to appropriate colours
data$Colour[data$col_name2>=3]="red"
data$Colour[data$col_name2<=1]="blue"
# Plot all points at once, using newly generated colours
plot(data$col_name1,data$col_name2, ylim=c(0,5), col=data$Colour, ylim=c(0,10))

It should be clear how to adapt this for plots with more colours & conditions.

Change color of points on a plot based on index R

here you plot your vector data without x specified so x is x1 = 1... xn = length(data)

just indicate the x corresponding and it will work

points(n_data, data[n_data],col=2)
points(p_data, data[p_data],col=3)

Colour points for x-axis plot R

using base R you can try

plot(iris$Sepal.Length, iris$Sepal.Width, col = rep(1:2, times = c(130, nrow(iris)-130)))

Sample Image

R - Color plot points by index

You can add points of any colour using points:

allcv10 <- c(0.3121693, 0.3174603, 0.3121693, 0.3068783, 0.2592593, 0.3015873, 
0.3068783, 0.3068783, 0.3121693, 0.3386243, 0.3650794, 0.3227513,
0.3174603, 0.3333333, 0.3492063, 0.3492063, 0.3121693, 0.3174603,
0.3121693, 0.3015873, 0.2751323, 0.3015873, 0.3015873, 0.3068783)

plot(allcv10[1:8], pch = 20, xlab = "Model Number", ylab = "CV Error Rate",
main = "Comparison of Error Rates", xlim = c(1, 24), ylim = c(0.25, 0.37),
col ="red")
points(9:16, allcv10[9:16], col = "blue", pch = 20)
points(17:24, allcv10[17:24], col = "green", pch = 20)
abline(v = c(8.5, 16.5))
legend("topright", legend = c("LDA", "QDA", "Logistic Regression"),
col = c("red", "blue", "green"), pch = 20, cex = 0.8)

Sample Image

Created on 2020-02-28 by the reprex package (v0.3.0)

Plot with conditional colors based on values in R

The argument col will set the colours, you could use this in conjunction with an ifelse statement. See ?plot for more details.

# using base plot
plot(x,y,xlab="PC1",ylab="PC2", col = ifelse(x < 0,'red','green'), pch = 19 )

Sample Image

To do the same thing in ggplot2.

#using ggplot2
library(ggplot2)
ggplot(np_graph) + geom_point(aes(x = C1, y = C2, colour = C1 >0)) +
scale_colour_manual(name = 'PC1 > 0', values = setNames(c('red','green'),c(T, F))) +
xlab('PC1') + ylab('PC2')

Sample Image



Related Topics



Leave a reply



Submit