modified polar plot using ggplots or other alternative packages using R

前端 未结 1 1948
栀梦
栀梦 2021-02-06 18:21

I am trying to create nice (!) polar plot with the following data.

gr1 <- c(0, 5, 15, 20, 30, 40)
gr3 <- c(0, 5, 10, 25, 40, 60, 80)
gr2 <- c(0, 15, 25,         


        
1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-06 19:11

    I am having a hard time understanding what you want to do (bar-plot counts frequencies, you probably know that). However here is a way to assign the groups into colors:

    gr1 <- c(0, 5, 15, 20, 30, 40)
    gr3 <- c(0, 5, 10, 25, 40, 60, 80)
    gr2 <- c(0, 15, 25, 30, 40)
    
    df2<- data.frame (pos = c(gr1, gr2, gr3), group = c(rep(1, length(gr1)),
     rep(2, length(gr2)), rep(3, length(gr3))))
    df2$cpos <- cumsum (df2$pos)
    
    cx <- ggplot(df2, aes(fill = factor(group), x = cpos))
    
    cx + geom_bar(width = 1, colour = "black", position = "dodge")  + coord_polar()
    

    enter image description here

    If you want to get pos as frequencies, use the melt() function in reshape2.

    If you want to use dots as in your example, could following approach work?

    cx <- ggplot(df2, aes(y = group, x = cpos))
    
    cx + geom_point(aes(color = factor(group))) + coord_polar() + ylim(0,3)
    

    Anyway, you see the pattern? Make a plot with normal coordinates using x-axis as angle and y-axis as distance from the middle and just convert it to polar coordinates.

    enter image description here

    Answer to Edit2

    I am still wondering whether you could make a plot that makes more sense, but maybe you have a reason to do this. Here is a plot that is closer to your example. It isn't perfect. Maybe the gurus can give you a better suggestion tomorrow once they arrive their offices. In a mean while you can look for more specifications from the links of this tread.

    library(ggplot2)
    
    gr1 <- c(0, 5, 15, 20, 30, 40)
    gr3 <- c(0, 5, 10, 25, 40, 60, 80)
    gr2 <- c(0, 15, 25, 30, 40)
    
    df2<- data.frame (pos = c(gr1, gr2, gr3), group = c(rep(1, length(gr1)),
     rep(2, length(gr2)), rep(3, length(gr3))), y  = c(rep(1, length(gr1)),
     rep(2, length(gr1)), rep(2, length(gr1))))
    
    df2$cpos <- cumsum (df2$pos)
    
    cx <- ggplot(df2, aes(y = y, x = cpos))
    cx + geom_point(aes(color = factor(group)), size = 4) + geom_line(aes(x = c(0,500), y = c(1)), color = "yellow") + 
    geom_line(aes(x = c(0,500), y = c(2)), color = "blue") + scale_y_continuous(limits=c(0,2), breaks = c(0,1,2)) +
    scale_x_continuous(labels = df2$pos, breaks = df2$cpos, limits = c(0,500)) + coord_polar() +
    opts(panel.grid.major = theme_line(colour = "grey"), 
    panel.grid.minor = theme_line(colour = "grey", linetype = "dashed"), 
    panel.background = theme_rect(colour = "white"))
    

    enter image description here

    0 讨论(0)
提交回复
热议问题