1

My screen is of size 15.6 inches .I have taken screen shot of my screen.I am working on a project where I have to plot scatter plot of the points where mouse was clicked.So I have coordinates where the mouse was clicked .I want to plot scatter plot over this image and that scatter plot are points on the screen where mouse was clicked .So when I am plotting and the graph using the ggplot2 and adding a background image .It scales the image and the scatter points are seen at different position from where the mouse was actually clicked.

p4 <- ggplot(subset(ClickData,type1=="singleClick"), aes(ms,x=x,y=-y)) + background_image(b)+
  geom_point(alpha=1,color='red') +
  # geom_smooth(alpha=.2, size=1) +
  ggtitle("Single click")+
  theme(legend.position="none")  
p4

How can I do this!! Thanks for answers

EDIT1:

This is my input image

x,y,type
1341,278,singleLeftClick
1194,320,singleLeftClick
1233,555,singleLeftClick
1111,586,singleLeftClick
1160,587,singleLeftClick

enter image description here Its aspect ratio is changinga and the points it is showing is not the place where click actually occurred!!

Nurav
  • 167
  • 1
  • 3
  • 15
  • A good explanation how this can be done is [here](https://www.engineeringbigdata.com/how-to-add-a-background-image-in-ggplot2-with-r/) – MarBlo Jul 04 '20 at 06:04
  • You understood my question? – Nurav Jul 04 '20 at 06:10
  • PLease add some data to make this question reproducible. You can add the screenshot as an image in the question, and use dput to provide some mouse coordinates – dww Jul 04 '20 at 17:44
  • Yes sure @dww Have a look at my edit!! – Nurav Jul 05 '20 at 02:49

1 Answers1

1

You should set the coordinate systems of the plot to match the dimensions of the image

ggplot(ClickData, aes(x, y  = dim(b)[1] - y)) + 
        background_image(b) +
        geom_point(color='red', size=5) +
        coord_cartesian(xlim = c(0, dim(b)[2]), ylim = c(0, dim(b)[1]), 
                        expand = FALSE)

enter image description here

dww
  • 30,425
  • 5
  • 68
  • 111
  • Thanks alot for this can you also answer my previous [question](https://stackoverflow.com/questions/62670403/how-show-graph-on-full-screen?noredirect=1#comment110839237_62670403) – Nurav Jul 05 '20 at 13:51