Chapter 4 Methods
We describe our methods in this chapter.
library(ggplot2)
ggplot(data = mpg, aes(x = hwy, fill = drv, binwidth = .5)) + geom_histogram(alpha = .5) + labs(subtitle="Histogram of Highway Mile Per Gallon", title="Histogram", caption = "Source: mpg") + theme_minimal()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(data = mpg, aes(x = hwy, fill = drv)) + geom_histogram(alpha = .5) + labs(subtitle="Histogram of Highway Mile Per Gallon", title="Histogram using facet_grid()", caption = "Source: mpg") + theme_minimal() + facet_grid(drv ~ .)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(midwest, aes(x=area, y=poptotal)) + geom_point(alpha = .4, aes(col=state, size=popdensity)) + geom_smooth(method="loess", se=F) + xlim(c(0, 0.1)) + ylim(c(0, 500000)) + labs(subtitle="Area Vs Population", y="Population", x="Area", title="Scatterplot", caption = "Source: midwest") + theme_classic()
## `geom_smooth()` using formula 'y ~ x'
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Species, shape = Species)) + geom_point(alpha = 0.5, size=6) + theme_minimal() + labs(subtitle="Sepal.Length Vs Sepal.Width", y="Sepal.Width", x="Sepal.Length", title="Scatterplot", caption = "Source: iris") + theme_minimal()
## sex ageYear ageMonth heightIn weightLb
## 1 f 11.92 143 56.3 85.0
## 2 f 12.92 155 62.3 105.0
## 3 f 12.75 153 63.3 108.0
## 4 f 13.42 161 59.0 92.0
## 5 f 15.92 191 62.5 112.5
## 6 f 14.25 171 62.5 112.0
## 7 f 15.42 185 59.0 104.0
## 8 f 11.83 142 56.5 69.0
## 9 f 13.33 160 62.0 94.5
## 10 f 11.67 140 53.8 68.5
## 11 f 11.58 139 61.5 104.0
## 12 f 14.83 178 61.5 103.5
## 13 f 13.08 157 64.5 123.5
## 14 f 12.42 149 58.3 93.0
## 15 f 11.92 143 51.3 50.5
## 16 f 12.08 145 58.8 89.0
## 17 f 15.92 191 65.3 107.0
## 18 f 12.50 150 59.5 78.5
## 19 f 12.25 147 61.3 115.0
## 20 f 15.00 180 63.3 114.0
## 21 f 11.75 141 61.8 85.0
## 22 f 11.67 140 53.5 81.0
## 23 f 13.67 164 58.0 83.5
## 24 f 14.67 176 61.3 112.0
## 25 f 15.42 185 63.3 101.0
## 26 f 13.83 166 61.5 103.5
## 27 f 14.58 175 60.8 93.5
## 28 f 15.00 180 59.0 112.0
## 29 f 17.50 210 65.5 140.0
## 30 f 12.17 146 56.3 83.5
## 31 f 14.17 170 64.3 90.0
## 32 f 13.50 162 58.0 84.0
## 33 f 12.42 149 64.3 110.5
## 34 f 11.58 139 57.5 96.0
## 35 f 15.50 186 57.8 95.0
## 36 f 16.42 197 61.5 121.0
## 37 f 14.08 169 62.3 99.5
## 38 f 14.75 177 61.8 142.5
## 39 f 15.42 185 65.3 118.0
## 40 f 15.17 182 58.3 104.5
## 41 f 14.42 173 62.8 102.5
## 42 f 13.83 166 59.3 89.5
## 43 f 14.00 168 61.5 95.0
## 44 f 14.08 169 62.0 98.5
## 45 f 12.50 150 61.3 94.0
## 46 f 15.33 184 62.3 108.0
## 47 f 11.58 139 52.8 63.5
## 48 f 12.25 147 59.8 84.5
## 49 f 12.00 144 59.5 93.5
## 50 f 14.75 177 61.3 112.0
## 51 f 14.83 178 63.5 148.5
## 52 f 16.42 197 64.8 112.0
## 53 f 12.17 146 60.0 109.0
## 54 f 12.08 145 59.0 91.5
## 55 f 12.25 147 55.8 75.0
## 56 f 12.08 145 57.8 84.0
## 57 f 12.92 155 61.3 107.0
## 58 f 13.92 167 62.3 92.5
## 59 f 15.25 183 64.3 109.5
## 60 f 11.92 143 55.5 84.0
## 61 f 15.25 183 64.5 102.5
## 62 f 15.42 185 60.0 106.0
## 63 f 12.33 148 56.3 77.0
## 64 f 12.25 147 58.3 111.5
## 65 f 12.83 154 60.0 114.0
## 66 f 13.00 156 54.5 75.0
## 67 f 12.00 144 55.8 73.5
## 68 f 12.83 154 62.8 93.5
## 69 f 12.67 152 60.5 105.0
## 70 f 15.92 191 63.3 113.5
## 71 f 15.83 190 66.8 140.0
## 72 f 11.67 140 60.0 77.0
## 73 f 12.33 148 60.5 84.5
## 74 f 15.75 189 64.3 113.5
## 75 f 11.92 143 58.3 77.5
## 76 f 14.83 178 66.5 117.5
## 77 f 13.67 164 65.3 98.0
## 78 f 13.08 157 60.5 112.0
## 79 f 12.25 147 59.5 101.0
## 80 f 12.33 148 59.0 95.0
## 81 f 14.75 177 61.3 81.0
## 82 f 14.25 171 61.5 91.0
## 83 f 14.33 172 64.8 142.0
## 84 f 15.83 190 56.8 98.5
## 85 f 15.25 183 66.5 112.0
## 86 f 11.92 143 61.5 116.5
## 87 f 14.92 179 63.0 98.5
## 88 f 15.50 186 57.0 83.5
## 89 f 15.17 182 65.5 133.0
## 90 f 15.17 182 62.0 91.5
## 91 f 11.83 142 56.0 72.5
## 92 f 13.75 165 61.3 106.5
## 93 f 13.75 165 55.5 67.0
## 94 f 12.83 154 61.0 122.5
## 95 f 12.50 150 54.5 74.0
## 96 f 12.92 155 66.0 144.5
## 97 f 13.58 163 56.5 84.0
## 98 f 11.75 141 56.0 72.5
## 99 f 12.25 147 51.5 64.0
## 100 f 17.50 210 62.0 116.0
## 101 f 14.25 171 63.0 84.0
## 102 f 13.92 167 61.0 93.5
## 103 f 15.17 182 64.0 111.5
## 104 f 12.00 144 61.0 92.0
## 105 f 16.08 193 59.8 115.0
## 106 f 11.75 141 61.3 85.0
## 107 f 13.67 164 63.3 108.0
## 108 f 15.50 186 63.5 108.0
## 109 f 14.08 169 61.5 85.0
## 110 f 14.58 175 60.3 86.0
## 111 f 15.00 180 61.3 110.5
## 112 m 13.75 165 64.8 98.0
## 113 m 13.08 157 60.5 105.0
## 114 m 12.00 144 57.3 76.5
## 115 m 12.50 150 59.5 84.0
## 116 m 12.50 150 60.8 128.0
## 117 m 11.58 139 60.5 87.0
## 118 m 15.75 189 67.0 128.0
## 119 m 15.25 183 64.8 111.0
## 120 m 12.25 147 50.5 79.0
## 121 m 12.17 146 57.5 90.0
## 122 m 13.33 160 60.5 84.0
## 123 m 13.00 156 61.8 112.0
## 124 m 14.42 173 61.3 93.0
## 125 m 12.58 151 66.3 117.0
## 126 m 11.75 141 53.3 84.0
## 127 m 12.50 150 59.0 99.5
## 128 m 13.67 164 57.8 95.0
## 129 m 12.75 153 60.0 84.0
## 130 m 17.17 206 68.3 134.0
## 132 m 14.67 176 63.8 98.5
## 133 m 14.67 176 65.0 118.5
## 134 m 11.67 140 59.5 94.5
## 135 m 15.42 185 66.0 105.0
## 136 m 15.00 180 61.8 104.0
## 137 m 12.17 146 57.3 83.0
## 138 m 15.25 183 66.0 105.5
## 139 m 11.67 140 56.5 84.0
## 140 m 12.58 151 58.3 86.0
## 141 m 12.58 151 61.0 81.0
## 142 m 12.00 144 62.8 94.0
## 143 m 13.33 160 59.3 78.5
## 144 m 14.83 178 67.3 119.5
## 145 m 16.08 193 66.3 133.0
## 146 m 13.50 162 64.5 119.0
## 147 m 13.67 164 60.5 95.0
## 148 m 15.50 186 66.0 112.0
## 149 m 11.92 143 57.5 75.0
## 150 m 14.58 175 64.0 92.0
## 151 m 14.58 175 68.0 112.0
## 152 m 14.58 175 63.5 98.5
## 153 m 14.42 173 69.0 112.5
## 154 m 14.17 170 63.8 112.5
## 155 m 14.50 174 66.0 108.0
## 156 m 13.67 164 63.5 108.0
## 157 m 12.00 144 59.5 88.0
## 158 m 13.00 156 66.3 106.0
## 159 m 12.42 149 57.0 92.0
## 160 m 12.00 144 60.0 117.5
## 161 m 12.25 147 57.0 84.0
## 162 m 15.67 188 67.3 112.0
## 163 m 14.08 169 62.0 100.0
## 164 m 14.33 172 65.0 112.0
## 165 m 12.50 150 59.5 84.0
## 166 m 16.08 193 67.8 127.5
## 167 m 13.08 157 58.0 80.5
## 168 m 14.00 168 60.0 93.5
## 169 m 11.67 140 58.5 86.5
## 170 m 13.00 156 58.3 92.5
## 171 m 13.00 156 61.5 108.5
## 172 m 13.17 158 65.0 121.0
## 173 m 15.33 184 66.5 112.0
## 174 m 13.00 156 68.5 114.0
## 175 m 12.00 144 57.0 84.0
## 176 m 14.67 176 61.5 81.0
## 177 m 14.00 168 66.5 111.5
## 178 m 12.42 149 52.5 81.0
## 179 m 11.83 142 55.0 70.0
## 180 m 15.67 188 71.0 140.0
## 181 m 16.92 203 66.5 117.0
## 182 m 11.83 142 58.8 84.0
## 183 m 15.75 189 66.3 112.0
## 184 m 15.67 188 65.8 150.5
## 185 m 16.67 200 71.0 147.0
## 186 m 12.67 152 59.5 105.0
## 187 m 14.50 174 69.8 119.5
## 188 m 13.83 166 62.5 84.0
## 189 m 12.08 145 56.5 91.0
## 190 m 11.92 143 57.5 101.0
## 191 m 13.58 163 65.3 117.5
## 192 m 13.83 166 67.3 121.0
## 193 m 15.17 182 67.0 133.0
## 194 m 14.42 173 66.0 112.0
## 195 m 12.92 155 61.8 91.5
## 196 m 13.50 162 60.0 105.0
## 197 m 14.75 177 63.0 111.0
## 198 m 14.75 177 60.5 112.0
## 199 m 14.58 175 65.5 114.0
## 200 m 13.83 166 62.0 91.0
## 201 m 12.50 150 59.0 98.0
## 202 m 12.50 150 61.8 118.0
## 203 m 15.67 188 63.3 115.5
## 204 m 13.58 163 66.0 112.0
## 205 m 14.25 171 61.8 112.0
## 206 m 13.50 162 63.0 91.0
## 207 m 11.75 141 57.5 85.0
## 208 m 14.50 174 63.0 112.0
## 209 m 11.83 142 56.0 87.5
## 210 m 12.33 148 60.5 118.0
## 211 m 11.67 140 56.8 83.5
## 212 m 13.33 160 64.0 116.0
## 213 m 12.00 144 60.0 89.0
## 214 m 17.17 206 69.5 171.5
## 215 m 13.25 159 63.3 112.0
## 216 m 12.42 149 56.3 72.0
## 217 m 16.08 193 72.0 150.0
## 218 m 16.17 194 65.3 134.5
## 219 m 12.67 152 60.8 97.0
## 220 m 12.17 146 55.0 71.5
## 221 m 11.58 139 55.0 73.5
## 222 m 15.50 186 66.5 112.0
## 223 m 13.42 161 56.8 75.0
## 224 m 12.75 153 64.8 128.0
## 225 m 16.33 196 64.5 98.0
## 226 m 13.67 164 58.0 84.0
## 227 m 13.25 159 62.8 99.0
## 228 m 14.83 178 63.8 112.0
## 229 m 12.75 153 57.8 79.5
## 230 m 12.92 155 57.3 80.5
## 231 m 14.83 178 63.5 102.5
## 232 m 11.83 142 55.0 76.0
## 233 m 13.67 164 66.5 112.0
## 234 m 15.75 189 65.0 114.0
## 235 m 13.67 164 61.5 140.0
## 236 m 13.92 167 62.0 107.5
## 237 m 12.58 151 59.3 87.0
ggplot(heightweight, aes(x = heightIn, y = weightLb, colour = sex)) + geom_point(alpha = 0.5, size = 3) + geom_smooth(method="lm", se = FALSE) + theme_classic() + labs(subtitle="Weight Vs Height", y="weightLb", x="heightIn", title="Scatterplot", caption = "Source: heightweight")
## `geom_smooth()` using formula 'y ~ x'
library(RColorBrewer)
ggplot(mpg, aes(x = manufacturer, fill = class, width = 0.5)) + labs(title="Histogram on Categorical Variable", subtitle="Manufacturer across Vehicle Classes") + scale_fill_brewer(palette = "Spectral") + theme_minimal() + geom_bar() + theme(axis.text.x = element_text(angle = 65, vjust=0.6))