如何缩放ggplot2中的背景图像? [英] How to scale background Image in ggplot2?

查看:34
本文介绍了如何缩放ggplot2中的背景图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的屏幕尺寸为15.6英寸.我已经拍摄了屏幕截图.我正在一个项目中,我必须绘制鼠标单击点的散点图.所以我有了鼠标单击位置的坐标我想在该图像上绘制散点图,该散点图是单击鼠标的屏幕上的点.所以当我使用ggplot2绘制图形并添加背景图像时,它会缩放图像并且散点是在与实际单击鼠标的位置不同的位置看到.

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:

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

它的长宽比正在变化,并且它所显示的点并不是实际发生点击的地方!

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

推荐答案

您应设置绘图的坐标系以匹配图像的尺寸

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)

这篇关于如何缩放ggplot2中的背景图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆