Advertisement
tomsim

Barplot Test

Aug 16th, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.05 KB | None | 0 0
  1. # This is a simple example to look at survival rate of Titanic passenger by ticket class
  2. #
  3. # Load data - note that read.csv can only read http NOT https
  4. # so change any url with https to http and hope that the web site support it
  5. tn = read.csv('http://vincentarelbundock.github.io/Rdatasets/csv/datasets/Titanic.csv')
  6. # list table column names
  7. names(tn)
  8. # list first few rows of data
  9. cat("first few rows:","\n")
  10. head(tn)
  11. # This table has data already summarized by Class, Sex, Age, Survived
  12. # Let's summarize it by passenger class and whether they survived
  13. fs = aggregate(tn$Freq, by=list(tn$Class, tn$Survived), FUN=sum)
  14. cat("Display variable freq summarized by passenger class and live/die:","\n")
  15. # the variable name by itself is a command to show what's in it
  16. fs
  17. # rename the summarized table columns so that they make more sense
  18. names(fs) = c("Class","Survived","Sum")
  19. fs
  20. # Transpose the data into a matrix to make it easier to bargraph
  21. # This is done in 2 steps
  22. # 1. order the data by the variable that will become the column and row names of the matrix
  23. # 2. use the reshape function to do the transpose
  24. # 3. set empty space in matrix with 0 so we don't get error when using them (this happen when row*col != total data row)
  25. rowOrder = order(fs$Class,fs$Survived,fs$Sum)
  26. fs = fs[rowOrder,]
  27. tfs = reshape( fs, v.name='Sum', idvar='Survived', timevar='Class', direction='wide')
  28. tfs[is.na(tfs)] = 0
  29. # set the matrix row name to make it looks nice :)
  30. row.names(tfs) = tfs$Survived
  31. tfs
  32. # create the matrix of the number part (don't need first column of yes/no)
  33. # the notation [,-1] means all row, all column minus 1st one
  34. m = as.matrix(tfs[,-1])
  35. # set the column names of the matrix to the passenger class
  36. colnames(m) = levels(fs$Class)
  37. m
  38. # Setup color to plot (see http://www.r-bloggers.com/color-palettes-in-r/ for example color palettes)
  39. colorSet = rainbow(length(row.names(m)))
  40. # Finally, we get to plot the data!
  41. barplot(m, col=colorSet)
  42. # And set the legend text
  43. legend('topleft', legend=row.names(m), col=colorSet, title='Survived', fill=colorSet)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement