Thursday 15 August 2013

r - How to group data and then draw bar chart in ggplot2 -



r - How to group data and then draw bar chart in ggplot2 -

i have info frame (df) 3 columns e.g.

numeric1: numeric2: group(character): 100 1 200 2 b 300 3 c 400 4

i want grouping numeric1 group(character), , calculate mean each group. that:

mean(numeric1): group(character): 250 200 b 300 c

finally i'd draw bar chart using ggplot2 having group(character) on x axis =nd mean(numeric) on y axis. should like: http://i.stack.imgur.com/ifrme.png

i used

mean <- tapply(df$numeric1, df$group(character), fun=mean)

but i'm not sure if it's ok, , if it's, don't know supposed next.

try like:

res <- aggregate(numeric1 ~ group, info = df, fun = mean) ggplot(res, aes(x = group, y = numeric1)) + geom_bar(stat = "identity")

data df <- structure(list(numeric1 = c(100l, 200l, 300l, 400l), numeric2 = 1:4, grouping = structure(c(1l, 2l, 3l, 1l), .label = c("a", "b", "c"), class = "factor")), .names = c("numeric1", "numeric2", "group"), class = "data.frame", row.names = c(na, -4l))

r ggplot2 group bar-chart

No comments:

Post a Comment