3

I am trying to make a Radar plot as in attached image using and ggplot2 ( or any other package in R).This talk about this but my case is different as i am trying to create a spider plot for response data with certain range. I made a plot using a code as below, but i couldn't figure out howto make this like in the image. Kindly help me with this.

Impcts <- c("system","supply","security","well-being")
present <- c(5,5,3,5)
past <- c(6,6,4,5)
group.names <- c("present", "past")
ddf.pre <- data.frame(matrix(c(rep(group.names[1], 4), Impcts), nrow = 4,      ncol = 2), var.order = seq(1:4), value = present)
ddf.pas <- data.frame(matrix(c(rep(group.names[2], 4), Impcts), nrow = 4, ncol = 2), var.order = seq(1:4), value = past)
 ddf <- rbind(ddf.pre, ddf.pas)
 colnames(ddf) <- c("Group", "Impcts", "var.order", "var.value")
 library(ggplot2)
 ggplot(ddf, aes(y = var.value, x = reorder(Impcts, var.order),
 group =   Group, colour = Group))+
 coord_polar() + 
 geom_path() +
 geom_point()+
 labs(title = "Impacts Analysis").

enter image description here

Community
  • 1
  • 1
Cirrus
  • 638
  • 3
  • 13
  • 26

1 Answers1

4

Here is my attempt.First I drew squares using geom_path(). Then, I drew two polygons on top of the squares using geom_polygon(). Finally I added annotations.

### Draw squares
mydf <- data.frame(id = rep(1:6, each = 5),
                   x = c(0, 6, 0, -6, 0,
                         0, 5, 0, -5, 0,
                         0, 4, 0, -4, 0,
                         0, 3, 0, -3, 0,
                         0, 2, 0, -2, 0,
                         0, 1, 0, -1, 0),
                   y = c(6, 0, -6, 0, 6,
                         5, 0, -5, 0, 5,
                         4, 0, -4, 0, 4,
                         3, 0, -3, 0, 3,
                         2, 0, -2, 0, 2,
                         1, 0, -1, 0, 1))

g <- ggplot(data = mydf, aes(x = x, y = y, group = factor(id)) +
     geom_path()

### Draw polygons
mydf2 <- data.frame(id = rep(7:8, each = 5),
                    x = c(0, 6, 0, -5, 0,
                          0, 5, 0, -5, 0),
                    y = c(6, 0, -4, 0, 6,
                          5, 0, -3, 0, 5))

gg <- g +
      geom_polygon(data = mydf2, aes(x = x, y = y, group = factor(id), fill = factor(id))) +
      scale_fill_manual(name = "Time", values = c("darkolivegreen4", "brown4"),
                        labels = c("Past", "Present"))


### Add annotation

mydf3 <- data.frame(x = c(0, 6.5, 0, -6.5),
                    y = c(6.5, 0, -6.5, 0),
                    label = c("system", "supply", "security", "well-being"))


ggg <- gg + 
       annotate("text", x = mydf3$x, y = mydf3$y, label = mydf3$label, size = 3)

ggsave(ggg, file = "name.png", width = 10, height = 9)

enter image description here

jazzurro
  • 23,179
  • 35
  • 66
  • 76
  • Dear @jazzurro, thank you for nice workout and proposed solution. In my case i have to make more than 30 different radar charts. In each case the value of present and past changes based on the responses (range from 0-10).So would kindly suggest me what would be the possible modifications on your code so that it can be used with different values of present and past. Change in value of past and present will change the shape of the polygons.Thank you. – Cirrus Apr 05 '15 at 02:42
  • 1
    @Cirrus Thanks for your comment. 1) I you have a rating scale of 1-10, you want to draw 10 squares in the first step. Here I have a 1-6 scale. You can easily modify this part. 2) If you have more than 30 charts, you may want to consider using `lapply()` with ggplot functions. You can create a large data frame including data from all points. You subset the data using a specific location name or something like that. As long as you specify x and y values as I did, you can draw polygons for the charts. Hope this helps you to some extent. – jazzurro Apr 05 '15 at 03:14
  • Thank you @jazzuuro. I will try to use facet for multiple plots. Thank you. – Cirrus Apr 05 '15 at 05:52
  • @Cirrus Hope you can make more progress. Good luck. :) – jazzurro Apr 05 '15 at 05:54
  • If some of you need more examples: I also used `geom_polygon` in one of [my implementations](https://github.com/region-spotteR/PrepPlot) – 5th Oct 29 '17 at 11:13