Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using geom_path from ggplot library

Tags:

plot

r

ggplot2

I have 12 variables, M1, M2, ..., M12, for which I compute a certain statistic x.

 df = data.frame(model = factor(paste("M", 1:28, sep = ""), levels=paste("M", 1:28, sep = "")), x = runif(28, 1, 1.05))

 levels = seq(0.8, 1.2, 0.05)

I would like to plot this data as follows:

enter image description here

Each circle (contour) represents the a level of that statistic "x". The three blue lines simply represent three different scenarios.

The dataframe included in this example represents one scenario. The blue line would simply join the values of all the models M1 to M28 for that specific scenario.

I tried the following:

 ggplot(data=df, aes(x=model, y=x, group=1)) + 
   geom_line() + coord_polar() + 
   scale_y_continuous(limits=range(levels), breaks=levels, labels=levels) +
   theme(axis.text.y = element_blank(), axis.ticks = element_blank(), axis.title.x = element_blank(), axis.title.y = element_blank())

However, I get a disconnected path (between M28 and M1)

enter image description here

Then, I replicated the first row and placed it at the bottom of the dataframe (see below), and then used geom_path() instead of geom_line(), but I didn't get the result I was looking for:

 ## Replicating the first row (model1) and placing it at end of dataframe
 df = rbind(df, df[1,])

 ## using geom_path()

 ggplot(data=df, aes(x=model, y=lg, group=1)) + 
   geom_path() + coord_polar() + 
   scale_y_continuous(limits=range(levels), breaks=levels, labels=levels) +
   theme(axis.text.y = element_blank(), axis.ticks = element_blank(), axis.title.x =  element_blank(), axis.title.y = element_blank())

enter image description here

Could any please help me achieve the result that I am looking for? Any help would be appreciated. Thanks!

like image 370
Mayou Avatar asked Feb 03 '14 15:02

Mayou


1 Answers

You have to use geom_polygon for closed paths:

library(ggplot2)
ggplot(data=df, aes(x=model, y=x, group=1)) + 
  geom_polygon(fill = NA, colour = "black") + 
  coord_polar() + 
  scale_y_continuous(limits=range(levels), breaks=levels, labels=levels) +
  theme(axis.text.y = element_blank(), axis.ticks = element_blank(), 
        axis.title.x = element_blank(), axis.title.y = element_blank())

enter image description here

like image 158
Sven Hohenstein Avatar answered Nov 12 '22 19:11

Sven Hohenstein