Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to conditionally highlight points in ggplot2 facet plots - mapping color to column

Tags:

plot

r

ggplot2

In the following example I create two series of points and plot them using ggplot2. I also highlight several points based on their values

library(ggplot2)
x <- seq(0, 6, .5)
y.a <- .1 * x -.1
y.b <- sin(x)
df <- data.frame(x=x, y=y.a, case='a')
df <- rbind(df, data.frame(x=x, y=y.b, case='b'))
print(ggplot(df) + geom_point(aes(x, y), color=ifelse(df$y<0, 'red', 'black')))

And here is the result

First result

Now I want to separate the two cases into two facets, keeping the highlighting scheme

> print(ggplot(df) + geom_point(aes(x, y), color=ifelse(df$y<0, 'red', 'black')) + facet_grid(case ~. ,))
Error: Incompatible lengths for set aesthetics: colour

How can this be acheived?

like image 504
Boris Gorelik Avatar asked Apr 04 '13 06:04

Boris Gorelik


People also ask

What is the function of Facet_grid () in Ggplot ()?

facet_grid() forms a matrix of panels defined by row and column faceting variables. It is most useful when you have two discrete variables, and all combinations of the variables exist in the data. If you have only one variable with many levels, try facet_wrap() .

What is the difference between Facet_wrap and Facet_grid?

The facet_grid() function will produce a grid of plots for each combination of variables that you specify, even if some plots are empty. The facet_wrap() function will only produce plots for the combinations of variables that have values, which means it won't produce any empty plots.

What is Facet_wrap?

facet_wrap() makes a long ribbon of panels (generated by any number of variables) and wraps it into 2d. This is useful if you have a single variable with many levels and want to arrange the plots in a more space efficient manner. You can control how the ribbon is wrapped into a grid with ncol , nrow , as.


2 Answers

You should put color=ifelse(y<0, 'red', 'black') inside the aes(), so color will be set according to y values in each facet independently. If color is set outside the aes() as vector then the same vector (with the same length) is used in both facets and then you get error because length of color vector is larger as number of data points.

Then you should add scale_color_identity() to ensure that color names are interpreted directly.

ggplot(df) + geom_point(aes(x, y, color=ifelse(y<0, 'red', 'black'))) + 
   facet_grid(case ~. ,)+scale_color_identity()

enter image description here

like image 76
Didzis Elferts Avatar answered Nov 20 '22 06:11

Didzis Elferts


Instead of using scale_..._identity, one can also wrap the color (and fill) aesthetic in I(). It also requires having color defined in aes.

I came across this question, where the OP I guess kind of accidentally made use of I()... ggplot color is not automatically coloring based on group

Not sure I would every make use of that, but I find this kind of fun.

library(ggplot2)
x <- seq(0, 6, .5)
y.a <- .1 * x -.1
y.b <- sin(x)
df <- data.frame(x=x, y=y.a, case='a')
df <- rbind(df, data.frame(x=x, y=y.b, case='b'))

ggplot(df) + 
  geom_point(aes(x, y, color= I(ifelse(y < 0, 'red', 'black')))) +
  facet_grid(case ~. )

Created on 2020-07-01 by the reprex package (v0.3.0)

like image 3
tjebo Avatar answered Nov 20 '22 05:11

tjebo