sebelum menampilkan data, yang harus kita lakukan yakni mangaktifkan library untuk package yang sudah di install. berikut datanya :
library(ggplot2)
library(ggpubr)
setelah mengaktifkan library, inputkan data yan ingin di olah
data("InsectSprays")
head(InsectSprays)
## count spray
## 1 10 A
## 2 7 A
## 3 20 A
## 4 14 A
## 5 14 A
## 6 12 A
NROW(InsectSprays) #NROW atau bisa juga dim
## [1] 72
dim(InsectSprays)
## [1] 72 2
berikut syntax dibawah ini untuk menampilkan atau membuat chart bar. dengan menggunakan function ggplot dan data yang ingin diinputkan.
ggplot(data=InsectSprays, mapping=aes(x=count), fill = "skyblue")+
geom_bar()
berikut syntax untuk membuat tabel frekuensi dengan memasukkan data yang ingin tampilkan dalam bentuk data frame
freqtab <- as.data.frame(table(InsectSprays$count))
freqtab
## Var1 Freq
## 1 0 2
## 2 1 6
## 3 2 4
## 4 3 8
## 5 4 4
## 6 5 7
## 7 6 3
## 8 7 3
## 9 9 1
## 10 10 3
## 11 11 3
## 12 12 2
## 13 13 4
## 14 14 4
## 15 15 2
## 16 16 2
## 17 17 4
## 18 19 1
## 19 20 2
## 20 21 2
## 21 22 1
## 22 23 1
## 23 24 1
## 24 26 2
sama seperti sebelumnya syntax berikut untuk memunculkan data dalam bentuk bar chart atau histogram.
ggplot(data=freqtab, mapping=aes(x=Var1, y=Freq))+
geom_bar(stat="identity")
Atau bisa juga menggunakan syntax sebagai berikut
ggplot(data=freqtab, mapping=aes(x=Var1, y=Freq))+
geom_col()
berikut adalah untuk memodifikasi bar chart seperti menambahkan judul, menamakan nama untuk variabel x dan variabel y. sebagai berikut
ggplot(data=freqtab, mapping=aes(x=Var1, y=Freq))+
geom_col(fill="blue", alpha=0.9)+
labs(title = "Frekuensi data penyemprot serangga",
x="count",
y="sprays")+
geom_text(aes(label=Freq), vjust=-0.25)
berikut dibawah ini adalah syntax untuk membuat needle chart, sebagai berikut
ggplot(data=freqtab,
mapping=aes(x=reorder(Var1,Freq), y=Freq))+
geom_segment(aes(x=reorder(Var1,Freq),
xend=reorder(Var1,Freq),
y=0, yend=Freq), color="black")+
geom_point(color="blue", size=3, alpha=0.5)+
coord_flip()+
labs(y="sprays", x="count")+
geom_text(aes(label=Freq), vjust=0.1)
ggplot(data=InsectSprays,
mapping=aes(x=count, fill= as.factor(count)))+
geom_bar(position="dodge", stat="count")+
labs(x="count", fill="data",
y="sprays")+
scale_fill_brewer(palette = "Blues")+
theme_light()
## Warning in RColorBrewer::brewer.pal(n, pal): n too large, allowed maximum for palette Blues is 9
## Returning the palette you asked for with that many colors
ggplot(data=InsectSprays,
mapping=aes(x=count, fill= as.factor(count)))+
geom_bar(position="stack", stat="count")+
labs(x="count", fill="data",
y="sprays")+
scale_fill_brewer(palette = "R3")+
theme_light()
## Warning: Unknown palette: "R3"
## Warning in RColorBrewer::brewer.pal(n, pal): n too large, allowed maximum for palette Greens is 9
## Returning the palette you asked for with that many colors
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
df <- InsectSprays %>%
group_by(spray) %>%
summarise(counts=n())
df
## # A tibble: 6 × 2
## spray counts
## <fct> <int>
## 1 A 12
## 2 B 12
## 3 C 12
## 4 D 12
## 5 E 12
## 6 F 12
library(dplyr)
df <- df %>%
arrange(desc(spray)) %>%
mutate(prop=round(counts*100/sum(counts), 1),
lab.ypos = cumsum(prop)-0.5*prop)
head(df,4)
## # A tibble: 4 × 4
## spray counts prop lab.ypos
## <fct> <int> <dbl> <dbl>
## 1 F 12 16.7 8.35
## 2 E 12 16.7 25.0
## 3 D 12 16.7 41.7
## 4 C 12 16.7 58.4
#membuat grafik pie chart
ggplot(df, aes(x= "",y = prop, fill=spray))+
geom_bar(width=1,stat="identity", color="pink")+
geom_text(aes(y=lab.ypos, label = prop), color="black")+
coord_polar("y", start=0)+
ggpubr::fill_palette("jco")+
theme_void()
library(sf)
## Linking to GEOS 3.13.0, GDAL 3.8.5, PROJ 9.5.1; sf_use_s2() is TRUE
library(readxl)
data.spasial <- read_xlsx("/Users/M.Fabian.R.D/Desktop/SEMESTER 4/visualisasi data/data_riau.xlsx", sheet = 1)
shp.riau<-read_sf("/Users/M.Fabian.R.D/Downloads/PETA SHP 34 Prov/25-Riau/Export_Output.dbf")
head(shp.riau)
## Simple feature collection with 6 features and 7 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: 100.4992 ymin: -1.121101 xmax: 103.8139 ymax: 2.154721
## Geodetic CRS: WGS 84
## # A tibble: 6 × 8
## NAME_1 ID_2 NAME_2 TYPE_2 ENGTYPE_2 Longitude Latitude
## <chr> <dbl> <chr> <chr> <chr> <dbl> <dbl>
## 1 Riau 313 Bengkalis Kabupaten Regency 102. 1.34
## 2 Riau 314 Dumai Kotamadya Municipality 101. 1.63
## 3 Riau 315 Indragiri Hilir Kabupaten Regency 103. -0.265
## 4 Riau 316 Indragiri Hulu Kabupaten Regency 102. -0.552
## 5 Riau 317 Kampar Kabupaten Regency 101. 0.297
## 6 Riau 318 Kuantan Singingi Kabupaten Regency 102. -0.492
## # ℹ 1 more variable: geometry <MULTIPOLYGON [°]>
head(data.spasial)
## # A tibble: 6 × 7
## NAME_1 ID_2 NAME_2 TYPE_2 ENGTYPE_2 Longitude Latitude
## <chr> <dbl> <chr> <chr> <chr> <dbl> <dbl>
## 1 Kepulauan Riau 236 Batam Kotamadya Municipality 104. 0.961
## 2 Kepulauan Riau 237 Karimun Kabupaten Regency 104. 0.826
## 3 Kepulauan Riau 238 Kepulauan Riau Kabupaten Regency 105. 0.991
## 4 Kepulauan Riau 239 Lingga Kotamadya Municipality 105. -0.216
## 5 Kepulauan Riau 240 Natuna Kabupaten Regency 108. 3.59
## 6 Kepulauan Riau 241 Tanjung Pinang Kotamadya Municipality 104. 1.05
library(dplyr)
gabung.riau = left_join(shp.riau,data.spasial, by = "NAME_1")
plot.riau <- ggplot(data = shp.riau)+
geom_sf(aes(fill = Latitude))+
scale_fill_distiller("pemetaan", palette = "Blues")
plot.riau