Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having a hard time lining up points over an image using ggplot2

Tags:

r

ggplot2

I initially tried using geom_raster and geom_tile to create this background but instead resorted to using an image as I thought it would be easier.

However, I can't get the image to take up the whole background so the points overlay on top of it correctly. The background image is here: http://imgur.com/a/eZGAP

It should look like this: enter image description here

But it's coming out like this: enter image description here

Here's a dput of the data:

structure(list(Industry.Group = c("Defense / Military", "Energy", "Financial Services", "Healthcare", "Manufacturing", "Public Sector (non-defense) / Not For Profit", "Retail", "Services", "Technology", "Telecommunications", "Transportation" ), ciq_tech = c(55.9, 53.7111111111111, 60.743661971831, 45.0620689655172, 42.1529411764706, 52.2444444444444, 40.8, 52.3263157894737, 50.9222222222222, 59.5111111111111, 39.6666666666667), Industry.Group = c("Defense / Military", "Energy", "Financial Services", "Healthcare", "Manufacturing", "Public Sector (non-defense) / Not For Profit", "Retail", "Services", "Technology", "Telecommunications", "Transportation"), ciq_org = c(39.6666666666667, 48.7413636363636, 48.0868442622951, 42.2482222222222, 39.0128260869565, 38.482, 39.7786956521739, 41.7248387096774, 47.8644262295082, 49.6739285714286, 41.4825)), .Names = c("Industry.Group", "ciq_tech", "Industry.Group", "ciq_org"), row.names = c(NA, -11L), class = "data.frame")

I've tried this and have been adjusting the xmin, xmax and ymin and ymax variables:

library(png)
library(grid)
library(ggplot2)
img <- readPNG("grid.png") 
g <- rasterGrob(img, interpolate=TRUE) 

ggplot(both, aes(x = ciq_org, y = ciq_tech))  +
  theme_bw() + annotation_custom(g, xmin=31, xmax=57, ymin=38, ymax=61) + geom_point()

As well as:

ggplot(both, aes(x = ciq_org, y = ciq_tech))  +
  theme_bw() + annotation_custom(g, xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) + geom_point()

I'm also open to using geom_raster instead if it'd be easier to recreate the background with the fixed squares and colors natively in ggplot2.

like image 235
tcash21 Avatar asked Feb 07 '17 18:02

tcash21


2 Answers

You can try this:

ggplot(both, aes(x = ciq_org, y = ciq_tech))  +
  theme_bw() + annotation_custom(g, xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) + 
  geom_point(size=5, col='steelblue') + 
  xlim(c(31, 57)) +
  ylim(c(38,61)) +
  theme_void()

enter image description here

like image 161
Sandipan Dey Avatar answered Sep 21 '22 08:09

Sandipan Dey


Here's an alternative solution I was working on:

img <- png::readPNG("KRWa02Q.png", info = TRUE)
dimensions <- attr(img, "info")$dim

 p <- ggplot(both, aes(x = ciq_org, y = ciq_tech))  +
        annotation_raster(img, xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, interpolate = TRUE) +
        geom_point(size = 3) +
        theme(axis.ticks = element_blank(), axis.line = element_blank(), axis.title = element_blank(), axis.text = element_blank(),
              panel.grid = element_blank(), panel.border = element_blank(), panel.background = element_rect(fill = NA),
              aspect.ratio = dimensions[1]/dimensions[2],
              plot.margin = margin(0, 0, 0, 0, "in")) +
        scale_x_continuous(expand = c(0.15, 0.15)) +
        scale_y_continuous(expand = c(0.15, 0.15))

ggsave("tc.png", p, width = dimensions[1]/72, height = dimensions[2]/72, units = "in", dpi = 72)

Draft of image for tcash21

like image 23
Mikhail Popov Avatar answered Sep 23 '22 08:09

Mikhail Popov