Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.78 KB | None | 0 0
  1. library(ggplot2)
  2. library(dplyr)
  3.  
  4. #specify color palette
  5. b.navHexRGB <- c(green=rgb(149,214,0, maxColorValue=255),
  6. red=rgb(229,60,46, maxColorValue=255),
  7. gray=rgb(85,87,89, maxColorValue=255),
  8. dark_green=rgb(100,140,26, maxColorValue=255),
  9. yellow=rgb(255,183,24, maxColorValue=255),
  10. purple=rgb(139,24,155, maxColorValue=255),
  11. blue=rgb(0,147,201, maxColorValue = 255))
  12.  
  13. #create plot
  14. ggplot(data = df, aes(x=as.character(bill_yrmo), y=mean_kwh)) +
  15. geom_line(aes(group = treatment, colour = treatment, linetype = treatment),
  16. size = .9) +
  17. geom_point(aes(group = treatment, colour=treatment),
  18. size = 1.5) +
  19. geom_point(data = df %>% mutate(treatment= 'Indicates the difference is statistically significant'),
  20. aes(y=stat_sig, colour=treatment),
  21. size = 2.5,
  22. shape=4,
  23. na.rm=T) +
  24. guides(colour=guide_legend(nrow=3)) +
  25. scale_color_manual(name= "Variable",values=c(palette(b.navHexRGB)), breaks=c("Control","Recipient","Indicates the difference is statistically significant")) +
  26. scale_linetype_manual(name="Variable",values=c(1,2), breaks=c("Control","Recipient","Indicates the difference is statistically significant")) +
  27. ylab("Average Daily Consumption (kWh)") +
  28. xlab("Year-Month") +
  29. theme_bw() +
  30. theme(legend.title = element_blank(),
  31. legend.justification = c(0,0),
  32. legend.position = "bottom",
  33. legend.key = element_rect(fill = "white",colour = "white"),
  34. #legend.key.width = unit(1.1, "cm"),
  35. axis.text.x = element_text(angle=45, hjust=1, color="black"),
  36. axis.text.y = element_text(color="black"),
  37. axis.title.y = element_text(vjust=1)
  38. )
  39.  
  40. df <- structure(list(treatment = structure(c(1L, 2L, 1L, 2L, 1L, 2L,
  41. 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
  42. 1L, 2L), .Label = c("Control", "Recipient"), class = "factor"),
  43. bill_month = c(9, 9, 10, 10, 11, 11, 12, 12, 1, 1, 2, 2,
  44. 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8), bill_year = c(2013,
  45. 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2014, 2014, 2014,
  46. 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014,
  47. 2014, 2014, 2014), bill_yrmo = c(201309, 201309, 201310,
  48. 201310, 201311, 201311, 201312, 201312, 201401, 201401, 201402,
  49. 201402, 201403, 201403, 201404, 201404, 201405, 201405, 201406,
  50. 201406, 201407, 201407, 201408, 201408), mean_kwh = c(34.1891698781763,
  51. 34.8263665605318, 22.998584869823, 23.6329516672246, 21.0428206185862,
  52. 21.7774153609304, 25.4992975653725, 25.8397296039854, 28.74368522348,
  53. 29.200670842288, 29.8474912589325, 30.373483172434, 26.7411627390396,
  54. 26.4600472396878, 21.628265542195, 21.3047667878863, 19.502019234349,
  55. 19.062337524723, 24.1381516068859, 24.3165665754673, 27.8915927136898,
  56. 28.3625761820341, 26.8570348685593, 27.1359185596385), p.value = c(9.36594553258583e-07,
  57. 9.36594553258583e-07, 1.76373182797948e-13, 1.76373182797948e-13,
  58. 2.12425701682086e-15, 2.12425701682086e-15, 0.00415203493379312,
  59. 0.00415203493379312, 0.00109178463449181, 0.00109178463449181,
  60. 0.00122110380638705, 0.00122110380638705, 0.0438138636035026,
  61. 0.0438138636035026, 0.00140538140516743, 0.00140538140516743,
  62. 5.74367939388898e-07, 5.74367939388898e-07, 0.100848768452669,
  63. 0.100848768452669, 0.000172505914392074, 0.000172505914392074,
  64. 0.145110211153141, 0.145110211153141), stat_sig = c(19, 19,
  65. 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
  66. 19, NA, NA, 19, 19, NA, NA)), .Names = c("treatment", "bill_month",
  67. "bill_year", "bill_yrmo", "mean_kwh", "p.value", "stat_sig"), class = c("tbl_df",
  68. "tbl", "data.frame"), row.names = c(NA, -24L))
  69.  
  70. ggplot(data = df, aes(x=as.character(bill_yrmo), y=mean_kwh, color = treatment, lty = treatment)) +
  71. geom_line(size = .9, aes(group = treatment)) +
  72. geom_point(size = 1.5) +
  73. geom_point(data = df %>% mutate(treatment= 'Indicates the difference is statistically significant'),
  74. aes(y=stat_sig, colour=treatment),
  75. size = 2.5,
  76. shape=4,
  77. na.rm=T) +
  78. scale_color_manual(name = "Variable", values = c("Recipient" = b.navHexRGB[["gray"]], "Control" = b.navHexRGB[["green"]], "Indicates the difference is statistically significant" = b.navHexRGB[["red"]]), breaks=c("Control","Recipient","Indicates the difference is statistically significant")) +
  79. scale_linetype_manual(name="Variable",values = c("Recipient" = 2, "Control" = 1, "Indicates the difference is statistically significant" = 0), breaks=c("Control","Recipient","Indicates the difference is statistically significant")) +
  80. labs(x = "Year-Month", y = "Average Daily Consumption (kWh)") +
  81. theme_bw() +
  82. theme(legend.title = element_blank(),
  83. legend.justification = c(0,0),
  84. legend.position = "bottom",
  85. legend.key = element_rect(fill = "white",colour = "white"),
  86. legend.direction = "vertical",
  87. axis.text.x = element_text(angle=45, hjust=1, color="black"),
  88. axis.text.y = element_text(color="black"),
  89. axis.title.y = element_text(vjust=1)
  90. )
  91.  
  92. dd <- rbind(df, df %>%
  93. mutate(treatment= 'Indicates the difference is statistically significant',
  94. mean_kwh = stat_sig))
  95.  
  96. #create plot
  97. ggplot(data = dd, aes(x=as.character(bill_yrmo), y=mean_kwh)) +
  98. geom_point(aes(group = treatment, colour = treatment, shape = treatment),
  99. size = 1.5) +
  100. geom_line(aes(group = treatment, colour = treatment, linetype = treatment),
  101. size = .9) +
  102. scale_shape_manual(values = c(1, 2, 4)) +
  103. scale_color_manual(values = c(palette(b.navHexRGB)),
  104. breaks = c("Control", "Recipient", "Indicates the difference is statistically significant")) +
  105. scale_linetype_manual(values = c(1, 2, 0),
  106. breaks = c("Control","Recipient","Indicates the difference is statistically significant")) +
  107. labs(y = "Average Daily Consumption (kWh)",
  108. x = "Year-Month") +
  109. theme_bw() +
  110. theme(legend.title = element_blank(),
  111. legend.justification = c(0,0),
  112. legend.position = "bottom",
  113. legend.key = element_rect(fill = "white",colour = "white"),
  114. axis.text.x = element_text(angle=45, hjust=1, color="black"),
  115. axis.text.y = element_text(color="black"),
  116. axis.title.y = element_text(vjust=1)
  117. )
  118.  
  119. ggplot(data = avgkwh_pre2, aes(x=as.character(bill_yrmo), y=mean_kwh)) +
  120. geom_point(aes(group = treatment, colour = treatment, shape = treatment),
  121. size = 2) +
  122. geom_line(aes(group = treatment, colour = treatment, linetype = treatment),
  123. size = .9) +
  124. scale_shape_manual(values = c("Recipient" = 16, "Control" = 16, "Indicates the difference is statistically significant" = 4)) +
  125. scale_color_manual(values = c("Recipient" = b.navHexRGB[["gray"]], "Control" = b.navHexRGB[["green"]], "Indicates the difference is statistically significant" = b.navHexRGB[["red"]]),
  126. breaks = c("Control", "Recipient", "Indicates the difference is statistically significant")) +
  127. scale_linetype_manual(values = c("Recipient" = 1,"Control" = 2, "Indicates the difference is statistically significant" = 0),
  128. breaks = c("Control","Recipient","Indicates the difference is statistically significant")) +
  129. ylab("Average Daily Consumption (kWh)") +
  130. xlab("Year-Month") +
  131. ggtitle(paste("Group Starting", rct_start)) +
  132. theme_bw() +
  133. theme(legend.title = element_blank(),
  134. legend.justification = c(0,0),
  135. legend.position = "bottom",
  136. legend.key = element_rect(fill = "white",colour = "white"),
  137. #legend.key.width = unit(1.1, "cm"),
  138. axis.text.x = element_text(angle=45, hjust=1, color="black"),
  139. axis.text.y = element_text(color="black"),
  140. axis.title.y = element_text(vjust=1)
  141. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement