R Color Scatter Plot Points Based on Values

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.

Give color to scatter plot points based on value thershold

I agree with the comments above, anyway I think this is what you are looking for

p <- runif(100, min=-10, max=10)
g <- 1:100
dat <- data.frame(p, g)
dat$colors <- 1
dat[which(dat$p < (-8) | dat$p > 8),"colors"] <- 0

library(ggplot2)

ggplot(dat, aes(x=g, y=p, group=colors)) + geom_point(aes(color=as.factor(colors)))

Which results in this:
Sample Image

Edit:
In a previous version of this answer the different colors were expressed as a continuous variable. I changed this to a dichotomous format with as.factor.

How to color scatter plot points based on temporal sequence

You could do:

library(ggplot2)

ggplot(df, aes(x = X, y = Y, color = starttime_ms)) +
geom_point() +
scale_color_gradientn(colors = c("red", "gold", "forestgreen"), name = "time")

Sample Image

An alternative that might help keep track of the motion better would be to use geom_segment:

df$X2 <- dplyr::lag(df$X)
df$Y2 <- dplyr::lag(df$Y)

ggplot(df, aes(x = X, y = Y, color = starttime_ms)) +
geom_segment(aes(xend = X2, yend = Y2), size = 1) +
scale_color_gradientn(colors = c("red", "gold", "forestgreen"), name = "time")

Sample Image

Color scatter plot based on Date column in R



library(ggplot2)

ggplot( df, aes(X,Y)) +
geom_point(shape=21, stroke=1, aes(fill=Date),color="grey",size=6) +
scale_fill_gradient(low = "white", high = "black", labels=function(x)as.Date(x, origin="1970-01-01") )

I was surprised as to how clunky it appears to be with grey scale color gradients for Dates.

Sample Image

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

Color points of a scatterplot according to row numbers

You could try:

w2v$color <- "blue"
w2v$color[w2v$D == "no"] <- "red"

plot(FB_MW$Ärger, w2v$aerger,
main = "Ärger",
xlab = "Rating Fragebogen (MW)",
ylab = "word2vec",
col = w2v$color)

Another option is using ggplot2, which is usally a little bit more complex. In this case it's straight forward:

ggplot(w2v, aes(x=FB_MW$Ärger, y=aerger, color=D)) +
geom_point() +
xlab("Rating Fragebogen (MW)") +
ylab("word2vec") +
labs(title="Ärger") +
theme(plot.title = element_text(hjust = 0.5))

Define points colors based on single column values (RGB) in R

finally got it.

d0 <- c(2, 2, 255, 255, 0)
d1 <- c(5, 1, 0, 255, 0)
d2 <- c(4, 3, 0, 0, 255)
d3 <- c(1, 5, 255, 0, 255)

data <- rbind(d0,d1,d2,d3)
print(data)

x <-data[,1]
print(x)

y <-data[,2]
print(y)

colors <-data[,c(3,4,5)]
colors_norm <- colors * (1/255)
print(colors_norm)

colors_hex <- rgb(colors_norm)
print(colors_hex)

# Simple Scatterplot
plot(x,y, main="Color Scatterplot", xlab="x", ylab="y ", pch=19, col=colors_hex)


Related Topics



Leave a reply



Submit