Home > Mobile >  Conditional color of points depending on which variable takes a specific value
Conditional color of points depending on which variable takes a specific value

Time:03-17

I have the following df

library(Rlab)    
    
q <- data.frame(v = seq(1,1000,1), w = round(rnorm(1000,500,200),2),
                    x = rbern(1000, 0.1),y = rbern(1000, 0.1), z = rbern(1000, 0.1))

When x = 1, I would like to plot 'w' on the graph with a color (say blue). When y = 1, I would like to plot 'w' on the graph with a color (say red). When z = 1, I would like to plot 'w' on the graph with a color (say green). When x,y,or z = 0, then I need no points. In case they overlap, I would like a dodge (but not necessary...)

How can I do that using ggplot2? notably with geom_point?

I tried the following for just one 'x' but it plots both the 1 and 0.

ggplot(q , aes (v,w,color = x)) geom_point()

Thanks in advance.

CodePudding user response:

I think the best way is to reshape (melt) the dataset first, otherwise you won't see the points with multiple colours. From your description you want multiple points when say x and y are both 1.

Reshaping to a long form (or melting in data.table language) will create three rows for each of your rows, one each for x, y and z. Then you can select only those rows where the value is 1 (the points you want to show). The variable factor corresponds to x, y, or z so now it is easy to set the colours and the dodge that you want.

library(data.table)
setDT(q)

qlong <- melt(q, id.vars = c("v","w"))[value==1]

ggplot(qlong, aes(v,w,group=variable,color=variable))   
  geom_point(position=position_dodge2(width = .2))

enter image description here

CodePudding user response:

You could use case_when:

library(dplyr)

q <- q %>%   mutate(color = factor(case_when(
    x == 1 ~ "blue",
    y == 1 ~ "red",
    z == 1 ~ "green"), levels=c("blue","green","red")))
  
ggplot(q[!is.na(q$color),]) 
  geom_point(aes(x=v,y=w,color=color)) 
  scale_color_manual(values=c("blue","green","red"))
  • Related