-2

I have a grid of equi-spaced points (13*8). I want to plot some specific points in that grid in a different color. The coordinates for those specific points are stored in a different matrix. Can someone please help me out with this?

enter image description here

Here's the code I am using to generate the grid.

ggplot(data=a,aes(x=X,y=Y))+geom_point()

'a' basically contains the coordinates for the points plotted in the grid. Those points are supposed to mimic the locations of a bolt on a plate.

Here's the matrix that contains the coordinates for the points to be highlighted

    sigbolts
      x.c y.c
 [1,]   4   4
 [2,]   4   5
 [3,]   3   6
 [4,]   4   6
 [5,]   5   6
 [6,]   3   7
 [7,]   4   7
 [8,]   5   7
 [9,]   3   8
[10,]   4   8
[11,]   5   8
[12,]   8   8
[13,]   4   9
[14,]   4  10
[15,]   6  13
  • 1
    See [how to create a reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Show what `a` contains and show how you have stored the points you wish to highlight. Changes are good it will just be another `geom_point()` layer with a different `data=` parameter. – MrFlick Oct 26 '17 at 18:05

1 Answers1

0
library(tidyverse)

a = as_data_frame(expand.grid(1:10,1:10))
colnames(a) = c('x', 'y')
sigbolts = data_frame(x.c = c(1,3,5), y.c = c(2,4,2))
sigbolts$indicator = 1

df = left_join(a, sigbolts, by = c('x' = 'x.c', 'y' = 'y.c')) %>%
    mutate(indicator = as.factor(ifelse(is.na(indicator), 0, 1)))

ggplot(df, aes(x = x, y = y, color = indicator)) +
    geom_point()
Fridolin Linder
  • 401
  • 6
  • 12
  • Actually just saw @MrFlick 's suggestion which makes the join unnecessary: `ggplot() + geom_point(data = a, aes(x = x, y = y)) + geom_point(data = sigbolts, aes(x = x.c, y = y.c), color = "red")` – Fridolin Linder Oct 26 '17 at 18:52