5

I am trying to plot rose diagrams/ circular histograms on specific coordinates on a map analogous to drawing pie charts on a map as in the package mapplots.

Below is an example generated with mapplots (see below for code), I'd like to replace the pie charts with rose diagrams

example plot

The package circular lets me plot the rose diagrams, but I am unable to integrate it with the mapplots package. Any suggestions for alternative packages or code to achieve this?

In response to the question for the code to make the map. It's all based on the mapplots package. I downloaded a shapefile for the map (I think from http://www.freegisdata.org/)

library(mapplots)
library(shapefiles)

xlim = c(-180, 180)
ylim = c(-90, 90)

#load shapefile
wmap = read.shapefile ("xxx")

# define x,y,z for pies
x <- c(-100, 100)
y <- c(50, -50)
z1 <- c(0.25, 0.25, 0.5)
z2 <- c(0.5, 0.2, 0.3)
z <- rbind(z1,z2)
# define radii of the pies
r <- c(5, 10)

# it's easier to have all data in a single df

plot(NA, xlim = xlim, ylim = ylim, cex = 0.75, xlab = NA, ylab = NA)
draw.shape(wmap, col = "grey", border = "NA")
draw.pie(x,y,z,radius = r, col=c("blue", "yellow", "red"))
legend.pie (x = -160, y = -70, labels = c("0", "1", "2"), radius = 5,
bty = "n", cex = 0.5, label.dist=1.5, col = c("blue", "yellow", "red"))

the legend for the pie size can then be added using legend.bubble

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Lukas
  • 655
  • 8
  • 20

2 Answers2

2

Have a look at this example, you can use the map as background an plot your rose diagrams withPlotrix or ggplot2. In either case you would want to overlay multiple of these diagrams on top of your map which is easy to do in ggplot, just have a look at the example.

Community
  • 1
  • 1
Mehdi Nellen
  • 8,486
  • 4
  • 33
  • 48
  • Apologies, I don't think my question was very clear. It's not about how to plot rose diagrams, but how to get them on a map. Hopefully the example above clarifies this. – Lukas Feb 10 '14 at 13:15
  • 1
    hey lukas you could use multiple plots made in plotrix and use a `grid()` to plot them on top of your map image. you want me to clarify it for you? – Mehdi Nellen Feb 10 '14 at 13:33
  • Thanks @Mehdi Nellen. I can plot the map and the rose diagrams (using 'coord.polar') individually. However, from your example it isn't clear to me how I make sure that the rose diagrams are plotted at the right coordinates. I'd be grateful if you could clarify your suggestion to use 'grid()'. – Lukas Feb 11 '14 at 15:53
  • yes I can do that but maybe not today, could you wait until the weekend? Then I could make you an great example. – Mehdi Nellen Feb 12 '14 at 13:53
  • Thanks again @Mehdi Nellen. I kept on looking for a solution and think I found one myself. See below – Lukas Feb 12 '14 at 16:03
2

I discovered subplot() in the package Hmisc, which seems to do exactly what I wanted. Below is my solution (without the map in the background, which can be plotted using mapplots). I am open to suggestions on how to improve this though...

library(Hmisc)
library (circular)

dat <- data.frame(replicate(2,sample(0:360,10,rep=TRUE)))
lat <- c(50, -40)
lon <- c(-100, 20)

# convert to class circular
cir.dat <- as.circular (dat, type ='angles', units = 'degrees', template = 'geographic', modulo = 'asis', zero = 'pi/2', rotation = 'clock')

# function for subplot, plots relative frequencies, see rose.diag for how to adjust the plot
sub.rose <- function(x){
                nu <- sum(!is.na(x))
                de <- max(hist(x, breaks = (seq(0, 360, 30)), plot = FALSE)$counts)
                prop <- nu/de
                rose.diag(x, bins = 12, ticks = FALSE, axes = FALSE,
                radii.scale = 'linear',
                border = NA, 
                prop = prop,
                col = 'black'
                )
                }

plot(NA, xlim = xlim, ylim = ylim)
for(i in 1:length(lat)){
    subplot(sub.rose(cir.dat[,i]), x = lon[i], y = lat[i], size = c(1, 1))
    }
Lukas
  • 655
  • 8
  • 20