R ile veri görselleştirme yöntemleri

GGPlo2 ve Plotly paketleri ile ilgili veri görselleştirme yöntemleri

  • Veri Görselleştirme
  • Araçlar
R ile veri görselleştirme yöntemleri

R paket programında en çok kullanılan görselleştirme programlarının kod yapısı, karşılaştırılmasına yönelik yazı olacaktır.

Ggplot2 paketinin çıktıları statik görselleştirmeler (PNG, PDF vb.). (Bazı eklentilerle etkileşimli hale getirilebilir.)
Plotly paketinin çıktıları etkileşimli web tabanlı görselleştirmeler (HTML/JavaScript).

Kod Yapısı

Ggplot2 Kod Yapısı (Katmanlı Yapı)

ggplot2, her grafik bileşenini artı (+) işaretiyle birleştiren katmanlı bir yapı kullanır.

Temel Bileşenler:

  1. Veri ve Estetik Eşlemeler (ggplot()): Veri kümesi ve hangi değişkenlerin hangi estetik özelliklere (x ekseni, y ekseni, renk, boyut) eşleneceği tanımlanır.
  2. Geometrik Nesneler (geom_...): Grafiğin türü (nokta, çizgi, sütun, kutu vb.) belirlenir.
  3. Ölçekler (scale_...): Renk, dolgu, eksen aralıkları gibi estetiklerin ölçekleri ayarlanır.
  4. Koordinat Sistemi (coord_...): Kartezyen, kutupsal gibi koordinat sistemi belirlenir.
  5. Fasetleme (facet_...): Verileri alt gruplara ayırıp birden çok küçük grafik oluşturur.
  6. Temalar (theme_...): Görsel stil (fontlar, arka plan, eksen çizgileri) ayarlanır.
# ggplot2 Örneği: Dağılım Grafiği
library(ggplot2)

ggplot(data = iris,
       aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point(alpha = 0.7) +
  labs(title = "İris Veri Seti Dağılımı",
       x = "Çanak Yaprağı Uzunluğu",
       y = "Çanak Yaprağı Genişliği") +
  theme_minimal()

Plotly Kod Yapısı (Pipe-And-Function Yapısı)

Plotly, genellikle temel bir grafik fonksiyonuyla başlar ve ardından pipe (|>) veya (%>%) operatörleri ile interaktif özellikler ve düzenlemeler eklenir.

Temel Bileşenler:

  1. Temel Grafik Fonksiyonu (plot_ly() veya plot_geo()): Veri ve temel eksen/tür tanımlanır.
  2. Modlar (add_markers(), add_lines(), vb.): Grafiğe geometri eklenir.
  3. Düzenleme Fonksiyonları (layout()): Başlıklar, eksen adları, lejant ayarları gibi statik düzenlemeler yapılır.
  4. Yapılandırma Fonksiyonları (config()): İndirme düğmeleri, yakınlaştırma/kaydırma özellikleri gibi etkileşimli ayarlar yapılır.
# Plotly Örneği: Dağılım Grafiği
library(plotly)

plot_ly(data = iris,
        x = ~Sepal.Length,
        y = ~Sepal.Width,
        color = ~Species,
        type = "scatter",
        mode = "markers") |>
  layout(title = "İris Veri Seti Dağılımı (Etkileşimli)",
         xaxis = list(title = "Çanak Yaprağı Uzunluğu"),
         yaxis = list(title = "Çanak Yaprağı Genişliği")) |>
  config(displayModeBar = TRUE) # Araç çubuğunu etkinleştir

Ggplot2 ve Plotly kütüphanelerinin detaylı kullanımlarına yönelik cheat sheet'lerinin pdf linklerini bırakıyorum buraya;

https://posit.co/wp-content/uploads/2022/10/data-visualization-1.pdf

https://images.plot.ly/plotly-documentation/images/r_cheat_sheet.pdf

This article looks at the code structure of the most widely used visualization packages in R and compares them.

The ggplot2 package produces static visualizations (PNG, PDF, etc.). (They can be made interactive with some extensions.)
The Plotly package produces interactive, web-based visualizations (HTML/JavaScript).

Code Structure

ggplot2 Code Structure (Layered Structure)

ggplot2 uses a layered structure that combines each chart component with a plus (+) sign.

Core Components:

  1. Data and Aesthetic Mappings (ggplot()): Defines the dataset and which variables are mapped to which aesthetics (x axis, y axis, color, size).
  2. Geometric Objects (geom_...): Sets the chart type (point, line, bar, box, etc.).
  3. Scales (scale_...): Adjusts the scales of aesthetics such as color, fill and axis ranges.
  4. Coordinate System (coord_...): Sets the coordinate system, such as Cartesian or polar.
  5. Faceting (facet_...): Splits the data into subgroups and creates several small charts.
  6. Themes (theme_...): Adjusts the visual style (fonts, background, axis lines).
# ggplot2 Örneği: Dağılım Grafiği
library(ggplot2)

ggplot(data = iris,
       aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point(alpha = 0.7) +
  labs(title = "İris Veri Seti Dağılımı",
       x = "Çanak Yaprağı Uzunluğu",
       y = "Çanak Yaprağı Genişliği") +
  theme_minimal()

Plotly Code Structure (Pipe-and-Function Structure)

Plotly usually starts with a base chart function, and interactive features and formatting are then added with the pipe (|>) or (%>%) operators.

Core Components:

  1. Base Chart Function (plot_ly() or plot_geo()): Defines the data and the basic axes/type.
  2. Modes (add_markers(), add_lines(), etc.): Adds geometry to the chart.
  3. Layout Functions (layout()): Applies static formatting such as titles, axis names and legend settings.
  4. Configuration Functions (config()): Sets interactive options such as download buttons and zoom/pan behavior.
# Plotly Örneği: Dağılım Grafiği
library(plotly)

plot_ly(data = iris,
        x = ~Sepal.Length,
        y = ~Sepal.Width,
        color = ~Species,
        type = "scatter",
        mode = "markers") |>
  layout(title = "İris Veri Seti Dağılımı (Etkileşimli)",
         xaxis = list(title = "Çanak Yaprağı Uzunluğu"),
         yaxis = list(title = "Çanak Yaprağı Genişliği")) |>
  config(displayModeBar = TRUE) # Araç çubuğunu etkinleştir

Here are the PDF links to the cheat sheets for using the ggplot2 and Plotly libraries in detail:

https://posit.co/wp-content/uploads/2022/10/data-visualization-1.pdf

https://images.plot.ly/plotly-documentation/images/r_cheat_sheet.pdf