Getting a stacked area plot in R

前端 未结 3 1406
你的背包
你的背包 2020-11-28 10:01

This question is a continuation of the previous question I asked.

Now I have a case where there is also a category column with Prop. So, the dataset becomes like

相关标签:
3条回答
  • 2020-11-28 10:22

    If you want to take the borders away you can use scale_x_discrete and coord_cartesian this way

     p <- ggplot(d, aes(x=Date,y=Volume,group=Platform,fill=Platform)) + geom_area(position="fill")
     base_size <- 9
     p + theme_set(theme_bw(base_size=9)) + scale_x_discrete(expand = c(0, 0)) +  coord_cartesian(ylim=c(0,1))
    
    0 讨论(0)
  • 2020-11-28 10:43

    You can use stackpoly from the plotrix package:

    library(plotrix)
    #create proportions table
    pdat <- prop.table(xtabs(Prop2~Hour+Category,Dat),margin=1)
    #draw chart
    stackpoly(pdat,stack=T,xaxlab=rownames(pdat))
    #add legend
    legend(1,colnames(pdat),bg="#ffffff55",fill=rainbow(dim(pdat)[2]))
    
    0 讨论(0)
  • 2020-11-28 10:49

    You can use the ggplot2 package from Hadley Wickham for that.

    R> library(ggplot2)
    

    An example data set :

    R> d <- data.frame(t=rep(0:23,each=4),var=rep(LETTERS[1:4],4),val=round(runif(4*24,0,50)))
    R> head(d,10)
       t var val
    1  0   A   1
    2  0   B  45
    3  0   C   6
    4  0   D  14
    5  1   A  35
    6  1   B  21
    7  1   C  13
    8  1   D  22
    9  2   A  20
    10 2   B  44
    

    And then you can use ggplot with geom_area :

    R> ggplot(d, aes(x=t,y=val,group=var,fill=var)) + geom_area(position="fill")
    

    enter image description here

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