ggplot2 grafiklerin dil bilgisi (grammar of graphics) prensiplerini temel alarak oluşturulmuştur. Bu prensiplere göre her grafik aynı parçalardan oluşturulabilir: bir veri seti, koordinat sistemi, ve "geom"lar - veri noktalarını temsil eden görsel işaretler. Temeller BASİT GRAFİKLER a + geom_blank() (Grafiğin sınırlarını genişletmek için faydalı.) b + geom_curve(aes(yend = lat + 1, xend=long+1,curvature=z)) - x, xend, y, yend, alpha, angle, color, curvature, linetype, size a + geom_path(lineend="butt", linejoin="round", linemitre=1) x, y, alpha, color, group, linetype, size a + geom_polygon(aes(group = group)) x, y, alpha, color, fill, group, linetype, size b + geom_rect(aes(xmin = long, ymin=lat, xmax= long + 1, ymax = lat + 1)) - xmax, xmin, ymax, ymin, alpha, color, fill, linetype, size a + geom_ribbon(aes(ymin=unemploy - 900, ymax=unemploy + 900)) - x, ymax, ymin, alpha, color, fill, group, linetype, size + = ggplot2 ile Veri Görselleştirme : : REFERANS KAĞIDI Değerleri göstermek için, verideki değişkenleri ilgili geom'un parametrelerine (aes, estetikleri) atayın. Bu parametrelerin bazıları büyüklük (size), renk (color), x ve y koordinatlarıdır. + = veri geom x = F · y = A koordinat sistemi grafik veri geom x = F · y = A color = F size = A koordinat sistemi grafik Aşağıdaki taslağı kullanarak bir grafik oluşturabilirsiniz. gerekli ggplot(data = mpg, aes(x = cty, y = hwy)) Katman ekleyerek bitirilmesi gereken bir grafiğin tabanını oluşturur. Her katman için bir geom fonksiyonu ekleyin. qplot(x = cty, y = hwy, data = mpg, geom = “point") Bir veri seti, geom ve parametre seti için tam teşekküllü bir grafik oluşturur. Çok sayıda faydalı varsayılan değer içerir. last_plot() Son grafiği getirir ggsave("plot.png", width = 5, height = 5) Son grafiği 5' x 5' boyutunda “plot.png” isimli bir dosya olarak çalışma klasörüne kaydeder. Dosya türünü, uzantısına (ör. png) göre ayarlar. F M A F M A parametre veri geom ÇİZGİ PARÇALARI b + geom_abline(aes(intercept=0, slope=1)) b + geom_hline(aes(yintercept = lat)) b + geom_vline(aes(xintercept = long)) ortak parametreler: x, y, alpha, color, linetype, size b + geom_segment(aes(yend=lat+1, xend=long+1)) b + geom_spoke(aes(angle = 1:1155, radius = 1)) a <- ggplot(economics, aes(date, unemploy)) b <- ggplot(seals, aes(x = long, y = lat)) TEK DEĞİŞKENLİ sürekli c <- ggplot(mpg, aes(hwy)); c2 <- ggplot(mpg) c + geom_area(stat = "bin") x, y, alpha, color, fill, linetype, size c + geom_density(kernel = "gaussian") x, y, alpha, color, fill, group, linetype, size, weight c + geom_dotplot() x, y, alpha, color, fill c + geom_freqpoly() x, y, alpha, color, group, linetype, size c + geom_histogram(binwidth = 5) x, y, alpha, color, fill, linetype, size, weight c2 + geom_qq(aes(sample = hwy)) x, y, alpha, color, fill, linetype, size, weight kesikli d <- ggplot(mpg, aes(fl)) d + geom_bar() x, alpha, color, fill, linetype, size, weight e + geom_label(aes(label = cty), nudge_x = 1, nudge_y = 1, check_overlap = TRUE) x, y, label, alpha, angle, color, family, fontface, hjust, lineheight, size, vjust e + geom_jitter(height = 2, width = 2) x, y, alpha, color, fill, shape, size e + geom_point(), x, y, alpha, color, fill, shape, size, stroke e + geom_quantile(), x, y, alpha, color, group, linetype, size, weight e + geom_rug(sides = "bl"), x, y, alpha, color, linetype, size e + geom_smooth(method = lm), x, y, alpha, color, fill, group, linetype, size, weight e + geom_text(aes(label = cty), nudge_x = 1, nudge_y = 1, check_overlap = TRUE), x, y, label, alpha, angle, color, family, fontface, hjust, lineheight, size, vjust kesikli x , sürekli y f <- ggplot(mpg, aes(class, hwy)) f + geom_col(), x, y, alpha, color, fill, group, linetype, size f + geom_boxplot(), x, y, lower, middle, upper, ymax, ymin, alpha, color, fill, group, linetype, shape, size, weight f + geom_dotplot(binaxis = "y", stackdir = "center"), x, y, alpha, color, fill, group f + geom_violin(scale = "area"), x, y, alpha, color, fill, group, linetype, size, weight kesikli x , kesikli y g <- ggplot(diamonds, aes(cut, color)) g + geom_count(), x, y, alpha, color, fill, shape, size, stroke ÜÇ DEĞİŞKENLİ seals$z <- with(seals, sqrt(delta_long^2 + delta_lat^2)) l <- ggplot(seals, aes(long, lat)) l + geom_contour(aes(z = z)) x, y, z, alpha, colour, group, linetype, size, weight l + geom_raster(aes(fill = z), hjust=0.5, vjust=0.5, interpolate=FALSE) x, y, alpha, fill l + geom_tile(aes(fill = z)), x, y, alpha, color, fill, linetype, size, width h + geom_bin2d(binwidth = c(0.25, 500)) x, y, alpha, color, fill, linetype, size, weight h + geom_density2d() x, y, alpha, colour, group, linetype, size h + geom_hex() x, y, alpha, colour, fill, size i + geom_area() x, y, alpha, color, fill, linetype, size i + geom_line() x, y, alpha, color, group, linetype, size i + geom_step(direction = "hv") x, y, alpha, color, group, linetype, size j + geom_crossbar(fatten = 2) x, y, ymax, ymin, alpha, color, fill, group, linetype, size j + geom_errorbar(), x, ymax, ymin, alpha, color, group, linetype, size, width (also geom_errorbarh()) j + geom_linerange() x, ymin, ymax, alpha, color, group, linetype, size j + geom_pointrange() x, y, ymin, ymax, alpha, color, fill, group, linetype, shape, size sürekli fonksiyon i <- ggplot(economics, aes(date, unemploy)) hata değerlerini görselleştirmek df <- data.frame(grp = c("A", "B"), fit = 4:5, se = 1:2) j <- ggplot(df, aes(grp, fit, ymin = fit-se, ymax = fit+se)) haritalar data <- data.frame(murder = USArrests$Murder, state = tolower(rownames(USArrests))) map <- map_data("state") k <- ggplot(data, aes(fill = murder)) k + geom_map(aes(map_id = state), map = map) + expand_limits(x = map$long, y = map$lat), map_id, alpha, color, fill, linetype, size Elzem değil, düzgün varsayılan değerleri sayesinde rahatça kullanılabilir. Geom Veri noktalarının gösterimleri için bir geom fonksiyonu kullanın. Geom'un aes parametrelerini de değişken gösterimleri için kullanabilirsiniz. Her fonksiyon, grafiğe bir katman ekler. İKİ DEĞİŞKENLİ sürekli x , sürekli y e <- ggplot(mpg, aes(cty, hwy)) sürekli iki değişkenli dağılım h <- ggplot(diamonds, aes(carat, price)) RStudio® is a trademark of RStudio, Inc. • CC BY RStudio • [email protected]• 844-448-1212 • rstudio.com • Learn more at http://ggplot2.tidyverse.org • ggplot2 2.1.0 • Updated: 2016-11 ggplot (data = <VERİ> ) + <GEOM_FONKSİYONU> (mapping = aes( <ATAMALAR> ), stat = <STAT> , position = <POZİSYON> ) + <KOORDİNAT_FONKSİYONU> + <FACET_FONKSİYONU> + <SCALE_FONKSİYONU> + <THEME_FONKSİYONU>
2
Embed
ggplot2 ile Veri Görselle tirme : : REFERANS KAĞIDI
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
ggplot2 grafiklerin dil bilgisi (grammar of graphics) prensiplerini temel alarak oluşturulmuştur. Bu prensiplere göre her grafik aynı parçalardan oluşturulabilir: bir veri seti, koordinat sistemi, ve "geom"lar - veri noktalarını temsil eden görsel işaretler.
TemellerBASİT GRAFİKLER
a + geom_blank()(Grafiğin sınırlarını genişletmek için faydalı.)
b + geom_curve(aes(yend = lat + 1,xend=long+1,curvature=z)) - x, xend, y, yend, alpha, angle, color, curvature, linetype, size
a + geom_path(lineend="butt", linejoin="round", linemitre=1) x, y, alpha, color, group, linetype, size
a + geom_polygon(aes(group = group))x, y, alpha, color, fill, group, linetype, size
b + geom_rect(aes(xmin = long, ymin=lat, xmax= long + 1, ymax = lat + 1)) - xmax, xmin, ymax, ymin, alpha, color, fill, linetype, size
ggplot2 ile Veri Görselleştirme : : REFERANS KAĞIDI
Değerleri göstermek için, verideki değişkenleri ilgili geom'un parametrelerine (aes, estetikleri) atayın. Bu parametrelerin bazıları büyüklük (size), renk (color), x ve y koordinatlarıdır.
+ =
veri geomx = F · y = A
koordinat sistemi
grafik
veri geomx = F · y = Acolor = Fsize = A
koordinat sistemi
grafik
Aşağıdaki taslağı kullanarak bir grafik oluşturabilirsiniz.gerekli
ggplot(data = mpg, aes(x = cty, y = hwy)) Katman ekleyerek bitirilmesi gereken bir grafiğin tabanını oluşturur. Her katman için bir geom fonksiyonu ekleyin.
qplot(x = cty, y = hwy, data = mpg, geom = “point")
Bir veri seti, geom ve parametre seti için tam teşekküllü bir grafik oluşturur. Çok sayıda faydalı varsayılan değer içerir.
last_plot() Son grafiği getirir
ggsave("plot.png", width = 5, height = 5) Son grafiği 5' x 5' boyutunda “plot.png” isimli bir dosya olarak çalışma klasörüne kaydeder. Dosya türünü, uzantısına (ör. png) göre ayarlar.
Elzem değil,düzgün varsayılan değerleri sayesinde rahatça kullanılabilir.
Geom Veri noktalarının gösterimleri için bir geom fonksiyonu kullanın. Geom'un aes parametrelerini de değişken gösterimleri için kullanabilirsiniz. Her fonksiyon, grafiğe bir katman ekler.
İKİ DEĞİŞKENLİsürekli x , sürekli ye <- ggplot(mpg, aes(cty, hwy))
sürekli iki değişkenli dağılımh <- ggplot(diamonds, aes(carat, price))
RStudio® is a trademark of RStudio, Inc. • CC BY RStudio • [email protected] • 844-448-1212 • rstudio.com • Learn more at http://ggplot2.tidyverse.org • ggplot2 2.1.0 • Updated: 2016-11
Ölçek (Scale) Koordinat SistemleriBir stat grafik çizdirmek için yeni değişkenler kullanır. (ör., count, prop).
Stat Katman oluşturmanın alternatif yolu.
+ =veri geom
x = x ·y = ..count..
koordinat sistemi
grafik
fl ct cylx ..count.
stat
Bir istatistiği (ör. histogram), bir geom fonksiyonunun stat parametresini değiştirerek geom_bar(stat="count") veya bir stat fonksiyonu kullanarak stat_count(geom="bar") görselleştirebilirsiniz. Stat fonksiyonu geom fonksiyonuna denk bir katman oluşturur. Stat değişkenlerini aes parametrelerine atamak için ..isim.. stilini kullanın.
i + stat_density2d(aes(fill = ..level..), geom = "polygon")
stat fonk. geom
stat değişkeni
geom
c + stat_bin(binwidth = 1, origin = 10)x, y | ..count.., ..ncount.., ..density.., ..ndensity..c + stat_count(width = 1) x, y, | ..count.., ..prop..c + stat_density(adjust = 1, kernel = “gaussian") x, y, | ..count.., ..density.., ..scaled..
e + stat_bin_2d(bins = 30, drop = T)x, y, fill | ..count.., ..density..e + stat_bin_hex(bins=30) x, y, fill | ..count.., ..density..e + stat_density_2d(contour = TRUE, n = 100)x, y, color, size | ..level..e + stat_ellipse(level = 0.95, segments = 51, type = "t")l + stat_contour(aes(z = z)) x, y, z, order | ..level..l + stat_summary_hex(aes(z = z), bins = 30, fun = max)x, y, z, fill | ..value..l + stat_summary_2d(aes(z = z), bins = 30, fun = mean)x, y, z, fill | ..value..f + stat_boxplot(coef = 1.5) x, y | ..lower.., ..middle.., ..upper.., ..width.. , ..ymin.., ..ymax..f + stat_ydensity(kernel = "gaussian", scale = “area") x, y | ..density.., ..scaled.., ..count.., ..n.., ..violinwidth.., ..width..
e + stat_ecdf(n = 40) x, y | ..x.., ..y..e + stat_quantile(quantiles = c(0.1, 0.9), formula = y ~ log(x), method = "rq") x, y | ..quantile..e + stat_smooth(method = "lm", formula = y ~ x, se=T, level=0.95) x, y | ..se.., ..x.., ..y.., ..ymin.., ..ymax..
GENEL AMAÇLI ÖLÇEKLERÇoğu aes parametresine uygundur.scale_*_continuous() - sürekli değerleri görsele atamascale_*_discrete() - kesikli değerleri görsele atamascale_*_identity() - veri değerlerini görsel değer yapmascale_*_manual(values = c()) - kesikli değerleri elle seçili görsel değerlere atamascale_*_date(date_labels = "%m/%d"), date_breaks = "2 weeks") - veri değerlerini tarih olarak kullanma. scale_*_datetime() - veri x değerlerini tarih-zaman olarak atama. scale_x_date() ile aynı parametreleri kullanır. Etiket formatları için konsola ?strptime yazın.
X & Y KONUM ÖLÇEKLERİx ve y aes parametreleriyle kullanın (x örn. aşağıda)scale_x_log10() - x değerleri log10 ölçeğindescale_x_reverse() - x koordinatını ters yüz etmescale_x_sqrt() - x değerleri kare kök ölçeğinde
COLOR VE FILL ÖLÇEKLERİ (KESİKLİ)n <- d + geom_bar(aes(fill = fl))n + scale_fill_brewer(palette = "Blues") Renk skalası seçenekleri için: RColorBrewer::display.brewer.all()n + scale_fill_grey(start = 0.2, end = 0.8, na.value = "red")
COLOR VE FILL ÖLÇEKLERİ (SÜREKLİ)o <- c + geom_dotplot(aes(fill = ..x..))o + scale_fill_distiller(palette = "Blues")o + scale_fill_gradient(low="red", high="yellow")o + scale_fill_gradient2(low="red", high=“blue", mid = "white", midpoint = 25) o +scale_fill_gradientn(colours=topo.colors(6)) Ayrıca: rainbow(), heat.colors(), terrain.colors(), cm.colors(), RColorBrewer::brewer.pal()
Konum Düzenlemelerigeomların üst üste çakışmasını önlemek için yapılan konum (position) düzenlemeleri.
s <- ggplot(mpg, aes(fl, fill = drv))
s + geom_bar(position = "dodge")Değerleri yan yana koyma.
s + geom_bar(position = "fill") Değerleri tepe tepeye koyup normalize etme.e + geom_point(position = "jitter") X ve Y değerleriyle oynayarak noktaların üst üste çakışmasını engelleme
e + geom_label(position = "nudge") Etiketleri noktalarla üst üste gelmesini engelleme.
s + geom_bar(position = "stack") Değerleri diğerlerinin tepesine koyma
Her pozisyon parametresi bir fonksiyon olarak çağırılabilir ve elle uzunluk ve genişlik parametreleri ayarlanabilir.s + geom_bar(position = position_dodge(width = 1))
AB
Temalar (Theme)r + theme_bw()Beyaz arkaplan, ızgara çizgilerir + theme_gray()Gri arkaplan (varsayılan tema)r + theme_dark()kontrast için karanlık tema
r + theme_classic()r + theme_light()r + theme_linedraw()r + theme_minimal()Minimal temar + theme_void()Boş tema
Cephe(Facet)Cepheler (facet) bir grafiği bir veya daha fazla kesikli değişkenin değerlerine göre alt grafiklere ayırır.
t <- ggplot(mpg, aes(cty, hwy)) + geom_point()
t + facet_grid(. ~ fl) fl değişkenine göre sütunlara ayırmat + facet_grid(year ~ .)year değişkenine göre satırlara ayırmat + facet_grid(year ~ fl)year ve fl'ye göre hem satır hem sütuna ayırmat + facet_wrap(~ fl)cepheleri dikdörtgen düzeninde yerleştirme
Cephelerin genel koordinat limitleri için scales kullanın.t + facet_grid(drv ~ fl, scales = "free")x ve y koordinat limitlerini ayrı ayrı ayarlayabilirsiniz“free_x" - x koordinatı limit ayarı"free_y" - y koordinatı limit ayarılabeller parametresi ile cephe isimlerini verebilirsiniz.t + facet_grid(. ~ fl, labeller = label_both)
t + facet_grid(fl ~ ., labeller = label_bquote(alpha ^ .(fl)))
t + facet_grid(. ~ fl, labeller = label_parsed)
fl: c fl: d fl: e fl: p fl: r
c d e p r
↵c ↵d ↵e ↵p ↵r
Etiketler (label)t + labs( x = “Yeni x doğrusu Etiketi", y = “Yeni y doğrusu etiketi",title =“Grafiğe başlık ekleme", subtitle = “Grafiğe alt başlık ekleme”,caption = “Grafiğe alt yazı ekle”)
t + annotate(geom = "text", x = 8, y = 9, label = "A")
Lejant etiketlerini değiştirmek için Scale fonksiyonların kullanabilirsiniz.
ekleyeceğiniz geom geom’un yeri
Lejant (Legend)n + theme(legend.position = "bottom") Place legend at "bottom", "top", "left", or "right"n + guides(fill = "none") Set legend type for each aesthetic: colorbar, legend, or none (no legend) n + scale_fill_discrete(name = "Title", labels = c("A", "B", "C", "D", "E"))Set legend title and labels with a scale function.
RStudio® is a trademark of RStudio, Inc. • CC BY RStudio • [email protected] • 844-448-1212 • rstudio.com • Learn more at http://ggplot2.tidyverse.org • ggplot2 2.1.0 • Updated: 2016-11