简体   繁体   中英

Plotting x/y coordinates onto an image in R -- points distorted

Essentially, I have an R script that takes X/Y coordinate info from a.csv, and plots them over a picture of a hand. The resolution of the picture is always 1280x1024. The coordinates are plotted successfully, and look to be in the right shape, but are distorted in that they are not overlaid on the hand 'to scale', if that makes sense (pic attached for reference, plus code).

I am very much a beginner when it comes to R -- this is part of a project for my master's -- and so any help would be appreciated. Thank you very much!

## Data
dbCoords <- read.csv("dbCoords.csv", header=TRUE, sep = ",") ## Load coordinate .csv
xCoords <- as.integer(dbCoords$x) ## Assign x-values
yCoords <- as.integer(dbCoords$y * -1) ## Assign y-values (make negative)
image <- read.bitmap("s1_Blank_Img.jpg")
rImage <- rasterGrob(image, width=unit(33.866666667,"cm"), height=unit(27.093333333,"cm")) ## Load image, set it to 1280x1024 in cm

#Graph
ggplot(dbCoords, aes(xCoords,yCoords)) + 
  annotation_custom(rImage) +
  geom_point(colour="red") +
  scale_x_continuous(expand=c(0,0), lim=c(min(xCoords),max(xCoords))) +
  scale_y_continuous(expand=c(0,0), lim=c(min(yCoords),max(yCoords))) +
  theme_void() +
  theme(aspect.ratio = nrow(image)/ncol(image)) 

RStudio 绘图输出

dput output for dbCoords.csv:

structure(list(x = c(711L, 826L, 426L, 791L, 624L, 577L, 573L, 
404L, 921L, 814L, 917L, 903L, 728L, 722L, 905L, 601L, 587L, 705L, 
415L, 721L, 588L, 902L, 733L, 795L, 716L, 803L, 915L, 845L, 404L, 
573L, 588L, 909L, 904L, 652L, 623L, 771L, 580L, 595L, 782L, 641L, 
402L, 779L, 810L, 917L, 893L, 905L, 592L, 418L, 616L, 824L, 915L, 
730L, 724L, 703L, 624L, 726L, 883L, 729L, 628L, 442L, 820L, 890L, 
803L, 708L, 799L, 794L, 580L, 914L, 903L, 585L, 419L, 591L, 590L, 
802L, 687L, 783L, 917L, 421L, 900L, 773L, 601L, 902L, 889L, 571L, 
791L, 620L, 726L, 422L, 821L, 617L, 816L, 822L, 768L, 404L, 917L, 
895L, 808L, 585L, 630L, 737L, 729L, 621L, 786L, 586L, 421L, 895L, 
891L, 598L), y = c(143L, 736L, 513L, 132L, 435L, 157L, 723L, 
440L, 318L, 298L, 401L, 535L, 317L, 340L, 218L, 80L, 326L, 220L, 
521L, 131L, 219L, 543L, 239L, 123L, 201L, 260L, 279L, 740L, 433L, 
700L, 161L, 227L, 392L, 428L, 88L, 293L, 336L, 737L, 320L, 392L, 
440L, 318L, 117L, 324L, 548L, 451L, 167L, 506L, 93L, 724L, 221L, 
273L, 134L, 171L, 82L, 205L, 554L, 121L, 415L, 472L, 701L, 307L, 
122L, 158L, 205L, 316L, 679L, 361L, 223L, 167L, 534L, 286L, 163L, 
728L, 166L, 238L, 313L, 518L, 566L, 346L, 343L, 430L, 235L, 704L, 
138L, 97L, 156L, 462L, 267L, 423L, 342L, 684L, 306L, 431L, 300L, 
203L, 116L, 179L, 408L, 274L, 100L, 95L, 235L, 230L, 522L, 440L, 
599L, 645L)), class = "data.frame", row.names = c(NA, -108L))

If this is all you are trying to do then I would probably avoid ggplot . Base R (here I've also used to the png library to load my image) gives you everything you need and it isn't any harder.

Here I'm loading the R logo and then plotting points corresponding to a square at 0,0 which is 100 pixels wide and 100 pixels high. Throughout I make sure the point coordinates are translated to 0,1 on each axis. You can see the plot always has the correct aspect ratio and I can control the point locations relative to the points on the plot.

There may be an even neater way with the imager library

image1 <- png::readPNG("Rlogo.png")
plot(NA, xlim=c(0,1), ylim=c(0,1), ann=F,axes=F,asp=dim(image1)[1]/dim(image1)[2])
par(mar=c(0,0,0,0))
rasterImage(image1,0,0,1,1)
points(x=c(0,0,100,100)/dim(image1)[2],
       y=c(0,100,0,100)/dim(image1)[1],pch=20)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM