Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. ggplot(survey,aes(x=age))+stat_bin(aes(n=nrow(h3),y=..count../n), binwidth=10)
  2. +scale_y_continuous(formatter = "percent", breaks=c(0, 0.1, 0.2))
  3. + facet_grid(hospital ~ .)
  4. + opts(panel.background = theme_blank())
  5.  
  6. # Using the Iris data
  7. > i <- iris
  8. > levels(i$Species)
  9. [1] "setosa" "versicolor" "virginica"
  10. > levels(i$Species) <- c("S", "Ve", "Vi")
  11. > ggplot(i, aes(Petal.Length)) + stat_bin() + facet_grid(Species ~ .)
  12.  
  13. hospital_names <- list(
  14. 'Hospital#1'="Some Hospital",
  15. 'Hospital#2'="Another Hospital",
  16. 'Hospital#3'="Hospital Number 3",
  17. 'Hospital#4'="The Other Hospital"
  18. )
  19.  
  20. hospital_labeller <- function(variable,value){
  21. return(hospital_names[value])
  22. }
  23.  
  24. ggplot(survey,aes(x=age)) + stat_bin(aes(n=nrow(h3),y=..count../n), binwidth=10)
  25. + facet_grid(hospital ~ ., labeller=hospital_labeller)
  26. ...
  27.  
  28. plot_labeller <- function(variable,value){
  29. if (variable=='facet1') {
  30. return(facet1_names[value])
  31. } else {
  32. return(facet2_names[value])
  33. }
  34. }
  35.  
  36. plot_labeller <- function(variable,value){
  37. if (variable=='facet1') {
  38. return(facet1_names[value])
  39. } else if (variable=='facet2') {
  40. return(facet2_names[value])
  41. } else {
  42. return(as.character(value))
  43. }
  44. }
  45.  
  46. hospital_names <- c(
  47. `Hospital#1` = "Some Hospital",
  48. `Hospital#2` = "Another Hospital",
  49. `Hospital#3` = "Hospital Number 3",
  50. `Hospital#4` = "The Other Hospital"
  51. )
  52.  
  53. ... + facet_grid(hospital ~ ., labeller = as_labeller(hospital_names))
  54.  
  55. library(ggplot2)
  56. labeli <- function(variable, value){
  57. names_li <- list("versicolor"="versi", "virginica"="virg")
  58. return(names_li[value])
  59. }
  60.  
  61. dat <- subset(iris,Species!="setosa")
  62. ggplot(dat, aes(Petal.Length)) + stat_bin() + facet_grid(Species ~ ., labeller=labeli)
  63.  
  64. labeli2 <- function(variable, value){
  65. value <- droplevels(value)
  66. names_li <- list("versicolor"="versi", "virginica"="virg")
  67. return(names_li[value])
  68. }
  69.  
  70. dat <- subset(iris,Species!="setosa")
  71. ggplot(dat, aes(Petal.Length)) + stat_bin() + facet_grid(Species ~ ., labeller=labeli2)
  72.  
  73. facet_grid( hospital ~ room, labeller = labeller(hospital = as_labeller(hospital_names)))
  74.  
  75. facet_grid( hospital ~ room, labeller = labeller(hospital = as_labeller(hospital_names),
  76. room = as_labeller(room_names)))
  77.  
  78. facet_grid(
  79. yfacet~xfacet,
  80. labeller = labeller(
  81. yfacet = c(`0` = "an y label", `1` = "another y label"),
  82. xfacet = c(`10` = "an x label", `20` = "another x label)
  83. )
  84. )
  85.  
  86. ggplot(transform(survey, survey = factor(survey,
  87. labels = c("Hosp 1", "Hosp 2", "Hosp 3", "Hosp 4"))), aes(x = age)) +
  88. stat_bin(aes(n = nrow(h3),y=..count../n), binwidth = 10) +
  89. scale_y_continuous(formatter = "percent", breaks = c(0, 0.1, 0.2)) +
  90. facet_grid(hospital ~ .) +
  91. opts(panel.background = theme_blank())
  92.  
  93. plot_labeller <- function(variable,value, facetVar1='<name-of-1st-facetting-var>', var1NamesMapping=<pass-list-of-name-mappings-here>, facetVar2='', var2NamesMapping=list() )
  94. {
  95. #print (variable)
  96. #print (value)
  97. if (variable==facetVar1)
  98. {
  99. value <- as.character(value)
  100. return(var1NamesMapping[value])
  101. }
  102. else if (variable==facetVar2)
  103. {
  104. value <- as.character(value)
  105. return(var2NamesMapping[value])
  106. }
  107. else
  108. {
  109. return(as.character(value))
  110. }
  111. }
  112.  
  113. clusteringDistance_names <- list(
  114. '100'="100",
  115. '200'="200",
  116. '300'="300",
  117. '400'="400",
  118. '600'="500"
  119. )
  120.  
  121. plot_labeller <- function(variable,value, facetVar1='clusteringDistance', var1NamesMapping=clusteringDistance_names, facetVar2='', var1NamesMapping=list() )
  122.  
  123. ggplot() +
  124. facet_grid(clusteringDistance ~ . , labeller=plot_labeller)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement