|
| 1 | +#*********** GGPLOT2 GRAMMAR OF GRAPHICS ***********# |
| 2 | +# # |
| 3 | +# COMPONENTS OF GRAMMAR OF GRAPHICS # |
| 4 | +# 1. DATA : the dataset # |
| 5 | +# 2. AESTHETICS : the metric onto which we plot data # |
| 6 | +# 3. GEOMETRY : visual elements to plot the data # |
| 7 | +# 4. FACET : groups by which we divide the data # |
| 8 | +#***********************************************************# |
| 9 | + |
| 10 | +library(ggplot2) |
| 11 | + |
| 12 | +#*** SCATTERPLOTS ***# |
| 13 | +ggplot(data=iris, aes(y=Petal.Length, |
| 14 | + x=Sepal.Length))+geom_point() |
| 15 | +ggplot(data=iris, aes(y=Petal.Length, |
| 16 | + x=Sepal.Length,col=Species))+geom_point() |
| 17 | +ggplot(data=iris, aes(y=Petal.Length, x=Sepal.Length, |
| 18 | + shape=Species))+geom_point() |
| 19 | +ggplot(data=iris, aes(y=Petal.Length, x=Sepal.Length, col=Species, |
| 20 | + shape=Species))+geom_point() |
| 21 | + |
| 22 | +str(house) |
| 23 | + |
| 24 | +house<-read.csv(file.choose(),header = TRUE) |
| 25 | +library(dplyr) |
| 26 | +house1<- house[,-1] |
| 27 | +house1 <- house %>% select(c(-1)) |
| 28 | +View (house) |
| 29 | +#histogram |
| 30 | +ggplot(data = house, aes(x=price))+ geom_histogram() |
| 31 | +#ggplot(data = house, aes(x=price))+ geom_histogram(bin=50) |
| 32 | +ggplot(data=house,aes(x=price))+ |
| 33 | +geom_histogram(bins=50,fill="brown") |
| 34 | +ggplot(data=house, aes(x=price))+ |
| 35 | +geom_histogram(bins=50,fill="brown",col="black") |
| 36 | +ggplot(data=house, aes(x=price, fill=air_cond))+geom_histogram |
| 37 | +(bins=50) |
| 38 | +ggplot(data=house, aes(x=price, fill= factor(air_cond) |
| 39 | +))+ |
| 40 | + geom_histogram(bins=50,position="fill") |
| 41 | + |
| 42 | +#***** BARPLOT *******# |
| 43 | +#to see distribution of continous variable we use histogram |
| 44 | +#to see distribution of categorical variable we use barplot |
| 45 | +ggplot(data=house, aes(x=waterfront))+geom_bar() |
| 46 | +ggplot(data=house, aes(x=waterfront, |
| 47 | + fill=air_cond))+geom_bar() |
| 48 | +ggplot(data=house, aes(x=waterfront, |
| 49 | + fill=air_cond))+geom_bar(position = "fill") |
| 50 | +ggplot(data=house, aes(x=waterfront, |
| 51 | + fill=sewer))+geom_bar(position="fill") |
| 52 | + |
| 53 | +#**** FREQUENCY-POLYGON ******# |
| 54 | +# an alternative to a histogram used to see a distribution of continous #variable |
| 55 | +ggplot(data=house, aes(x=price))+geom_freqpoly() |
| 56 | +#increase variation |
| 57 | +ggplot(data=house, aes(x=price))+geom_freqpoly(bins=50) |
| 58 | +ggplot(data=house, aes(x=price))+geom_freqpoly(bins=100) |
| 59 | +ggplot(data=house, aes(x=price, |
| 60 | + col=air_cond))+geom_freqpoly(bins=60) |
| 61 | + |
| 62 | +# Modify formatting of axis |
| 63 | +pl + scale_x_continuous(labels = comma) |
| 64 | + |
| 65 | +pl + # Remove axis labels & ticks |
| 66 | + theme(axis.text.x = element_blank(), |
| 67 | + axis.ticks.x = element_blank(), |
| 68 | + axis.text.y = element_blank(), |
| 69 | + axis.ticks.y = element_blank()) |
| 70 | + |
| 71 | + #**** BOXPLOTS *****# |
| 72 | +# how does continous var change w.r.t. ategorical var |
| 73 | +#outliers are beyond the avg value |
| 74 | +ggplot(data=house, aes(x=factor(rooms), |
| 75 | + y=price))+geom_boxplot() |
| 76 | +ggplot(data=house, aes(x=factor(rooms), |
| 77 | + y=price, fill=factor(rooms)))+geom_boxplot() |
| 78 | +ggplot(data=house, aes(x=factor(rooms), |
| 79 | + y=price, fill=air_cond))+geom_boxplot() |
| 80 | +ggplot(data=house, aes(x=factor(rooms), |
| 81 | + y=price, fill=sewer))+geom_boxplot() |
| 82 | + |
| 83 | + |
| 84 | +#***** Smooth-Line ******# |
| 85 | +#how does one continous variable change w.r.t to other continous var |
| 86 | +ggplot(data=house, |
| 87 | + aes(y=price, x=living_area))+geom_smooth() |
| 88 | + |
| 89 | +ggplot(data=house, aes(y=price, x=living_area), |
| 90 | + col=air_cond))+geom_smooth(se=F) |
| 91 | +ggplot(data=house, aes(y=price, x=living_area), |
| 92 | + col=heat))+geom_smooth(se=F) |
| 93 | + |
| 94 | +#**** Applying "lm" (linear model) method ****# |
| 95 | +ggplot(data=house, aes(y=price, x=living_area))+geom_point()+ |
| 96 | + geom_smooth(method="lm",se=F) |
| 97 | +ggplot(data=house, aes(y=price, x=living_area, |
| 98 | + col=air_cond))+geom_point()+ |
| 99 | + geom_smooth(method="lm",se=F) |
| 100 | +ggplot(data=house, aes(y=price, x=living_area, |
| 101 | + col=heat))+geom_point()+ |
| 102 | + geom_smooth(method="lm",se=F) |
| 103 | + |
| 104 | +##facets |
| 105 | +ggplot(data=house, aes(y=price, x=living_area, |
| 106 | + col=air_cond))+geom_point()+ |
| 107 | + geom_smooth(method="lm",se=F)+facet_grid(~air_cond) |
| 108 | +ggplot(data=house, aes(y=price, x=living_area, |
| 109 | + col=fireplaces))+geom_point()+ |
| 110 | + geom_smooth(method="lm",se=F)+facet_grid(~fireplaces) |
| 111 | +ggplot(data=house, aes(y=price, x=age, |
| 112 | + col=fireplaces))+geom_point()+ |
| 113 | + geom_smooth(method="lm",se=F)+facet_grid(~fireplaces) |
| 114 | + |
| 115 | + |
0 commit comments