Guest User

Untitled

a guest
Feb 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. library(reshape)
  2. library(lattice)
  3.  
  4. #
  5. # Categorical example
  6. #
  7.  
  8. data1 <- read.csv(file="plots/categorical.csv")
  9.  
  10. # Reshape the data into long format
  11. data1 <- melt(data1,measure.var=c("Systems.biology","Functional.genomics","Non.coding.RNA"))
  12.  
  13. # Barplot of number of cups of tea per day
  14. plot1 <- bwplot(value ~ variable, data = data1)
  15. plot1$ylab <- "Cups of Tea per day"
  16.  
  17. #
  18. # Continuous example
  19. #
  20.  
  21. data2 <- read.csv(file="/Users/mike/Desktop/plots/continuous.csv")
  22.  
  23. # Plot data as a scatter plot
  24. plot2 <- xyplot(productivity ~ distance, data=data2)
  25. plot2$xlab <- "Distance to tea making area (feet)"
  26. plot2$ylab <- "Weekly productivity (hours)"
  27.  
  28. # Add a custom panel with a loess trend
  29. custom_panel_loess <- function(x,y,...){
  30. panel.xyplot(x,y,...)
  31. panel.loess(x,y)
  32. }
  33. plot2$panel <- custom_panel_loess
  34.  
  35. #
  36. # Factored catergorical data
  37. #
  38.  
  39. data3<- read.csv(file="/Users/mike/Desktop/plots/categorical_categorical.csv")
  40.  
  41. # Split data by seasons
  42. winter <- data3[,1:3]
  43. summer <- data3[,4:6]
  44.  
  45. # Convert to long format
  46. winter <- melt(winter,measure.var=c("SB","FG","ncRNA"))
  47. summer <- melt(summer,measure.var=c("SB.1","FG.1","ncRNA.1"))
  48.  
  49. # Add the name of the season as an extra column
  50. summer <- cbind(summer,season="summer")
  51. winter <- cbind(winter,season="winter")
  52.  
  53. # Convert back to a single data set
  54. data3 <- rbind(winter,summer)
  55.  
  56. # Rename the variables for consistency
  57. levels(data3$variable)[4:6] <- levels(data3$variable)[1:3]
  58.  
  59. # Print a plot of the data
  60. plot3 <- bwplot(value ~ variable | season, data=data3)
  61. plot3$ylab <- "Cups of Tea per day"
  62. print(plot3)
  63.  
  64. #
  65. # Factored continuous data
  66. #
  67.  
  68. data4 <- read.csv("/Users/mike/Desktop/plots/continuous_categorical.csv")
  69.  
  70. # Name each of the sets of data
  71. water <- cbind(data4[,1:2],drink="water")
  72. tea <- cbind(data4[,3:4],drink="tea")
  73. hipflask <- cbind(data4[,5:6],drink="hipflask")
  74.  
  75. # Name the columns and bind the data into a single dataset
  76. col.names <- c("volume","productivity")
  77. names(water)[1:2] <- col.names
  78. names(tea)[1:2] <- col.names
  79. names(hipflask)[1:2] <- col.names
  80.  
  81. data4 <- rbind(water,tea,hipflask)
  82.  
  83. # Plot the factored data
  84. plot4 <- xyplot(productivity ~ volume | drink, data=data4)
  85. plot4$xlab <- "Average volume ingested (litres / day)"
  86. plot4$ylab <- "Weekly productivity (hours)"
  87. custom_panel <- function(x,y,...){
  88.  
  89. # Add a custom panel
  90. panel.xyplot(x,y,...)
  91. panel.loess(x,y,col="red",lty=2)
  92. }
  93.  
  94. plot4$panel <- custom_panel
  95. print(plot4)
Add Comment
Please, Sign In to add comment