library(tuev)
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.0.5
ggplot2 paketi, lattice paketi gibi verilerdeki birden çok değişkeni aynı grafik üzerinde göstermek ve veriler arasındaki çok düzeyli ilişkileri özetlemek amacıyla geliştirilmiştir.
Açılımı grafiğin grameri (grammer of graphics) şeklindedir.
lattice grafiklerindeki gibi grafikler nesneler olarak kaydedilmekte ve birden çok grafiği tek bir grafiğin üzerinde gösterebilmektedir.
lattice paketine göre en önemli farkı katman mantığıyla çalışmasıdır.
Metin ekleme,
renklendirme,
açıklama kutucukları vb… özelleştirmeler toplama (+) işareti ile kodlara eklenebilmektedir.
qplot() ve ggplot() fonksiyonları
qplot(), hızlı grafik (quick plot) çizimi anlamına gelmektedir.
qplot(x, y, data, geom) veya
ggplot(x, y, data, geom) veya
ggplot( data, aes(x, y)) +geom.grafikismi()
aes() her bir değişkenin alacağı rolü belirlemede kullanılır.
geom() argümanı çizilecek grafiği türünü belirlemek için kullanılmaktadır. Geometrik nesneler (geometric objects) olarak adlandırılmaktadır.
Örneğin yoğunluk grafiği çizilmek istendiğinde; - ggplot(x, y, data, geom=“density”) veya
- ggplot( data, aes(x, y)) +geom.density()
library(ggplot2)
ls(pattern = '^geom_', env = as.environment('package:ggplot2'))
## [1] "geom_abline" "geom_area" "geom_bar"
## [4] "geom_bin_2d" "geom_bin2d" "geom_blank"
## [7] "geom_boxplot" "geom_col" "geom_contour"
## [10] "geom_contour_filled" "geom_count" "geom_crossbar"
## [13] "geom_curve" "geom_density" "geom_density_2d"
## [16] "geom_density_2d_filled" "geom_density2d" "geom_density2d_filled"
## [19] "geom_dotplot" "geom_errorbar" "geom_errorbarh"
## [22] "geom_freqpoly" "geom_function" "geom_hex"
## [25] "geom_histogram" "geom_hline" "geom_jitter"
## [28] "geom_label" "geom_line" "geom_linerange"
## [31] "geom_map" "geom_path" "geom_point"
## [34] "geom_pointrange" "geom_polygon" "geom_qq"
## [37] "geom_qq_line" "geom_quantile" "geom_raster"
## [40] "geom_rect" "geom_ribbon" "geom_rug"
## [43] "geom_segment" "geom_sf" "geom_sf_label"
## [46] "geom_sf_text" "geom_smooth" "geom_spoke"
## [49] "geom_step" "geom_text" "geom_tile"
## [52] "geom_violin" "geom_vline"
aşağıdaki kod sadece ilk katmanı olusturur.
library(tuev)
ggplot(PISA_OGR_2018,aes(x=ODOKUMA1,y=OK_YETERLIK))
Katman eklenmesi
library(tuev)
ggplot(PISA_OGR_2018,aes(x=ODOKUMA1,y=OK_YETERLIK))+
geom_point()
## Warning: Removed 199 rows containing missing values (geom_point).
Yüzey eklenmesi
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.0.5
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v tibble 3.1.4 v dplyr 1.0.7
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 2.0.2 v forcats 0.5.1
## v purrr 0.3.4
## 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.5
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
ggplot(PISA_OGR_2018,aes(x=ODOKUMA1,y=OK_YETERLIK))+
geom_point()+
facet_wrap(~CINSIYET, nrow=2)
## Warning: Removed 199 rows containing missing values (geom_point).
Yüzey eklenmesi
ggplot(PISA_OGR_2018,aes(x=ODOKUMA1,y=OK_YETERLIK))+
geom_point()+
facet_wrap(~SINIF, nrow=2)
## Warning: Removed 199 rows containing missing values (geom_point).
Yüzeyde kategorik değişkenlerin düzeylerini görebilmek için factor değişken olarak tanımlamak gerekir.
library(haven)
## Warning: package 'haven' was built under R version 4.0.5
PISA_OGR_2018 <- PISA_OGR_2018 %>% mutate_if(is.labelled,
funs(as_factor(.)))
## Warning: `funs()` was deprecated in dplyr 0.8.0.
## Please use a list of either functions or lambdas:
##
## # Simple named list:
## list(mean = mean, median = median)
##
## # Auto named with `tibble::lst()`:
## tibble::lst(mean, median)
##
## # Using lambdas
## list(~ mean(., trim = .2), ~ median(., na.rm = TRUE))
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.
ggplot(PISA_OGR_2018,aes(x=ODOKUMA1,y=OK_YETERLIK))+
geom_point()+
facet_grid(SINIF~CINSIYET)
## Warning: Removed 199 rows containing missing values (geom_point).
Yüzeylerin sütunda olusturlması
ggplot(PISA_OGR_2018,aes(x=ODOKUMA1,y=OK_YETERLIK))+
geom_point()+
facet_grid(.~CINSIYET)
## Warning: Removed 199 rows containing missing values (geom_point).
Yüzeylerin satırlarda olusturlması
ggplot(PISA_OGR_2018,aes(x=ODOKUMA1,y=OK_YETERLIK))+
geom_point()+
facet_grid(CINSIYET~.)
## Warning: Removed 199 rows containing missing values (geom_point).
ggplot(PISA_OGR_2018,aes(x=ODOKUMA1,y=OK_YETERLIK))+
geom_point()+
facet_grid(.~SINIF)
## Warning: Removed 199 rows containing missing values (geom_point).
ggplot(PISA_OGR_2018,aes(x=ODOKUMA1,y=OK_YETERLIK))+
geom_point()+
facet_grid(SINIF~.)
## Warning: Removed 199 rows containing missing values (geom_point).
Renklendirme, sembol şekli, sembol büyüklüğü ve çizgi türü belirleyen fonksiyonlar yardımıyla gruplama yapılmaktadır.
Açıklama kutucukları otomatik çıkar!
ggplot(PISA_OGR_2018,aes(x=ODOKUMA1,y=OK_YETERLIK,color=SINIF)) + geom_point()
## Warning: Removed 199 rows containing missing values (geom_point).
ggplot(PISA_OGR_2018,aes(x=ODOKUMA1,y=OK_YETERLIK,color=CINSIYET)) + geom_point()+ facet_grid(.~SINIF)
## Warning: Removed 199 rows containing missing values (geom_point).
p <- ggplot(mtcars, aes(cyl, mpg)) +
geom_point()
p
ggplot(mtcars, aes(factor(cyl), mpg)) +
geom_point()
color parametresi
ggplot(PISA_OGR_2018, aes(CINSIYET, OK_YETERLIK)) +
geom_point(color = "blue")
## Warning: Removed 199 rows containing missing values (geom_point).
size ve shape parametresi
her iki parametrenin de olağan değeri 1 dir.
ggplot(PISA_OGR_2018, aes(CINSIYET, OK_YETERLIK)) +
geom_point(color = "blue",size=5,shape="a")
## Warning: Removed 199 rows containing missing values (geom_point).
Veri seti her bir okul türünden 5 kişi alınarak veri seti boyutu küçültülmüştür.
df <- PISA_OGR_2018 %>% group_by(OKUL_TUR)%>% sample_n(5)
ggplot(df, aes(CINSIYET, OK_YETERLIK, color = SINIF)) +
geom_point()
## Warning: Removed 6 rows containing missing values (geom_point).
Ustuse gelen noktalar için position
ggplot(df, aes(CINSIYET, OK_YETERLIK, color = SINIF)) +
geom_point()
## Warning: Removed 6 rows containing missing values (geom_point).
ggplot(df, aes(CINSIYET, OK_YETERLIK, color = SINIF)) +
geom_point(position = "jitter")
## Warning: Removed 6 rows containing missing values (geom_point).
size parametresi
parametreler için veri setinden bir değişken değeri seçilebilir.
ggplot(df, aes(CINSIYET, OK_YETERLIK, size = SINIF)) +
geom_point()
## Warning: Using size for a discrete variable is not advised.
## Warning: Removed 6 rows containing missing values (geom_point).
üst üste binen noktaları kaydırarak ayırma
ggplot(df, aes(CINSIYET, OK_YETERLIK, size = SINIF)) +
geom_point(position = "jitter")
## Warning: Using size for a discrete variable is not advised.
## Warning: Removed 6 rows containing missing values (geom_point).
Şeffaflık düzeyi için alpa
ggplot(df, aes(ODOKUMA1, OK_YETERLIK, color = SINIF)) +
geom_point(alpha = 0.4)
## Warning: Removed 6 rows containing missing values (geom_point).
Katmanları nesneye ekleme grafik1 adlı nesneye istenilen katmanlar eklenebilir.
grafik1 <- ggplot(df, aes(ODOKUMA1, OK_YETERLIK, color = SINIF))
grafik1 +geom_point(alpha = 1.2)
## Warning: Removed 6 rows containing missing values (geom_point).
grafik1 adlı nesneye CINSIYET değişkenine göre şekil ekleme
grafik1 +geom_point(aes(shape=CINSIYET))
## Warning: Removed 6 rows containing missing values (geom_point).
text
Değişken adları text komutu ile veri sembolu olarak eklenebilir. Gösterim amacıyla df veri setinin sadece ilk 10 satırı kullanılmıştır.
ggplot(df[1:10,], aes(ODOKUMA1, OK_YETERLIK))+
geom_text(aes(label = CINSIYET))
Scale functions - scale_x() - scale_y() - scale_color() - scale_fill() - scale_shape() - scale_linetype() - scale_size() - scale_x_continuous() - scale_y() - scale_color_discrete() - scale_fill() - scale_shape() - scale_linetype() - scale_size()
ggplot(df, aes(x = ODOKUMA1,
y = OK_YETERLIK,
color = CINSIYET)) +
geom_point(position = "jitter") +
scale_x_continuous("Okuma Puanları") +
scale_color_discrete("Cinsiyet")
## Warning: Removed 6 rows containing missing values (geom_point).
limits
ggplot(df, aes(x = ODOKUMA1,
y = OK_YETERLIK,
color = CINSIYET)) +
geom_point(position = "jitter") +
scale_x_continuous("Okuma Puanları",limits = c(100,900)) +
scale_color_discrete("Cinsiyet")
## Warning: Removed 6 rows containing missing values (geom_point).
beraks
ggplot(df, aes(x = ODOKUMA1,
y = OK_YETERLIK,
color = CINSIYET)) +
geom_point(position = "jitter") +
scale_x_continuous("Okuma Puanları",limits = c(100,900),
breaks=seq(100,900,100)) +
scale_color_discrete("Cinsiyet")
## Warning: Removed 6 rows containing missing values (geom_point).
expand
ggplot(df, aes(x = ODOKUMA1,
y = OK_YETERLIK,
color = CINSIYET)) +
geom_point(position = "jitter") +
scale_x_continuous("Okuma Puanları",limits = c(100,900),
breaks=seq(100,900,100),
expand=c(0,0)) +
scale_color_discrete("Cinsiyet")
## Warning: Removed 6 rows containing missing values (geom_point).
labs
ggplot(df, aes(x = ODOKUMA1,
y = OK_YETERLIK,
color = CINSIYET)) +
geom_point(position = "jitter") +
labs(x = "Basari Puanları",
y = "Yeterlik Puanları",
color = "Grup")
## Warning: Removed 6 rows containing missing values (geom_point).
ggplot(df, aes(CINSIYET, fill = SINIF)) + geom_bar() +
labs(x = "Cinsiyet",
y = "Frekans")
scale_fill
ggplot(df, aes(CINSIYET, fill = SINIF)) +
geom_bar() +
labs(x = "Cinsiyet",
y = "Frekans") +
scale_fill_manual("CINSIYET", values = c("red","blue","orange","green","darkblue"))