Skip to content

Commit 15766a1

Browse files
Add the drop parameter to facetWrap()
1 parent 41f6082 commit 15766a1

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "b64186c9-ae08-4eae-8ea8-d1ae43bc8b95",
6+
"metadata": {},
7+
"source": [
8+
"# Handling of Empty Panels in `facetWrap()`"
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": null,
14+
"id": "0ce1758f-9bdb-4213-b22e-8376d39681d6",
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"%useLatestDescriptors\n",
19+
"%use lets-plot\n",
20+
"%use dataframe"
21+
]
22+
},
23+
{
24+
"cell_type": "code",
25+
"execution_count": null,
26+
"id": "32276694-1825-447a-ad40-b6fd30abc47f",
27+
"metadata": {},
28+
"outputs": [],
29+
"source": [
30+
"LetsPlot.getInfo()"
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": null,
36+
"id": "2c595e38-65ec-40d3-a16b-927879998360",
37+
"metadata": {},
38+
"outputs": [],
39+
"source": [
40+
"import org.jetbrains.kotlinx.dataframe.api.*\n",
41+
"\n",
42+
"val df = DataFrame.readCSV(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg2.csv\")\n",
43+
"val data = df.toMap() "
44+
]
45+
},
46+
{
47+
"cell_type": "code",
48+
"execution_count": null,
49+
"id": "49e00fb9-1a05-4366-ad23-c6973a942ac1",
50+
"metadata": {},
51+
"outputs": [],
52+
"source": [
53+
"val p = letsPlot(data) {\n",
54+
" x = \"engine horsepower\"\n",
55+
" y = \"miles per gallon\"\n",
56+
" color = asDiscrete(\"origin of car\", order = -1)\n",
57+
"} + geomPoint() +\n",
58+
" themeGrey() +\n",
59+
" ggtitle(\"Efficiency vs Engine Horsepower\")\n",
60+
"\n",
61+
"p"
62+
]
63+
},
64+
{
65+
"cell_type": "markdown",
66+
"id": "467498b5-95ac-4cce-9ce0-9024af0120b2",
67+
"metadata": {},
68+
"source": [
69+
"### Empty Facet Panels are Dropped by Default\n",
70+
"\n",
71+
"For example, there are no 3-cylinder engine cars originated in the US. Accordingly, this facet panel is dropped."
72+
]
73+
},
74+
{
75+
"cell_type": "code",
76+
"execution_count": null,
77+
"id": "ebda0228-9500-4e48-ba05-64f98b645218",
78+
"metadata": {},
79+
"outputs": [],
80+
"source": [
81+
"p + facetWrap(\n",
82+
" listOf(\"origin of car\", \"number of cylinders\"),\n",
83+
" order = listOf(-1, 1),\n",
84+
" ncol = 3\n",
85+
")"
86+
]
87+
},
88+
{
89+
"cell_type": "markdown",
90+
"id": "28a91a2c-38dd-4f75-b9aa-2d7daa624070",
91+
"metadata": {},
92+
"source": [
93+
"### Using the `drop` Parameter to Control Empty Panels\n",
94+
"\n",
95+
"Use `drop=false` to show all factor levels regardless of whether they occur in the data."
96+
]
97+
},
98+
{
99+
"cell_type": "code",
100+
"execution_count": null,
101+
"id": "adfcc480-582e-41b9-b6d9-1f2c2c687a0b",
102+
"metadata": {},
103+
"outputs": [],
104+
"source": [
105+
"p + facetWrap(\n",
106+
" listOf(\"origin of car\", \"number of cylinders\"),\n",
107+
" order = listOf(-1, 1),\n",
108+
" ncol = 5,\n",
109+
" drop = false\n",
110+
")"
111+
]
112+
}
113+
],
114+
"metadata": {
115+
"kernelspec": {
116+
"display_name": "Kotlin [conda env:lpk-master]",
117+
"language": "kotlin",
118+
"name": "conda-env-lpk-master-kotlin"
119+
},
120+
"language_info": {
121+
"codemirror_mode": "text/x-kotlin",
122+
"file_extension": ".kt",
123+
"mimetype": "text/x-kotlin",
124+
"name": "kotlin",
125+
"nbconvert_exporter": "",
126+
"pygments_lexer": "kotlin",
127+
"version": "1.9.23"
128+
}
129+
},
130+
"nbformat": 4,
131+
"nbformat_minor": 5
132+
}

plot-api/src/commonMain/kotlin/org/jetbrains/letsPlot/facet/facet_wrap.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import org.jetbrains.letsPlot.intern.filterNonNullValues
3333
* The `order` values are positionally matched to variables in `facets`.
3434
* @param format Specifies the format pattern for displaying faceting values.
3535
* The `format` values are positionally matched to variables in `facets`.
36+
* @param drop Specifies whether to drop unused factor levels (the default behavior)
37+
* or to show all factor levels regardless of whether they occur in the data.
3638
* @param dir Direction: either "h" for horizontal, the default, or "v", for vertical.
3739
* @param labWidth Specifies the maximum label length (in characters) before a line break is applied.
3840
* The `labWidth` values are positionally matched to variables in `facets`.
@@ -55,6 +57,7 @@ fun facetWrap(
5557
scales: String? = null,
5658
order: Any? = 1,
5759
format: Any? = null,
60+
drop: Boolean = true,
5861
dir: String = "h",
5962
labWidth: Any? = null
6063
): OptionsMap {
@@ -68,6 +71,7 @@ fun facetWrap(
6871
Option.Facet.SCALES to scales,
6972
Option.Facet.FACETS_ORDER to order,
7073
Option.Facet.FACETS_FORMAT to format,
74+
Option.Facet.DROP_UNUSED_LEVELS to drop,
7175
Option.Facet.FACETS_FILL_DIR to dir,
7276
Option.Facet.FACETS_LABWIDTH to labWidth
7377
).filterNonNullValues()

0 commit comments

Comments
 (0)