Install library
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.3
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.3.3
## Warning: package 'tibble' was built under R version 4.3.3
## Warning: package 'tidyr' was built under R version 4.3.3
## Warning: package 'readr' was built under R version 4.3.3
## Warning: package 'dplyr' was built under R version 4.3.3
## Warning: package 'forcats' was built under R version 4.3.3
## Warning: package 'lubridate' was built under R version 4.3.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ lubridate 1.9.3 ✔ tibble 3.2.1
## ✔ purrr 1.0.2 ✔ tidyr 1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)
read data
Automobile <- read.csv("C:/Users/User/OneDrive - Institut Teknologi Sepuluh Nopember/Dokumen/MATKUL SMSTR 2/Visualisasi Data/R/workinf directory/Automobile.csv")
View(Automobile)
glimpse(Automobile)
## Rows: 398
## Columns: 9
## $ name <chr> "chevrolet chevelle malibu", "buick skylark 320", "plymou…
## $ mpg <dbl> 18, 15, 18, 16, 17, 15, 14, 14, 14, 15, 15, 14, 15, 14, 2…
## $ cylinders <int> 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 4, 6, 6, 6, 4, …
## $ displacement <dbl> 307, 350, 318, 304, 302, 429, 454, 440, 455, 390, 383, 34…
## $ horsepower <int> 130, 165, 150, 150, 140, 198, 220, 215, 225, 190, 170, 16…
## $ weight <int> 3504, 3693, 3436, 3433, 3449, 4341, 4354, 4312, 4425, 385…
## $ acceleration <dbl> 12.0, 11.5, 11.0, 12.0, 10.5, 10.0, 9.0, 8.5, 10.0, 8.5, …
## $ model_year <int> 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 7…
## $ origin <chr> "usa", "usa", "usa", "usa", "usa", "usa", "usa", "usa", "…
summary(Automobile)
## name mpg cylinders displacement
## Length:398 Min. : 9.00 Min. :3.000 Min. : 68.0
## Class :character 1st Qu.:17.50 1st Qu.:4.000 1st Qu.:104.2
## Mode :character Median :23.00 Median :4.000 Median :148.5
## Mean :23.51 Mean :5.455 Mean :193.4
## 3rd Qu.:29.00 3rd Qu.:8.000 3rd Qu.:262.0
## Max. :46.60 Max. :8.000 Max. :455.0
##
## horsepower weight acceleration model_year
## Min. : 46.0 Min. :1613 Min. : 8.00 Min. :70.00
## 1st Qu.: 75.0 1st Qu.:2224 1st Qu.:13.82 1st Qu.:73.00
## Median : 93.5 Median :2804 Median :15.50 Median :76.00
## Mean :104.5 Mean :2970 Mean :15.57 Mean :76.01
## 3rd Qu.:126.0 3rd Qu.:3608 3rd Qu.:17.18 3rd Qu.:79.00
## Max. :230.0 Max. :5140 Max. :24.80 Max. :82.00
## NA's :6
## origin
## Length:398
## Class :character
## Mode :character
##
##
##
##
names(Automobile)
## [1] "name" "mpg" "cylinders" "displacement" "horsepower"
## [6] "weight" "acceleration" "model_year" "origin"
Automobile_subset1 <- Automobile %>%
select(displacement, mpg, cylinders)
ggplot(Automobile_subset1,aes(x=displacement,y=mpg ,col = cylinders))+geom_jitter()+geom_smooth()
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: The following aesthetics were dropped during statistical transformation:
## colour.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
## the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
## variable into a factor?
efisiensi energi berbanding terbalik dengan ukuran mesin dan volume
perpindahan piston, semakin besar ukuran mesin dan perpindahan piston
maka semakin banyak energi yang digunakan
Automobile_model <- Automobile %>%
group_by(origin, name) %>%
summarise(count = n()) %>%
arrange(origin, desc(count))
## `summarise()` has grouped output by 'origin'. You can override using the
## `.groups` argument.
Automobile_model
## # A tibble: 305 × 3
## # Groups: origin [3]
## origin name count
## <chr> <chr> <int>
## 1 europe peugeot 504 4
## 2 europe volkswagen dasher 3
## 3 europe audi 100ls 2
## 4 europe fiat 128 2
## 5 europe opel 1900 2
## 6 europe opel manta 2
## 7 europe saab 99le 2
## 8 europe volkswagen rabbit 2
## 9 europe vw rabbit 2
## 10 europe audi 100 ls 1
## # ℹ 295 more rows
ggplot(Automobile_model, aes(y = reorder(origin, origin, function(y)length(y)))) +
geom_bar(fill = "cadetblue4") +
geom_text(aes(label = ..count..), stat = 'count', hjust = 1.5, colour = 'white') + theme_minimal() +
labs(x = remove(),y = remove() )
## Warning: The dot-dot notation (`..count..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(count)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
Mengurutkan jumlah produksi mobil dari tiga negara
Automobile_subset2 <- Automobile %>%
filter(horsepower>100) %>%
arrange(desc(horsepower)) %>% head(10) %>%
select(name, horsepower)
Automobile_subset2
## name horsepower
## 1 pontiac grand prix 230
## 2 pontiac catalina 225
## 3 buick estate wagon (sw) 225
## 4 buick electra 225 custom 225
## 5 chevrolet impala 220
## 6 plymouth fury iii 215
## 7 ford f250 215
## 8 chrysler new yorker brougham 215
## 9 dodge d200 210
## 10 mercury marquis 208
ggplot(Automobile_subset2, aes(x=horsepower,y = reorder(name,horsepower))) +
geom_bar(stat="identity",fill = "cadetblue4") +
geom_text(aes(label = horsepower), hjust = -0.1, size = 3)+ theme_minimal() +
labs(title = "Horsepower menurut Merek (>100 HP)") + labs(x = remove(), y = remove() )
mengurutkan 10 merek dengan horsepower tercepat ()
tapply(Automobile$weight, Automobile$origin, FUN = function(x) summary(x))
## $europe
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1825 2067 2240 2423 2770 3820
##
## $japan
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1613 1985 2155 2221 2412 2930
##
## $usa
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1800 2720 3365 3362 4054 5140
Automobile_acc <- Automobile %>%
select(origin, weight)
ggplot(Automobile_acc, aes(x = origin, y = weight, fill= origin)) + geom_boxplot() + labs(title = "Berat mobil dari tiga negara") + guides(fill = F) + theme_minimal() + labs(x = remove(),y = remove() )
## Warning: The `<scale>` argument of `guides()` cannot be `FALSE`. Use "none" instead as
## of ggplot2 3.3.4.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
Membandingkan berat mobil dari ketiga negara, diketahui mobil produksi USA lebih berat daripada mobil produksi Eurpe dan Japan