library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.0.5
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## Warning: package 'ggplot2' was built under R version 4.0.5
## Warning: package 'tibble' was built under R version 4.0.5
## Warning: package 'tidyr' was built under R version 4.0.5
## Warning: package 'readr' was built under R version 4.0.5
## Warning: package 'dplyr' was built under R version 4.0.5
## Warning: package 'forcats' was built under R version 4.0.4
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(ggridges)
library(GGally)
## Warning: package 'GGally' was built under R version 4.0.5
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
library(plotly)
## Warning: package 'plotly' was built under R version 4.0.5
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout

Secara umum pembuatan grafik pada package ggplot2 dapat dilakukan dengan menggunakan dua cara yaitu qplot(x,y,geom=“xxx”) atau ggplot()+geom_xxx(aes(x,y)).

set.seed(12)
qplot(x=rnorm(10),y=rnorm(10),geom = "point")

ggplot()+geom_point(aes(x=rnorm(10),y=rnorm(10)))

set.seed(12)
qplot(x=rnorm(10),y=rnorm(10),geom = "point")+theme_bw()

ggplot()+geom_point(aes(x=rnorm(10),y=rnorm(10)))+theme_dark()

qplot(x=rnorm(10),y=rnorm(10),geom = "point")+theme_classic()

ggplot()+geom_point(aes(x=rnorm(10),y=rnorm(10)))+theme_minimal()

qplot(x=rnorm(10),y=rnorm(10),geom = "point")+theme_light()

ggplot()+geom_point(aes(x=rnorm(10),y=rnorm(10)))+theme_linedraw()

ggplot()+geom_point(aes(x=rnorm(10),y=rnorm(10)))+theme_gray()

set.seed(12)
ggplot()+geom_line(aes(x=1:10,y=rnorm(10)))+
  #manambah plot titik
  geom_point(aes(x=1:10,y=rnorm(10)))+
  #mengubah nama sumbu x
  xlab("Waktu")+
  #menambahkan nama grafik
  ggtitle("Grafik Percobaan")+
  #mengubah tema
  theme_dark()

set.seed(12)
ggplot()+geom_point(aes(x=rnorm(2),y=rnorm(2),col=c('gen1','gen2')))

set.seed(12)
ggplot()+geom_point(aes(x=rnorm(20),y=rnorm(20)),col="green")

set.seed(12)
ggplot()+geom_point(aes(x=rnorm(20),y=rnorm(20),col="green"))

data_house <- read_csv("https://raw.githubusercontent.com/gerrydito/Sains-Data-S2/master/Praktikum/Visualisasi%20Data/house_price.csv",)
## Rows: 4600 Columns: 18
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr   (4): street, city, statezip, country
## dbl  (13): price, bedrooms, bathrooms, sqft_living, sqft_lot, floors, waterf...
## dttm  (1): date
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
head(data_house)
## # A tibble: 6 x 18
##   date                  price bedrooms bathrooms sqft_living sqft_lot floors
##   <dttm>                <dbl>    <dbl>     <dbl>       <dbl>    <dbl>  <dbl>
## 1 2014-05-02 00:00:00  313000        3      1.5         1340     7912    1.5
## 2 2014-05-02 00:00:00 2384000        5      2.5         3650     9050    2  
## 3 2014-05-02 00:00:00  342000        3      2           1930    11947    1  
## 4 2014-05-02 00:00:00  420000        3      2.25        2000     8030    1  
## 5 2014-05-02 00:00:00  550000        4      2.5         1940    10500    1  
## 6 2014-05-02 00:00:00  490000        2      1            880     6380    1  
## # ... with 11 more variables: waterfront <dbl>, view <dbl>, condition <dbl>,
## #   sqft_above <dbl>, sqft_basement <dbl>, yr_built <dbl>, yr_renovated <dbl>,
## #   street <chr>, city <chr>, statezip <chr>, country <chr>

Histogram

ggplot(data_house) +
  geom_histogram(aes(x = price),fill="darkred",col="darkred",binwidth = 5000) +
  ggtitle("Histogram Harga Rumah") +
  ylab("Jumlah Rumah") +
  xlab("Harga Rumah") + 
  theme(plot.title = element_text(hjust = 0.5))

Boxplot

data_house%>%
  filter(city %in% c("Shoreline","Seattle"
                     ,"Auburn","Duvall","Burien",
                     "Tukwila","Vashon","Yarrow Point",
                     "SeaTac","Medina")
  )%>%
  ggplot() +
  geom_boxplot(aes(x = city,y = price,fill=city),show.legend = F) +
  ggtitle("Sebaran Harga Rumah Setiap Kota") +
  ylab("Harga Rumah") +
  xlab("Kota") + 
  theme(plot.title = element_text(hjust = 0.5))+
  theme_bw()

Ridgeline

data_house%>%
  filter(city %in% c("Shoreline","Seattle"
                     ,"Auburn","Duvall","Burien",
                     "Tukwila","Vashon","Yarrow Point",
                     "SeaTac","Medina")
  )%>%
  ggplot() +
  geom_density_ridges(aes(y = city,x = price,fill=city), show.legend = F) +
  ggtitle("Sebaran Harga Rumah Setiap Kota") +
  ylab("Kota") +
  xlab("Harga Rumah") + 
  theme(plot.title = element_text(hjust = 0.5))+
  theme_bw()
## Picking joint bandwidth of 133000

## Barplot

data_house%>%count(city)%>%
  ggplot()+
  geom_col(aes(x=fct_reorder(as.factor(city),n),y=n), fill="steelblue", 
           width=0.4) +
  scale_y_continuous(expand = c(0,0))+
  coord_flip() +
  ggtitle("Jumlah Rumah Setiap Kota") +
  xlab("") +
  ylab("Jumlah Rumah") +
  theme(plot.title = element_text(hjust = 0.5))+
  theme_classic()

Lollipop Chart

data_house%>%count(city)%>%
  mutate(city=fct_reorder(as.factor(city),desc(n)))%>%
  ggplot()+
  geom_segment(aes(x=city,xend=city, y=0, yend=n), color="skyblue")+
  geom_point(aes(x=city,y=n),color="steelblue", size=2)+ 
    scale_y_continuous(expand = c(0,0))+
    coord_flip() +
    ggtitle("Jumlah Rumah Setiap Kota") +
    xlab("") +
    ylab("Jumlah Rumah") +
    theme(plot.title = element_text(hjust = 0.5))

Scatter Plot

ggplot(data_house) +
  geom_point(aes(x = sqft_living,y = price),color="steelblue",size=2) +
  ggtitle("Scatter Plot Harga Rumah vs Luas Rumah") +
  ylab("Luas Rumah") +
  xlab("Harga Rumah") + 
  theme(plot.title = element_text(hjust = 0.5))

Correlogram

ggcorr(data_house, method = c("everything","pearson"),geom = "tile") 
## Warning in ggcorr(data_house, method = c("everything", "pearson"), geom =
## "tile"): data in column(s) 'date', 'street', 'city', 'statezip', 'country' are
## not numeric and were ignored

Plot Interaktif

p1 <- ggplot(data_house) +
  geom_point(aes(x = sqft_living,y = price),color="steelblue",size=2) +
  ggtitle("Scatter Plot Harga Rumah vs Luas Rumah") +
  ylab("Luas Rumah") +
  xlab("Harga Rumah") + 
  theme(plot.title = element_text(hjust = 0.5))
ggplotly(p1)