R geom_tile ggplot2 what kind of stat is applied?

后端 未结 3 1725
渐次进展
渐次进展 2020-12-19 17:07

I used geom_tile() for plot 3 variables on the same graph... with

tile_ruined_coop<-ggplot(data=df.1[sel1,])+
  geom_tile(aes(x=bonus, y=mal         


        
3条回答
  •  佛祖请我去吃肉
    2020-12-19 17:59

    It uses stat_identity as can be seen in the documentation. You can test that easily:

    DF <- data.frame(x=c(rep(1:2, 2), 1), 
                     y=c(rep(1:2, each=2), 1), 
                     fill=1:5)
    
    #  x y fill
    #1 1 1    1
    #2 2 1    2
    #3 1 2    3
    #4 2 2    4
    #5 1 1    5
    
    p <- ggplot(data=DF) +
      geom_tile(aes(x=x, y=y, fill=fill))
    
    print(p)
    

    enter image description here

    As you see the fill value for the 1/1 combination is 5. If you use factors it's even more clear what happens:

    p <- ggplot(data=DF) +
      geom_tile(aes(x=x, y=y, fill=factor(fill)))
    
    print(p)
    

    enter image description here

    If you want to depict means, I'd suggest to calculate them outside of ggplot2:

    library(plyr)
    DF1 <- ddply(DF, .(x, y), summarize, fill=mean(fill))
    p <- ggplot(data=DF1) +
      geom_tile(aes(x=x, y=y, fill=fill))
    
    print(p)
    

    enter image description here

    That's easier than trying to find out if stat_summary can play with geom_tile somehow (I doubt it).

提交回复
热议问题