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:
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)
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())
Could any please help me achieve the result that I am looking for? Any help would be appreciated. Thanks!
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())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With