Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Radial plot using ggplot2

Tags:

r

ggplot2

I'm trying to create piechart similar to radial plot (plotrix), in ggplot2. Basically the slices would have different length.

radii <- c(2,3,2,1,3,1,2,3,2)
color <- c("lightgrey", "chartreuse", "lightgrey", "darkturquoise", "darkolivegreen3",
           "orangered", "lightgrey", "darkseagreen1", "lightgrey")

radial.pie(radii, labels = NA, sector.colors = color,
          show.grid = F, show.grid.labels = F ,show.radial.grid = T,
          radial.labels = F, clockwise = T,start=3)

Is there an easy way to do this? The reason for doing it in ggplot is that I want to have a this piechart on top of a ggplot violin plot in one page using plot_grid.

Radial plot using plotrix

like image 401
may m Avatar asked Apr 19 '26 06:04

may m


1 Answers

This answer was copied from:

Making polar plots with ggplot2 by Carolyn Parkinson (April 10, 2015)

http://rstudio-pubs-static.s3.amazonaws.com/72298_c1ba7f77276a4f27a0f375cadc9fac5d.html

Basically, all you have to do is plot a bar plot with coord_ploar() to make it this kind of radial plot:

require(ggplot2)

# function to compute standard error of mean
se <- function(x) sqrt(var(x)/length(x)) 
set.seed(9876) 

DF <- data.frame(variable = as.factor(1:10),
                 value = sample(10, replace = TRUE))

ggplot(DF, aes(variable, value, fill = variable)) +
    geom_bar(width = 1, stat = "identity", color = "white") +
    geom_errorbar(aes(ymin = value - se(DF$value), 
                      ymax = value + se(DF$value), 
                      color = variable), 
                      width = .2) + 
    scale_y_continuous(breaks = 0:nlevels(DF$variable)) +
    theme_gray() +
    theme(axis.ticks = element_blank(),
          axis.text = element_blank(),
          axis.title = element_blank(),
          axis.line = element_blank()) +
    coord_polar() 

enter image description here

like image 142
divibisan Avatar answered Apr 21 '26 22:04

divibisan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!