library(ggplot2)
data(mpg)
ggplot(mpg, aes(x=hwy)) + geom_histogram(bins = 20, fill="red", alpha=0.5)
# Histogram with manufacturer using facet_grid.
data(mpg)
ggplot(mpg, aes(x=manufacturer)) + geom_bar(aes(fill=factor(cyl)))
## Use position=“dodge” to change the stacking of graph.
data("txhousing")
head(txhousing)
## # A tibble: 6 x 9
## city year month sales volume median listings inventory date
## <chr> <int> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Abilene 2000 1 72 5380000 71400 701 6.3 2000
## 2 Abilene 2000 2 98 6505000 58700 746 6.6 2000.
## 3 Abilene 2000 3 130 9285000 58100 784 6.8 2000.
## 4 Abilene 2000 4 98 9730000 68600 785 6.9 2000.
## 5 Abilene 2000 5 141 10590000 67300 794 6.8 2000.
## 6 Abilene 2000 6 156 13910000 66900 780 6.6 2000.
ggplot(txhousing, aes(x=sales, y=volume))+geom_point(aes(color="pink", alpha=0.4))
## Warning: Removed 568 rows containing missing values (geom_point).
ggplot(txhousing, aes(x=sales, y=volume))+geom_point(aes(color="pink", alpha=0.4))+geom_smooth()
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
## Warning: Removed 568 rows containing non-finite values (stat_smooth).
## Warning: Removed 568 rows containing missing values (geom_point).
ggplot(txhousing, aes(x=sales, y=volume))+geom_point(aes(color="pink", alpha=0.3)) + theme_dark()
## Warning: Removed 568 rows containing missing values (geom_point).
library(ggthemes)
ggplot(txhousing, aes(x=sales, y=volume))+geom_point(aes(color="pink", alpha=0.4))+geom_smooth()+theme_economist()
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
## Warning: Removed 568 rows containing non-finite values (stat_smooth).
## Warning: Removed 568 rows containing missing values (geom_point).
ggplot(txhousing, aes(x=sales, y=volume))+geom_point(aes(color="pink", alpha=0.4))+geom_smooth()+theme_wsj()
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
## Warning: Removed 568 rows containing non-finite values (stat_smooth).
## Warning: Removed 568 rows containing missing values (geom_point).
ggplot(mpg, aes(x=displ, y=hwy))+geom_point()
ggplot(mpg, aes(x=displ, y=hwy))+geom_point()+coord_cartesian(xlim = c(1,5), ylim = c(10,35))
## Now check the x-axis & y-axis, they are set to the limits.
ggplot(mpg, aes(x=displ, y=hwy))+geom_point()+facet_grid(.~cyl)
ggplot(mpg, aes(x=displ, y=hwy))+geom_point()+facet_grid(drv~cyl)
## This facet syntax works as (y~x), means teh variable before telda(~) will be shown on y-axis & the variable after telda(~) will be shown on x-axis.
data(mtcars)
ggplot(mtcars, aes(x=mpg, y=hp))+geom_point()+stat_smooth(level = 0.95)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## The shaded region showing the 95% confidence interval around the values.