0
votes

I have the following table in R (inspired by a cran help datasheet) :

      > dfx <- data.frame(
        +   group = c(rep('A', 108), rep('B', 115), rep('C', 106)),
        +   sex = sample(c("M", "F","U"), size = 329, replace = TRUE),
        +   age = runif(n = 329, min = 18, max = 54)
        + )
      > head(dfx)
        group sex      age
        1     A   U 47.00788
        2     A   M 32.40236
        3     A   M 21.95732
        4     A   F 19.82798
        5     A   F 30.70890
        6     A   M 30.00830

I am interested in plotting the percentages of males (M), females (F) and "unknown"(U) in each group using barcharts, including error bars. To do this graph, i plan to use the panel.ci/prepanel.ci commands.

I can easily build a proportion table for each group using the prop.table command :

       > with(dfx, prop.table(table(group,sex), margin=1)*100)
               sex
         group        F        M        U
             A 29.62963 28.70370 41.66667
             B 35.65217 35.65217 28.69565
             C 37.73585 33.01887 29.24528

But now, i would like to build a similar table with error bars, and use these two tables to make a barchart. If possible, i would like to use the ddply command, that i use for similar purposes (except that it was nor percentages but means).

1
Since you refer to the [standard] error, I am assuming you request a box plot. Your title refers to a bar chart, however, which is slightly different.Jack Ryan

1 Answers

0
votes

Try something like this:

library(plyr)
library(ggplot2)
summary(dfx) # for example, each variable
dfx$interaction <- interaction(dfx$group, dfx$sex)
ddply(dfx, .(interaction), summary) #group by interaction, summary on dfx
ggplot(dfx, aes(x = sex, y = age, fill = group)) + geom_boxplot()

You can get a good on-line tutorial on building graphs here.

edit

I'm pretty sure you would need more than 1 value for the proportion in order to have any error. I only see 1 value for the proportion for each unique combination of variables group and sex.

This is the most I can help you with (below), but I'd be interested to see you post an answer to your own question when you find a suitable solution.

dfx$interaction <- interaction(dfx$group, dfx$sex)
dfx.summary <- ddply(dfx, .(group, sex), summarise, total = length(group)) 
dfx.summary$prop <- with(dfx.summary, total/sum(total))
dfx.summary
# group sex      prop
# 1     A   F 0.06382979
# 2     A   M 0.12158055
# 3     A   U 0.14285714
# 4     B   F 0.12462006
# 5     B   M 0.11854103
# 6     B   U 0.10638298
# 7     C   F 0.10334347
# 8     C   M 0.12158055
# 9     C   U 0.09726444
ggplot(dfx.summary, aes(sex, total, color = group)) + geom_point(size = 5)