r - aggregate/sum with ggplot -
is there way sum data ggplot2
?
i want bubble map size depending of sum of z.
currently i'm doing
dd <- ddply(d, .(x,y), transform, z=sum(z)) qplot(x,y, data=dd, size=z)
but feel i'm writing same thing twice, able write something
qplot(x,y, data=dd, size=sum(z))
i had @ stat_sum
, stat_summmary
i'm not sure appropriate either.
is possible ggplot2
? if not, best way write 2 lines.
it can done using stat_sum
within ggplot2. default, dot size represents proportions. dot size represent counts, use size = ..n..
aesthetic. counts (and proportions) third variable can obtained weighting third variable (weight = cost
) aesthetic. examples, first, data.
library(ggplot2) set.seed = 321 # generate somme data df <- expand.grid(x = seq(1:5), y = seq(1:5), keep.out.attrs = false) df$count = sample(1:25, 25, replace = f) library(plyr) new <- dlply(df, .(count), function(data) matrix(rep(matrix(c(data$x, data$y), ncol = 2), data$count), byrow = true, ncol = 2)) df2 <- data.frame(do.call(rbind, new)) df2$cost <- 1:325
the data contains units categorised according 2 factors: x1 , x2; , third variable cost of each unit.
plot 1: plots proportion of elements @ each x1 - x2 combination. group=1
tells ggplot calculate proportions out of total number of units in data frame.
ggplot(df2, aes(factor(x1), factor(x2))) + stat_sum(aes(group = 1))
plot 2: plots number of elements @ each x1 - x2 combination.
ggplot(df2, aes(factor(x1), factor(x2))) + stat_sum(aes(size = ..n..))
plot 3: plots cost of elements @ each x1 - x2 combination, weight
third variable.
ggplot(df2, aes(x=factor(x1), y=factor(x2))) + stat_sum(aes(group = 1, weight = cost, size = ..n..))
plot 4: plots proportion of total cost of elements in data frame @ each x1 - x2 combination
ggplot(df2, aes(x=factor(x1), y=factor(x2))) + stat_sum(aes(group = 1, weight = cost))
plot 5: plots proportions, instead of proportion being out of total cost across elements in data frame, proportion out of cost elements within each category of x1. is, within each x1 category, major cost x2 units occur?
ggplot(df2, aes(x=factor(x1), y=factor(x2))) + stat_sum(aes(group = x1, weight = cost))
Comments
Post a Comment