Bubble Chart in R with # of Occurrences / Sums of Values

前端 未结 2 962
我在风中等你
我在风中等你 2021-01-28 20:31

I\'m playing around with drawing bubble charts in R -- the current project is to graph a bubble chart of political donations that has the following characteristics:



        
2条回答
  •  伪装坚强ぢ
    2021-01-28 21:13

    You can use ddply from package plyr here. If your original data.frame was called dfr, then something close to this should work:

    result<-ddply(dfr, .(CTRIB_AMT), function(partialdfr){data.frame(amt=partialdfr$CTRIB_AMT[1], sm=sum(partialdfr$CTRIB_AMT), mn=mean(partialdfr$CTRIB_AMT)) })
    

    In fact, a base R solution is also rather simple:

    vals<-sort(unique(dfr$CTRIB_AMT))
    sums<-tapply( dfr$CTRIB_AMT, dfr$CTRIB_AMT, sum)
    counts<-tapply( dfr$CTRIB_AMT, dfr$CTRIB_AMT, length)
    

    I'm sure more elegant solutions exist.

提交回复
热议问题