I'm just starting with R and I wanted to know if it's possible to create a scatterplot with custom icons (svg or png) instead of points.

I started to import a shapefile, and then I have a set of points (latitude and longitude) that represents the location of the icons.


par(mar = c(2, 2, 2, 2))
 root <- "C:/"
Al= readShapeLines("/DZA_adm0.shp")
simple.output <- gSimplify(Al, topologyPreserve = TRUE, tol = 3)
(tab <- read.delim(file = paste(root, "/ville.txt", sep = "")))
dat <- SpatialPoints(coords = tab[, 2:3])
(dat <- SpatialPointsDataFrame(coords = tab[, 2:3], data = tab[,
                                                               + c(1, 4)]))
# Rajout des capitales
plot(dat, pch =20, add = T,col="red")

# Extraction des coordonnees
lon <- dat@coords [, "lon"]
lat <- dat@coords [, "lat"]
# Extraction du nom des villes
labs <- as.character(dat@data[, "ville"])

enter image description here


rasterImage draws a raster image at a given location and size.

Below is a very rough example, which you can hopefully adjust to your needs. (I made up some location points, you would obviously have to use yours.)


# load icons in PNG format
iconfile1 <- download.file('http://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/256/Status-weather-clouds-icon.png', destfile = 'icon1.png', mode = 'wb')
icon1 <- readPNG('icon1.png')

iconfile2 <- download.file('http://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/256/Status-weather-showers-scattered-icon.png', destfile = 'icon2.png', mode = 'wb')
icon2 <- readPNG('icon2.png')

# load shapefile
al <- readOGR("DZA", "DZA_adm0")

# make up some points
dat <- spsample (al, 3, type='random')

# need to offset the x/y location for the icon (depends on desired icon size)
offset <- 2

rasterImage(icon1, coordinates(dat)[1,1]-offset, coordinates(dat)[1,2]-offset, coordinates(dat)[1,1]+offset, coordinates(dat)[1,2]+offset)
rasterImage(icon2, coordinates(dat)[2,1]-offset, coordinates(dat)[2,2]-offset, coordinates(dat)[2,1]+offset, coordinates(dat)[2,2]+offset)
rasterImage(icon1, coordinates(dat)[3,1]-offset, coordinates(dat)[3,2]-offset, coordinates(dat)[3,1]+offset, coordinates(dat)[3,2]+offset)

enter image description here

| improve this answer | |
  • This is exactly what I want, a big thank to cengel. another question if it's possible, if I want to animate over time ,is that it's possible to animate just icons, or I have to create SEVERAL superimposed images and animate them later? – zina_GIS Oct 15 '14 at 11:15
  • 1
    @zina_GIS: Animation is a different beast, and it depends on what you want to achieve and the level of interactivity. What you propose is certainly one way. I suggest you explore alternatives and try something. Then post back a new question with details of what you have tried and where you are stuck. – cengel Oct 15 '14 at 17:12
  • @cengelok : thank you again I'll try and post here – zina_GIS Oct 15 '14 at 17:19
  • I haven't found how to make the legend in this case! is that you have an idea? – zina_GIS Oct 29 '14 at 11:58
  • 1
    @zina_GIS You can use rasterImage() to place your image as I have done above and use the text()command to place your text. – cengel Oct 29 '14 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.