Guest User

Untitled

a guest
Feb 25th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. # Define the function
  2. loghistplot <- function(data) {
  3.  
  4. require(ggplot2); require(gridExtra) # load packages
  5.  
  6. names(data) <- c('x','y') # rename columns
  7.  
  8. # get min and max axis values
  9. min_x <- min(data$x)
  10. max_x <- max(data$x)
  11. min_y <- min(data$y)
  12. max_y <- max(data$y)
  13.  
  14. # get bin numbers
  15. bin_no <- max(hist(data$x)$counts) + 5
  16.  
  17. # create plots
  18. a <- ggplot(data, aes(x = x, y = y)) +
  19. theme_bw(base_size=16) +
  20. geom_smooth(method = "glm", family = "binomial", se = TRUE,
  21. colour='black', size=1.5, alpha = 0.3) +
  22. # scale_y_continuous(limits=c(0,1), breaks=c(0,1)) +
  23. scale_x_continuous(limits=c(min_x,max_x)) +
  24. labs(panel.grid.major = element_blank(),
  25. panel.grid.minor=element_blank(),
  26. panel.background = element_blank()) +
  27. labs(y = "Probability\n", x = "\nYour X Variable")
  28.  
  29. b <- ggplot(data[data$y == unique(data$y)[1], ], aes(x = x)) +
  30. theme_bw(base_size=16) +
  31. geom_histogram(fill = "grey") +
  32. scale_y_continuous(limits=c(0,bin_no)) +
  33. scale_x_continuous(limits=c(min_x,max_x)) +
  34. labs(panel.grid.major = element_blank(),
  35. panel.grid.minor=element_blank(),
  36. axis.text.y = element_blank(),
  37. axis.text.x = element_blank(),
  38. axis.ticks = element_blank(),
  39. panel.border = element_blank(),
  40. panel.background = element_blank()) +
  41. labs(y='\n', x='\n')
  42.  
  43. c <- ggplot(data[data$y == unique(data$y)[2], ], aes(x = x)) +
  44. theme_bw(base_size=16) +
  45. geom_histogram(fill = "grey") +
  46. scale_y_continuous(trans='reverse') +
  47. scale_y_continuous(trans='reverse', limits=c(bin_no,0)) +
  48. scale_x_continuous(limits=c(min_x,max_x)) +
  49. labs(panel.grid.major = element_blank(),panel.grid.minor=element_blank(),
  50. axis.text.y = element_blank(), axis.text.x = element_blank(),
  51. axis.ticks = element_blank(),
  52. panel.border = element_blank(),
  53. panel.background = element_blank()) +
  54. labs(y='\n', x='\n')
  55.  
  56. grid.newpage()
  57. pushViewport(viewport(layout = grid.layout(1,1)))
  58.  
  59. vpa_ <- viewport(width = 1, height = 1, x = 0.5, y = 0.5)
  60. vpb_ <- viewport(width = 1, height = 1, x = 0.5, y = 0.5)
  61. vpc_ <- viewport(width = 1, height = 1, x = 0.5, y = 0.5)
  62.  
  63. print(b, vp = vpb_)
  64. print(c, vp = vpc_)
  65. print(a, vp = vpa_)
  66. }
  67.  
  68. # Examples
  69. # loghistplot(mtcars[,c("mpg","vs")])
  70. # loghistplot(movies[,c("rating","Action")])
  71.  
  72. logpointplot <- function(data) {
  73.  
  74. require(ggplot2); require(gridExtra) # load packages
  75.  
  76. names(data) <- c('x','y') # rename columns
  77.  
  78. # get min and max axis values
  79. min_x <- min(data$x)
  80. max_x <- max(data$x)
  81. min_y <- min(data$y)
  82. max_y <- max(data$y)
  83.  
  84. # create plots
  85. ggplot(data, aes(x = x, y = y)) +
  86. theme_bw(base_size=16) +
  87. geom_point(alpha = 0.5, position = position_jitter(w=0, h=0.02)) +
  88. geom_smooth(method = "glm", family = "binomial", se = TRUE,
  89. colour='black', size=1.5, alpha = 0.3) +
  90. scale_x_continuous(limits=c(min_x,max_x)) +
  91. labs(panel.grid.major = element_blank(),
  92. panel.grid.minor=element_blank(),
  93. panel.background = element_blank()) +
  94. labs(y = "Probability\n", x = "\nYour X Variable")
  95.  
  96. }
  97. # Examples
  98. # logpointplot(mtcars[,c("mpg","vs")])
  99. # logpointplot(movies[,c("rating","Action")])
Add Comment
Please, Sign In to add comment