Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. activity yes no dontknow
  2. Social events 27 3 3
  3. Academic skills workshops 23 5 8
  4. Summer research 22 7 7
  5. Research fellowship 20 6 9
  6. Travel grants 18 8 7
  7. Resume preparation 17 4 12
  8. RAs 14 11 8
  9. Faculty preparation 13 8 11
  10. Job interview skills 11 9 12
  11. Preparation of manuscripts 10 8 14
  12. Courses in other campuses 5 11 15
  13. Teaching fellowships 4 14 16
  14. TAs 3 15 15
  15. Access to labs in other campuses 3 11 18
  16. Interdisciplinary research 2 11 18
  17. Interdepartamental projects 1 12 19
  18.  
  19. melted.data(wide.data,id.vars=c("activity"),measure.vars=c("yes","no","dontknow"),variable.name="haveused",value.name="responses")
  20.  
  21. ggplot(melted.data,aes(x=activity,y=responses))+geom_bar(aes(fill=haveused))
  22.  
  23. # Load required packages
  24. library(ggplot2)
  25. library(reshape2)
  26.  
  27. # Read in data
  28. df = read.table(text = "
  29. activity yes no dontknow
  30. Social.events 27 3 3
  31. Academic.skills.workshops 23 5 8
  32. Summer.research 22 7 7
  33. Research.fellowship 20 6 9
  34. Travel.grants 18 8 7
  35. Resume.preparation 17 4 12
  36. RAs 14 11 8
  37. Faculty.preparation 13 8 11
  38. Job.interview.skills 11 9 12
  39. Preparation.of.manuscripts 10 8 14
  40. Courses.in.other.campuses 5 11 15
  41. Teaching.fellowships 4 14 16
  42. TAs 3 15 15
  43. Access.to.labs.in.other.campuses 3 11 18
  44. Interdisciplinay.research 2 11 18
  45. Interdepartamental.projects 1 12 19", header = TRUE, sep = "")
  46.  
  47. # Melt the data frame
  48. dfm = melt(df, id.vars=c("activity"), measure.vars=c("yes","no","dontknow"),
  49. variable.name="haveused", value.name="responses")
  50.  
  51. # Reorder the levels of activity
  52. dfm$activity = factor(dfm$activity, levels = df$activity)
  53.  
  54. # Draw the plot
  55. ggplot(dfm, aes(x=activity, y=responses)) +
  56. geom_bar(aes(fill=haveused), stat = "identity") +
  57. theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.25)) + # Rotate tick mark labels
  58. guides(fill = guide_legend(reverse = TRUE)) # Reverse the order of the colours in the legend
  59.  
  60. # Get the positions of the labels
  61. library(plyr)
  62. dfm = ddply(dfm, .(activity), transform, pos = cumsum(responses) - 0.5*responses)
  63.  
  64. # Draw the plot
  65. ggplot(dfm, aes(x=activity, y=responses)) +
  66. geom_bar(aes(fill=haveused), stat = "identity") +
  67. theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.25)) +
  68. guides(fill = guide_legend(reverse = TRUE)) +
  69. geom_text(aes(label = responses, y = pos), size = 3) # labels inside the bar segments
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement