Remi.b
Remi.b

Reputation: 18219

Circles of different size with ggplot

Here is the kind of data I have

V1 = c('a','b','a','b','c','c','c','b','b','a','c','c','c','b','a','a')
V2 = c('A','A','A','B','B','C','A','B','C','C','B','B','B','C','A','B')

I'd like to make a ggplot with V1 in x-axis and V2 in y-axis. The plot should be made of filled circles which size indicates the number of interactions. For example: in x-axis == 'a', y-axis = 'B' the circle should be of a size which depends on the number of times in V1and V2 when, at the same position, there is a a in V1 and a B in V2. Does it make sense?

The same kind of information could as well be displayed on a bar graph… But I'd like to try this circle representation! Below is the bar graph.

enter image description here

And here is my code to implement this bar graph

ggplot(data=data, aes(factor(Fish_sp), fill=General.substrate)) + geom_bar(stats='bin', position=position_dodge()) + coord_flip() + xlab('Fish species')

Upvotes: 3

Views: 3526

Answers (1)

alexwhan
alexwhan

Reputation: 16026

Here's how I would do it. You need to map size to the number of occurrences, the easiest way for me to get that data is with dcast() from reshape2 followed melt(). Then the plotting is trivial:

library(reshape2)
dat <- data.frame(V1, V2)
dat.c <- dcast(dat, V1 ~ V2)
dat.m <- melt(dat.c, id.var = "V1")
ggplot(dat.m, aes(V1, variable)) + geom_point(aes(size = value))

enter image description here

Upvotes: 4

Related Questions