Hardly building dots over the image using ggplot2

At first I tried to use geom_raster and geom_tile to create this background, but instead turned to using an image, as I thought it would be easier.

However, I cannot force the image to occupy the entire background so that the dots overlap 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 looks like this: enter image description here

Here is a dput 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 tried this and set up the variables xmin , xmax and ymax and ymax :

 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 can also use geom_raster instead if it would be easier to recreate a background with fixed squares and colors initially in ggplot2.

+7
r ggplot2
source share
2 answers

You can try the following:

 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

+4
source share

Here's an alternative solution that I worked 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

+3
source share

All Articles