%useLatestDescriptors
%use dataframe
%use lets-plot
LetsPlot.getInfo()
Lets-Plot Kotlin API v.4.9.0. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.4.5.1.
var df = DataFrame.readCSV("https://raw.githubusercontent.com/JetBrains/lets-plot-kotlin/master/docs/examples/data/mpg.csv")
df.head(3)
DataFrame: rowsCount = 3, columnsCount = 12
untitled | manufacturer | model | displ | year | cyl | trans | drv | cty | hwy | fl | class |
---|---|---|---|---|---|---|---|---|---|---|---|
1 | audi | a4 | 1.800000 | 1999 | 4 | auto(l5) | f | 18 | 29 | p | compact |
2 | audi | a4 | 1.800000 | 1999 | 4 | manual(m5) | f | 21 | 29 | p | compact |
3 | audi | a4 | 2.000000 | 2008 | 4 | manual(m6) | f | 20 | 31 | p | compact |
// Initialize Lets-Plot GeoTools extension.
%use lets-plot-gt
@file:DependsOn("org.geotools:gt-shapefile:[30,)")
@file:DependsOn("org.geotools:gt-cql:[30,)")
import org.geotools.data.shapefile.ShapefileDataStoreFactory
import org.geotools.data.simple.SimpleFeatureCollection
import java.net.URL
val factory = ShapefileDataStoreFactory()
val worldFeatures : SimpleFeatureCollection = with("naturalearth_lowres") {
val url = "https://raw.githubusercontent.com/JetBrains/lets-plot-kotlin/master/docs/examples/shp/${this}/${this}.shp"
factory.createDataStore(URL(url)).featureSource.features
}
// Convert Feature Collection to SpatialDataset.
// Use 10 decimals to encode floating point numbers (this is the default).
val world = worldFeatures.toSpatialDataset(10)
world["continent"]?.distinct()
[Oceania, Africa, North America, Asia, South America, Europe, Seven seas (open ocean), Antarctica]
val p_plot = ggplot(df.toMap()) + geomPoint {x = "cty"; y="hwy"}
p_plot
val b_plot = letsPlot(df.toMap()) + geomBar {x = "fl"; color="fl"; fill="fl"}
b_plot
val f_plot = b_plot + facetGrid(x="year")
f_plot
val c_plot = CorrPlot(df.toMap(), flip=false).points().build()
c_plot
val g_plot = ggplot() + geomMap(data=world)
g_plot
val e_blank = elementBlank()
val e_line = elementLine(color=java.awt.Color.RED, size=4)
val e_line2 = elementLine(color="#fcae91", size=1)
val e_line3 = elementLine(size=4)
val e_rect = elementRect(color="#2c7fb8", fill="#edf8b1", size=2)
val e_text = elementText(color="#31a354")
val (w, h) = 400 to 300
gggrid(
plots = listOf(
p_plot + ggtitle("themeMinimal2() - the default"),
p_plot + ggtitle("themeLight()") + themeLight(),
p_plot + ggtitle("themeClassic()") + themeClassic(),
p_plot + ggtitle("themeGrey()") + themeGrey(),
p_plot + ggtitle("themeMinimal()") + themeMinimal(),
p_plot + ggtitle("themeNone()") + themeNone()
),
ncol = 2,
cellWidth = w,
cellHeight = h,
fit = true
)
fun orangeTheme() : org.jetbrains.letsPlot.intern.Feature {
val yellowLight = "#ffffcc"
val orangeDark = "#7f2704"
val orangeNormal = "#f16913"
val orangeLight = "#fff5eb"
return themeNone() + theme(
line=elementLine(color=orangeNormal, size=2),
rect=elementRect(color=orangeNormal, fill=orangeLight, size=2),
text=elementText(color=orangeDark),
axisOntop=true,
axisTicks=elementLine(color=orangeNormal, size=1),
axisTicksLength=7,
legendBackground=elementRect(size=1),
panelGridMajor=elementLine(color=orangeNormal, size=.5),
panelGridMinor="blank",
plotBackground=elementRect(fill=yellowLight, size=1),
axisTooltip=elementRect(color=orangeDark)
).legendPositionBottom()
}
gggrid(
plots = listOf(
p_plot + orangeTheme() + ggtitle("Scatter plot"),
b_plot + orangeTheme() + ggtitle("Bar plot"),
c_plot + orangeTheme() + ggtitle("Correlation plot"),
g_plot + orangeTheme() + ggtitle("World plot"),
),
ncol = 2,
cellWidth = w,
cellHeight = h,
fit = true
)
b_plot + theme().legendPosition(1, 1).legendJustification(1, 1).legendDirectionHorizontal() +
ggtitle("Place legend")
gggrid(
plots = listOf(
p_plot + ggtitle("axisTooltipX=e_blank") + theme(axisTooltipX = e_blank),
p_plot + ggtitle("axisTooltipX=e_rect") + theme(axisTooltipX = e_rect),
),
ncol = 2,
cellWidth = w,
cellHeight = h,
)
gggrid(
plots = listOf(
p_plot + ggtitle("Default"),
p_plot + ggtitle("line=e_line") + theme(line = e_line),
),
ncol = 2,
cellWidth = w,
cellHeight = h,
)
gggrid(
plots = listOf(
b_plot + ggtitle("themeNone") + themeNone(),
b_plot + ggtitle("themeNone + rect") + themeNone() + theme(rect = e_rect),
),
ncol = 2,
cellWidth = w,
cellHeight = h,
)
gggrid(
plots = listOf(
f_plot + ggtitle("Default"),
f_plot + ggtitle("text = e_text") + theme(text = e_text),
f_plot + ggtitle("legendText = e_text") + theme(legendText = e_text),
f_plot + ggtitle("stripText = e_text") + theme(stripText = e_text),
),
ncol = 2,
cellWidth = w,
cellHeight = h,
fit = true
)
gggrid(
plots = listOf(
b_plot + ggtitle("Default"),
b_plot + ggtitle("text = e_text") + theme(text = e_text),
b_plot + ggtitle("plotTitle = e_text") + theme(plotTitle = e_text),
b_plot + ggtitle("legendTitle = e_text") + theme(legendTitle = e_text),
),
ncol = 2,
cellWidth = w,
cellHeight = h,
fit = true
)
gggrid(
plots = listOf(
p_plot + ggtitle("Default"),
p_plot + ggtitle("axis = e_blank") + theme(axis = e_blank),
p_plot + ggtitle("axis = e_line") + theme(axis = e_line),
p_plot + ggtitle("axis = e_text") + theme(axis = e_text),
),
ncol = 2,
cellWidth = w,
cellHeight = h,
fit = true
)
gggrid(
plots = listOf(
p_plot + ggtitle("Default"),
p_plot + ggtitle("panelBackground = e_rect") + theme(panelBackground = e_rect),
),
ncol = 2,
cellWidth = w,
cellHeight = h,
fit = true
)
gggrid(
plots = listOf(
b_plot + ggtitle("panelGrid = e_blank") + theme(panelGrid = e_blank),
p_plot + ggtitle("panelGrid = e_line") + theme(panelGrid = e_line),
p_plot + ggtitle("panelGridMajor = e_line, panelGridMinor = e_line2") +
theme(panelGridMajor = e_line, panelGridMinor = e_line2),
b_plot + ggtitle("panelGridMajorX = e_blank, panelGridMajorY, panelGridMinorY..") +
theme(panelGridMajorX = e_blank, panelGridMajorY = e_line,
panelGridMinorY = e_line2),
),
ncol = 2,
cellWidth = w,
cellHeight = h,
fit = true
)
gggrid(
plots = listOf(
f_plot + ggtitle("themeGrey") + themeGrey(),
f_plot + ggtitle("stripBackground = e_rect") + themeGrey() + theme(stripBackground = e_rect),
f_plot + ggtitle("stripBackground = e_blank") + themeGrey() + theme(stripBackground = e_blank),
f_plot + ggtitle("stripText = e_blank") + themeGrey() + theme(stripText = e_blank),
),
ncol = 2,
cellWidth = w,
cellHeight = h,
fit = true
)
gggrid(
plots = listOf(
b_plot + ggtitle("Default") + theme(axisLineX = e_line3),
b_plot + ggtitle("axisOntopX = true") + theme(axisLineX = e_line3, axisOntopX = true),
),
ncol = 2,
cellWidth = w,
cellHeight = h,
fit = true
)
gggrid(
plots = listOf(
b_plot + ggtitle("Default") + coordFlip(),
b_plot + ggtitle("Plot 1") + coordFlip() + theme(line=e_line, text=e_text),
b_plot + ggtitle("Plot 2") + coordFlip() + theme(axis=e_line, legendTitle=e_text,
axisOntop=true
),
b_plot + ggtitle("Plot 3") + coordFlip() + theme(plotTitle=e_text,
panelBackground=e_rect,
panelGridMajorX=e_blank,
panelGridMajorY=e_line,
panelGridMinorX=e_blank,
panelGridMinorY=e_line2,
),
f_plot + ggtitle("Plot 4") + coordFlip() + theme(stripBackground=e_rect, stripText=e_text),
b_plot + ggtitle("Plot 5") + coordFlip() + theme(axisTitleX=e_text,
axisTextX=e_text,
axisTicksX=e_line,
axisLineX=e_line2,
axisTooltipX=e_blank,
).legendPositionBottom().legendDirectionHorizontal(),
),
ncol = 2,
cellWidth = w,
cellHeight = h,
fit = true
)
gggrid(
plots = listOf(
c_plot + ggtitle("Default"),
c_plot + ggtitle("With theme") + theme(text=e_text,
axis=e_line,
panelBackground=e_rect,
axisTitleX=e_text,
axisTextX=e_text,
axisTicksX=e_line,
axisLineX=e_line,
axisTooltipX=e_blank
),
),
ncol = 2,
cellWidth = w,
cellHeight = h,
fit = true
)
gggrid(
plots = listOf(
g_plot + ggtitle("Default"),
g_plot + ggtitle("coordCartesian()") + coordCartesian(),
g_plot + ggtitle("coordFixed()") + coordFixed(),
g_plot + ggtitle("themeClassic()") + themeClassic(),
),
ncol = 2,
cellWidth = w,
cellHeight = h,
fit = true
)
val pt_plot = ggplot(df.toMap()) {x = "cty"; y = "hwy"} +
geomPoint(tooltips=layerTooltips().anchor("top_center").minWidth(50)) {color="fl"}
gggrid(
plots = listOf(
pt_plot + ggtitle("Tooltip: top-center"),
pt_plot + ggtitle("Gray background") + theme(panelBackground=elementRect(color="black", fill="gray")),
),
ncol = 2,
cellWidth = w,
cellHeight = h,
)