2

Here is my sample dataset:

df1 = data.frame(Count.amp = c(8,8,1,2,2,5,8), Count.amp.1 = c(4,4,2,3,2,5,4))

I tried

library(ggplot2)
qplot(Count.amp,Count.amp.1, data=df1)  

Is there any way to plot in such a way that the size of the dot is proportional to the number of elements in each dots?

vestland
  • 55,229
  • 37
  • 187
  • 305
Kryo
  • 921
  • 9
  • 24

1 Answers1

2

Yes, broadly speaking you are looking at creating a bubble plot, this code:

df1 = data.frame(Count.amp = c(8,8,1,2,2,5,8), Count.amp.1 = c(4,4,2,3,2,5,4))
df1$sum <- df1$Count.amp + df1$Count.amp.1
ggplot(df1, aes(x=Count.amp, y=Count.amp.1, size=sum),guide=FALSE)+
  geom_point(colour="white", fill="red", shape=21)+ scale_size_area(max_size = 15)+
  theme_bw()

would give you something like that: bubble plot It wasn't immediately clear to me what do yo mean by the number of elements but on principle you can pass any figures into the size= to get the desired result.

Konrad
  • 17,740
  • 16
  • 106
  • 167
  • I was trying to convey that the size of the bubble present in the extreme right should be the largest because it contains more entries in the data.frame – Kryo Jun 04 '15 at 15:36
  • 1
    @Kryo Try something like `df1$nrep <- apply(df1, 1, function(r) sum(df1[,1] == r[1] & df1[,2] == r[2]))` and then `size=nrep`. – Molx Jun 04 '15 at 15:39
  • 1
    @Kryo I got you now. If you are simply interested in counting occurrences in a variable you have multiple options, for instance you could use `count(numbers)` from the `plyr` package. Have a look at [this discussion](http://stackoverflow.com/questions/1923273/counting-the-number-of-elements-with-the-values-of-x-in-a-vector) where possible approaches are discussed at lengths. Of course, the solution suggested by Molx should also work. – Konrad Jun 04 '15 at 15:51