使用 Lets-Plot for Kotlin 進行資料視覺化
Lets-Plot for Kotlin (LPK) 是一個多平台繪圖函式庫,它將 R 語言的 ggplot2 函式庫 移植到 Kotlin。LPK 將功能豐富的 ggplot2 API 帶入 Kotlin 生態系統,使其適合需要複雜資料視覺化功能的科學家和統計學家。
LPK 支援多種平台,包括 Kotlin 筆記本、Kotlin/JS、JVM 的 Swing、JavaFX 和 Compose Multiplatform。此外,LPK 還與 IntelliJ、DataGrip、DataSpell 和 PyCharm 無縫整合。
本教學課程演示如何使用 LPK 和 Kotlin DataFrame 函式庫在 IntelliJ IDEA 的 Kotlin 筆記本中建立不同類型的圖表。
開始之前
Kotlin 筆記本依賴於 Kotlin 筆記本外掛程式,該外掛程式在 IntelliJ IDEA 中預設捆綁並啟用。
如果 Kotlin 筆記本功能不可用,請確保外掛程式已啟用。更多資訊請參閱 設定環境。
建立一個新的 Kotlin 筆記本以使用 Lets-Plot:
選擇 檔案 | 新增 | Kotlin 筆記本。
在您的筆記本中,執行以下指令以匯入 LPK 和 Kotlin DataFrame 函式庫:
kotlin%use lets-plot %use dataframe
準備資料
讓我們建立一個 DataFrame,用於儲存柏林、馬德里和卡拉卡斯三座城市每月平均溫度的模擬數字。
使用 Kotlin DataFrame 函式庫中的 dataFrameOf()
函數來生成 DataFrame。將以下程式碼片段貼上並在您的 Kotlin 筆記本中執行:
// The months variable stores a list with 12 months of the year
val months = listOf(
"January", "February",
"March", "April", "May",
"June", "July", "August",
"September", "October", "November",
"December"
)
// The tempBerlin, tempMadrid, and tempCaracas variables store a list with temperature values for each month
val tempBerlin =
listOf(-0.5, 0.0, 4.8, 9.0, 14.3, 17.5, 19.2, 18.9, 14.5, 9.7, 4.7, 1.0)
val tempMadrid =
listOf(6.3, 7.9, 11.2, 12.9, 16.7, 21.1, 24.7, 24.2, 20.3, 15.4, 9.9, 6.6)
val tempCaracas =
listOf(27.5, 28.9, 29.6, 30.9, 31.7, 35.1, 33.8, 32.2, 31.3, 29.4, 28.9, 27.6)
// The df variable stores a DataFrame of three columns, including monthly records, temperature, and cities
val df = dataFrameOf(
"Month" to months + months + months,
"Temperature" to tempBerlin + tempMadrid + tempCaracas,
"City" to List(12) { "Berlin" } + List(12) { "Madrid" } + List(12) { "Caracas" }
)
df.head(4)
您可以看到此 DataFrame 有三列:Month、Temperature 和 City。DataFrame 的前四列包含柏林從一月到四月的溫度記錄:
要使用 LPK 函式庫建立圖表,您需要將資料 (df
) 轉換為以鍵值對形式儲存資料的 Map
型別。您可以使用 .toMap()
函數輕鬆地將 DataFrame 轉換為 Map
:
val data = df.toMap()
建立散佈圖
讓我們使用 LPK 函式庫在 Kotlin 筆記本中建立一個散佈圖。
一旦您的資料為 Map
格式,使用 LPK 函式庫中的 geomPoint()
函數來生成散佈圖。您可以指定 X 和 Y 軸的值,以及定義類別及其顏色。此外,您可以自訂圖表的大小和點的形狀以符合您的需求:
// Specifies X and Y axes, categories and their color, plot size, and plot type
val scatterPlot =
letsPlot(data) { x = "Month"; y = "Temperature"; color = "City" } + ggsize(600, 500) + geomPoint(shape = 15)
scatterPlot
結果如下:
建立箱形圖
讓我們在箱形圖中視覺化資料。使用 LPK 函式庫中的 geomBoxplot()
函數來生成圖表,並使用 scaleFillManual()
函數自訂顏色:
// Specifies X and Y axes, categories, plot size, and plot type
val boxPlot = ggplot(data) { x = "City"; y = "Temperature" } + ggsize(700, 500) + geomBoxplot { fill = "City" } +
// Customizes colors
scaleFillManual(values = listOf("light_yellow", "light_magenta", "light_green"))
boxPlot
結果如下:
建立 2D 密度圖
現在,讓我們建立一個 2D 密度圖來視覺化一些隨機資料的分佈和集中度。
為 2D 密度圖準備資料
匯入依賴項以處理資料並生成圖表:
kotlin%use lets-plot @file:DependsOn("org.apache.commons:commons-math3:3.6.1") import org.apache.commons.math3.distribution.MultivariateNormalDistribution
有關匯入 Kotlin 筆記本依賴項的更多資訊,請參閱 Kotlin 筆記本文件。
將以下程式碼片段貼上並在您的 Kotlin 筆記本中執行,以建立 2D 資料點集:
kotlin// Defines covariance matrices for three distributions val cov0: Array<DoubleArray> = arrayOf( doubleArrayOf(1.0, -.8), doubleArrayOf(-.8, 1.0) ) val cov1: Array<DoubleArray> = arrayOf( doubleArrayOf(1.0, .8), doubleArrayOf(.8, 1.0) ) val cov2: Array<DoubleArray> = arrayOf( doubleArrayOf(10.0, .1), doubleArrayOf(.1, .1) ) // Defines the number of samples val n = 400 // Defines means for three distributions val means0: DoubleArray = doubleArrayOf(-2.0, 0.0) val means1: DoubleArray = doubleArrayOf(2.0, 0.0) val means2: DoubleArray = doubleArrayOf(0.0, 1.0) // Generates random samples from three multivariate normal distributions val xy0 = MultivariateNormalDistribution(means0, cov0).sample(n) val xy1 = MultivariateNormalDistribution(means1, cov1).sample(n) val xy2 = MultivariateNormalDistribution(means2, cov2).sample(n)
從上面的程式碼中,
xy0
、xy1
和xy2
變數儲存包含 2D (x, y
) 資料點的陣列。將您的資料轉換為
Map
型別:kotlinval data = mapOf( "x" to (xy0.map { it[0] } + xy1.map { it[0] } + xy2.map { it[0] }).toList(), "y" to (xy0.map { it[1] } + xy1.map { it[1] } + xy2.map { it[1] }).toList() )
生成 2D 密度圖
使用上一步中的 Map
,建立一個帶有散佈圖 (geomPoint
) 作為背景的 2D 密度圖 (geomDensity2D
),以便更好地視覺化資料點和離群值。您可以使用 scaleColorGradient()
函數來自訂顏色比例:
val densityPlot = letsPlot(data) { x = "x"; y = "y" } + ggsize(600, 300) + geomPoint(
color = "black",
alpha = .1
) + geomDensity2D { color = "..level.." } +
scaleColorGradient(low = "dark_green", high = "yellow", guide = guideColorbar(barHeight = 10, barWidth = 300)) +
theme().legendPositionBottom()
densityPlot
結果如下:
接下來
- 在 Lets-Plot for Kotlin 的文件中 探索更多圖表示例。
- 查閱 Lets-Plot for Kotlin 的 API 參考。
- 在 Kotlin DataFrame 和 Kandy 函式庫文件中了解如何使用 Kotlin 轉換和視覺化資料。
- 查找有關 Kotlin 筆記本使用方式和主要功能 的更多資訊。