library(ggplot2)
data("morley")
head(morley)
##     Expt Run Speed
## 001    1   1   850
## 002    1   2   740
## 003    1   3   900
## 004    1   4  1070
## 005    1   5   930
## 006    1   6   850
nrow(morley)
## [1] 100
dim(morley)
## [1] 100   3

#barchart

ggplot(data = morley, mapping = aes(x = Speed))+
  geom_bar()

bar chart dengan tabel frekuensi

membuat tabel frekuensi

(freqtab <- as.data.frame(table(morley$Speed)))
##    Var1 Freq
## 1   620    1
## 2   650    1
## 3   720    3
## 4   740    3
## 5   750    1
## 6   760    5
## 7   770    1
## 8   780    2
## 9   790    3
## 10  800    5
## 11  810   10
## 12  820    2
## 13  830    2
## 14  840    8
## 15  850    8
## 16  860    3
## 17  870    4
## 18  880   10
## 19  890    3
## 20  900    2
## 21  910    2
## 22  920    1
## 23  930    2
## 24  940    3
## 25  950    3
## 26  960    4
## 27  970    1
## 28  980    3
## 29 1000    3
## 30 1070    1

membuat bar chart

ggplot(data = freqtab, mapping = aes(x = Var1, y = Freq))+
  geom_bar(stat = "identity")

ggplot(data = freqtab, mapping = aes(x = Var1, y = Freq))+
  geom_col()

modifikasi bar chart

ggplot(data = freqtab, mapping = aes(x = Var1, y = Freq))+
  geom_col(fill = "green", alpha = 0.7) +
  labs(title = "variansi hasil pengukuran oleh Albert.A ",
       x = "kecepatan km/s",
       y = "Frekuensi")+
  geom_text(aes(label = Freq), vjust = -0.25)

2. Needle chart

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 = "magenta")+
             geom_point(color = "black", size =4 , alpha = 0.6)+
  coord_flip() +
  labs(y = "Frekuensi", x = "kecepatan km/s")+
   geom_text(aes(label = Freq), vjust = -1)

3. Grouped bar chart

ggplot(data=morley,
       mapping=aes(x=Speed, fill=as.factor(Expt)))+
  geom_bar(position="dodge", stat="count")+
  labs(x="kecepatan km/s", fill="Run",
       y="Frekuensi")+
  scale_fill_brewer(palette = "Blues")+
  theme_light()

4. Stacked bar chart

ggplot(data=morley,
       mapping=aes(x=Speed, fill=as.factor(Expt)))+
  geom_bar(position="stack", stat="count")+
  labs(x="kecepatan km/s", fill="Run",
       y="Frekuensi")+
  scale_fill_brewer(palette = "R3")+
  theme_light()
## Warning: Unknown palette: "R3"

5. pie chart

menyiapkan data dengan meringkas menjadi tabel frekuensi

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 <- morley%>%
  group_by(Speed) %>%
  summarise(counts = n())
df
## # A tibble: 30 × 2
##    Speed counts
##    <int>  <int>
##  1   620      1
##  2   650      1
##  3   720      3
##  4   740      3
##  5   750      1
##  6   760      5
##  7   770      1
##  8   780      2
##  9   790      3
## 10   800      5
## # ℹ 20 more rows

menghitung posisi label teks sebagai jumlah kumulatif proporsi

library(dplyr)
df <- df%>%
  arrange (desc(Speed)) %>%
  mutate(prop = round(counts*100/sum(counts), 1),
         lab.ypos = cumsum(prop)- 0.5*prop)
head(df,4)
## # A tibble: 4 × 4
##   Speed counts  prop lab.ypos
##   <int>  <int> <dbl>    <dbl>
## 1  1070      1     1      0.5
## 2  1000      3     3      2.5
## 3   980      3     3      5.5
## 4   970      1     1      7.5

membuat grafik pie chart

library(ggpubr)
ggplot(df, aes (x = "", y = prop, fill = factor(Speed)))+
  geom_bar(width = 1, stat = "identity", color = "white")+
  geom_text(aes(y = lab.ypos,label = prop), color = "black")+
  coord_polar("y", start = 0)+
  ggpubr::fill_palette("jco","Blues","YIGn")+
  theme_void()
## Warning: This manual palette can handle a maximum of 10 values. You have
## supplied 30

library(sf)
## Linking to GEOS 3.13.0, GDAL 3.10.1, PROJ 9.5.1; sf_use_s2() is TRUE
library(readxl)
library(ggplot2)
library(dplyr)

data_spasial<- read_excel("C:/Users/Acer/Downloads/Export_Output_2.dbf.xlsx")
shp.yogyakarta<-read_sf("C:/Users/Acer/Downloads/PETA SHP 34 Prov/PETA SHP 34 Prov/34-Yogyakarta/Export_Output_2.shp")
head(shp.yogyakarta)
## Simple feature collection with 5 features and 7 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 110.009 ymin: -8.204723 xmax: 110.8379 ymax: -7.543099
## Geodetic CRS:  WGS 84
## # A tibble: 5 × 8
##   NAME_1      ID_2 NAME_2       TYPE_2    ENGTYPE_2    Longitude Latitude
##   <chr>      <dbl> <chr>        <chr>     <chr>            <dbl>    <dbl>
## 1 Yogyakarta   440 Bantul       Kabupaten Regency           110.    -7.90
## 2 Yogyakarta   441 Gunung Kidul Kabupaten Regency           111.    -7.99
## 3 Yogyakarta   442 Kulon Progo  Kabupaten Regency           110.    -7.82
## 4 Yogyakarta   443 Sleman       Kabupaten Regency           110.    -7.72
## 5 Yogyakarta   444 Yogyakarta   Kotamadya Municipality      110.    -7.80
## # ℹ 1 more variable: geometry <MULTIPOLYGON [°]>
head(data_spasial)
## # A tibble: 5 × 7
##   NAME_1      ID_2 NAME_2       TYPE_2    ENGTYPE_2    Longitude Latitude
##   <chr>      <dbl> <chr>        <chr>     <chr>            <dbl>    <dbl>
## 1 Yogyakarta   440 Bantul       Kabupaten Regency           110.    -7.90
## 2 Yogyakarta   441 Gunung Kidul Kabupaten Regency           111.    -7.99
## 3 Yogyakarta   442 Kulon Progo  Kabupaten Regency           110.    -7.82
## 4 Yogyakarta   443 Sleman       Kabupaten Regency           110.    -7.72
## 5 Yogyakarta   444 Yogyakarta   Kotamadya Municipality      110.    -7.80
gabung.yogyakarta <- left_join(shp.yogyakarta,data_spasial,by = "NAME_1")
## Warning in sf_column %in% names(g): Detected an unexpected many-to-many relationship between `x` and `y`.
## ℹ Row 1 of `x` matches multiple rows in `y`.
## ℹ Row 1 of `y` matches multiple rows in `x`.
## ℹ If a many-to-many relationship is expected, set `relationship =
##   "many-to-many"` to silence this warning.
# Pemetaan data spasial
plot.yogyakarta <- ggplot(data = shp.yogyakarta)+
  geom_sf(aes (fill = Latitude))+
  scale_fill_distiller("pemetaan", palette = "Blues")
plot.yogyakarta