library(ggplot2)
summary(airquality$Ozone)
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## 1.00 18.00 31.50 42.13 63.25 168.00 37
ggplot(airquality, aes(x = Wind, y = Temp)) +
geom_point() +
labs(title = "Scatter Plot Wind vs Temp",
x = "Wind",
y = "Temperature") +
theme_minimal()
2. Buat bar chart untuk variabel cyl dari dataset mtcars dan tambahkan
label jumlah setiap kategori pada grafik
ggplot(mtcars, aes(x = factor(cyl))) +
geom_bar(fill = "skyblue") +
geom_text(stat = "count", aes(label = ..count..), vjust = -0.5) +
labs(title = "Bar Chart Jumlah Mobil Berdasarkan Jumlah Silinder",
x = "Jumlah Silinder (cyl)",
y = "Jumlah Mobil") +
theme_minimal()
## 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.
3.A Membuat boxplot untuk membandingkan petal.width berdasarkan variabel
species
ggplot(iris, aes(x = Species, y = Petal.Width, fill = Species)) +
geom_boxplot() +
labs(title = "Boxplot Petal.Width Berdasarkan Spesies",
x = "Species",
y = "Petal Width") +
theme_minimal()
3.B Hitung korelasi antara Sepal.Lenght dan Sepal.width
cor(iris$Sepal.Length,iris$Petal.Length)
## [1] 0.8717538
3.C Buat scatter plot antara Sepal.Length dan Sepal.Width dengan warna berbeda berdasarkan spesies dengan warna berbeda
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point() +
labs(title = "Scatter Plot Sepal.Length vs Sepal.Width",
x = "Sepal Length",
y = "Sepal Width") +
theme_minimal()
4. Lakukan uji chi square untuk menguji hubungan antara dua variabel vs
dan am dalam mtcars
chisq.test(mtcars$vs, mtcars$am)
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: mtcars$vs and mtcars$am
## X-squared = 0.34754, df = 1, p-value = 0.5555
5.A. Tanpilan ringgakas model
model_regresi <- lm(vs ~ am, data = mtcars)
summary(model_regresi)
##
## Call:
## lm(formula = vs ~ am, data = mtcars)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.5385 -0.3684 -0.3684 0.4615 0.6316
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.3684 0.1159 3.180 0.00341 **
## am 0.1700 0.1818 0.935 0.35704
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.505 on 30 degrees of freedom
## Multiple R-squared: 0.02834, Adjusted R-squared: -0.004049
## F-statistic: 0.875 on 1 and 30 DF, p-value: 0.357
5.B. Membuat scatter plot untuk garis regresi
ggplot(mtcars, aes(x = am, y = vs)) +
geom_point(color = "blue", size = 3) +
geom_smooth(method = "lm", se = FALSE, color = "red", size = 1.5) +
labs(title = "Plot Regresi Linier: vs ~ am",
x = "Transmisi (am: 0 = Automatic, 1 = Manual)",
y = "Engine Shape (vs: 0 = V-Engine, 1 = Straight Engine)") +
theme_minimal() +
theme(legend.position = "none")
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## `geom_smooth()` using formula = 'y ~ x'
5.C Interpretasi hasil, termasuk koefisien regresi dan nilai R square
Koefisien dari regresi variabel vs dan am dalam dataset mtcars adalah
standar deviasi : 0,17 t value = 0,935 p value = 0,357 R square =
0,028