0

I have a matrix data as follows:

df <- matrix(c(18, 54, 33, 75, 24, 15, 23, 15, 8), nrow=3, byrow=T)
dimnames(df) <- list(c('a', 'b', 'c'), c('low', 'moderate', 'high'))
names(dimnames(df))<-c("medicine",  "level")
prop.table(df)
prop.table(df, 1)
prop.table(df, 2)

Output:

         low   moderate       high
a 0.06792453 0.20377358 0.12452830
b 0.28301887 0.09056604 0.05660377
c 0.08679245 0.05660377 0.03018868
        low  moderate      high
a 0.1714286 0.5142857 0.3142857
b 0.6578947 0.2105263 0.1315789
c 0.5000000 0.3260870 0.1739130
        low  moderate      high
a 0.1551724 0.5806452 0.5892857
b 0.6465517 0.2580645 0.2678571
c 0.1982759 0.1612903 0.1428571

How can I plot those proportion data properly in R? Thanks for your help.

ah bon
  • 9,293
  • 12
  • 65
  • 148
  • How to plot it? Soory I'm new in R. – ah bon Nov 30 '19 at 01:27
  • `ggplot(reshape2::melt(prop.table(df)), aes(level, value, col=medicine, group=medicine)) + geom_point() + geom_line()` – user20650 Nov 30 '19 at 01:27
  • `Error in FUN(X[[i]], ...) : can't find 'level'`, I get this error. – ah bon Nov 30 '19 at 01:30
  • `reshape2::melt(prop.table(df))`, works for me as well, but the whole line of `ggplot` doesn't. I rechecked, I have installed `reshape2`. – ah bon Nov 30 '19 at 01:37
  • Maybe need to rename column names after reshape? I try with `aes(Var1, value, col=Var2, group=Var2)`, it's OK right now. – ah bon Nov 30 '19 at 01:47
  • Should I upgrade my `reshape2` or `R`? – ah bon Nov 30 '19 at 01:51
  • 1
    oh sorry, my fault. Thanks for your help. – ah bon Nov 30 '19 at 01:54
  • Sorry another question, maybe to plot proportion values, use pie or histogram are better solution, what do you think? – ah bon Nov 30 '19 at 01:57
  • 1
    definitely not a pie chart. Try a barplot and see what presents your data best: `ggplot(reshape2::melt(prop.table(df)), aes(level, value, fill=medicine)) + geom_col(position="dodge")` – user20650 Nov 30 '19 at 01:59

0 Answers0