Plot polygons on image by pixel coordinates

27 views
Skip to first unread message

Doug Mitarotonda

unread,
Jul 29, 2015, 1:00:57 PM7/29/15
to ggplot2
Hey everyone, 
I have been given an image (PNG file) and a data frame of a bunch of coordinates, in the units of pixels relative to the image, and asked to draw polygons on top of the image using the coordinates. I have made some progress on getting this to work, but can’t get the pixel coordinates to line up with the ggplot coordinates. I have provided a MWE using the R logo below. My questions are: 
1) is the general approach I took make sense? I have not worked with images like this before, and found this method by searching the web 
2) if this is the right approach, how do I correctly plot the polygon? 

###
library(png)
library(grid)
# I downloaded and saved the R logo from http://r-project.org, or see attached.
img <- readPNG("~/Desktop/Rlogo.png")
g <- rasterGrob(img)
xy <- data.frame(x = c(80,  80, 116, 116, 80), y = c(42, 155, 155,  42, 42))
# The original coordinates are standard computer graphics, where positive y goes down, so I need to transform them to cartesian
xy_transform <- xy
xy_transform$y <- nrow(g$raster) - xy$y
library(ggplot2)
ggplot(xy_transform) + 
annotation_custom(g, -Inf, Inf, -Inf, Inf) +
geom_polygon(aes(x = x, y = y), color = "red", fill = "red", alpha = 0.5) +
xlim(c(0, ncol(g$raster))) + ylim(c(0, nrow(g$raster))) 
ggsave("~/Desktop/Rlogo-R.pdf”)
###

I have attached the output from ggplot as well as what the actual pixels look like when I draw the same rectangle using GIMP. 

Thanks!
Doug
Rlogo-gimp.png
Rlogo.png
Rlogo-R.pdf

Brandon Hurr

unread,
Jul 29, 2015, 2:57:57 PM7/29/15
to Doug Mitarotonda, ggplot2
Doug, 

I don't have a complete answer, but I'll show you where I've got. 

Initially had some issues with too new of a libpng..., but a restart fixed it. 

img <- readPNG("~/Desktop/Rlogo.png")
g <- rasterGrob(img)
xy <- data.frame(x = c(80,  80, 116, 116, 80), y = c(42, 155, 155,  42, 42))
# The original coordinates are standard computer graphics, where positive y goes down, so I need to transform them to cartesian
xy_transform <- xy
xy_transform$y <- nrow(g$raster) - xy$y

library(ggplot2)
ggplot(xy_transform) +
annotation_custom(g, -Inf, Inf, -Inf, Inf) +
geom_polygon(aes(x = x, y = y), color = "red", fill = "red", alpha = 0.5) +
coord_fixed() +
scale_y_continuous(expand = c(0,0), limits = c(0, dim(img)[1])) +
scale_x_continuous(expand = c(0,0), limits = c(0, dim(img)[2]))
I believe the issue is actually that ggplot adds padding around the plot and because your image is stretch, I've prevented this by using coord_fixed(), the expand argument in scales, and setting the limits to the size of the photo itself. This gets fairly close to what you want (I think). 

HTH, 
Brandon

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Doug Mitarotonda

unread,
Jul 29, 2015, 3:09:58 PM7/29/15
to Brandon Hurr, ggplot2
Hey Brandon,
Your solution works perfectly! That is a very clever use of the coordinate pieces. Thanks so much for your help.

Cheers,
Doug
Reply all
Reply to author
Forward
0 new messages