Bubble chart in R C # places / sums of values

I play with bubble drawing in R - the current project is a graph of a bubble diagram of political donations that has the following characteristics:

x-axis: size of donation, in ranges ie $10-$19, $20-29, $30-49, etc. y-axis: number of donations of that amount area of bubble: total amount of donations 

I do not plan anything complicated, just something like:

 symbols(amount_ranges,amount_occurrences, circles=sums) 

The data is quite detailed, so there is a separate entry for each donation, and they need to be summed up to get the values โ€‹โ€‹I'm looking for.

For example, the data looks as follows (extraneous columns are deleted):

 CTRIB_NAML CTRIB_NAMF CTRIB_AMT FILER_ID John Smith $49 123456789 

It is not so difficult, but is there an easy way in R to count the number of occurrences of a certain value (for the y axis)? And add the amount of these donations (which is derived from the axes)? Or do I need to create a function that iterates through the data and compiles these numbers separately? Or pre-process the data in some way?

0
r
source share
2 answers

Here you can use ddply from the plyr package. If your original data.frame was called by dfr , then something close to it 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, the basic R solution is also quite 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 am sure there are more elegant solutions.

+2
source share

This is easy if you use the ggplot2 package with geom_point .

One of the many benefits of using ggplot is that the built-in statistics means that you do not need to summarize your data in advance. geom_point in combination with stat_sum is all you need.

Here is an example from ?geom_point . (Note that mtcars is a built-in dataset with ggplot2 .)

See the ggplot and geom_point website for more details.

 library(ggplot2) ggplot(mtcars, aes(wt, mpg)) + geom_point(aes(size = qsec)) 

enter image description here

+3
source share

All Articles