0

I have a few raster plots separated with facets. In each plot, I want to add an independent point. This shows how to add a point but I can only add the same point to all plots.

Suppose I want to add a point at the maximum value of the following three plots (the code is given below). How can I do that?

xy    <- expand.grid(0:20,0:20)
data  <- rbind(xy,xy,xy)
group <- rep(1:3,each=nrow(xy))
set.seed(100)
z     <- rnorm(nrow(data))
data <- cbind(data,group,z)
colnames(data) <- c("x","y","group","z")
library(ggplot2)
ggplot(data,aes(x,y,z))+geom_raster(aes(fill=z))+facet_wrap(~group)
markus
  • 25,843
  • 5
  • 39
  • 58
quibble
  • 303
  • 2
  • 13

1 Answers1

2

You would need to have a seperate data.frame with the point coordinates, which also contains the group variable:

library(ggplot2)

xy    <- expand.grid(0:20,0:20)
data  <- rbind(xy,xy,xy)
group <- rep(1:3,each=nrow(xy))
set.seed(100)
z     <- rnorm(nrow(data))
data <- cbind(data,group,z)
colnames(data) <- c("x","y","group","z")


pointxy <- data.frame(
  x = runif(10, 0, 20),
  y = runif(10, 0, 20),
  group = sample(1:3, 10, TRUE)
)

ggplot(data,aes(x,y,z))+
  geom_raster(aes(fill=z))+
  geom_point(data = pointxy) +
  facet_wrap(~group)

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

teunbrand
  • 33,645
  • 4
  • 37
  • 63