In Lets-Plot you can apply a formatting to:
geomText()
.Using format string you can format values of numeric and date-time types.
In addition, you can use a string template.
In string template the value's format string is surrounded by curly braces: "... {.2f} ..."
.
An empty placeholder {}
is also allowed. In this case a default string representation will be shown. This is also applicable to categorical values.
To learn more about formatting templates see: Formatting.
%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 economics = DataFrame.readCSV("https://vincentarelbundock.github.io/Rdatasets/csv/ggplot2/economics.csv")
economics.head()
DataFrame: rowsCount = 5, columnsCount = 7
rownames | date | pce | pop | psavert | uempmed | unemploy |
---|---|---|---|---|---|---|
1 | 1967-07-01 | 506.700000 | 198712.000000 | 12.600000 | 4.500000 | 2944 |
2 | 1967-08-01 | 509.800000 | 198911.000000 | 12.600000 | 4.700000 | 2945 |
3 | 1967-09-01 | 515.600000 | 199113.000000 | 11.900000 | 4.600000 | 2958 |
4 | 1967-10-01 | 512.200000 | 199311.000000 | 12.900000 | 4.900000 | 3143 |
5 | 1967-11-01 | 517.400000 | 199498.000000 | 12.800000 | 4.700000 | 3066 |
import kotlinx.datetime.*
fun LocalDate.toEpochMilliseconds(): Long {
return atStartOfDayIn((TimeZone.currentSystemDefault())).toEpochMilliseconds()
}
val startDate = LocalDate.parse("2000-01-01")
economics = economics.filter { date.compareTo(startDate) >= 0 }.convert {date}.with {it.toEpochMilliseconds()}
val p = letsPlot(economics.toMap()){x="date"; y="uempmed"} +
geomLine() +
ylab("unemployment rate") +
ggsize(900, 400)
Use the format
parameter in scaleXxx()
.
Note that the text in tooltips is now also formatted.
p + scaleXDateTime(format="%b %Y") + scaleYContinuous(format="{} %")
val breaks = listOf(
LocalDate.parse("2001-01-01"),
LocalDate.parse("2016-01-01")
).map {it.toEpochMilliseconds()}
p + scaleXDateTime(format="%b %Y", breaks=breaks) + scaleYContinuous(format="{} %")
letsPlot(economics.toMap()){x="date"; y="uempmed"} +
geomLine(tooltips=layerTooltips()
.line("Unemployment rate:|^y")
.anchor("top_center")
.minWidth(170)) +
scaleXDateTime(format="%b %Y") +
scaleYContinuous(format="{} %") +
ylab("unemployment rate") +
ggsize(900, 400)
letsPlot(economics.toMap()){x="date"; y="uempmed"} +
geomLine(tooltips=layerTooltips()
.line("@uempmed % in @date")
.format("date", "%B %Y")
.anchor("top_left")
.minWidth(170)) +
scaleXDateTime() +
scaleYContinuous() +
ylab("unemployment rate") +
ggsize(900, 400)
The geomText
label is formatted using the labelFormat
parameter.
val unemploymentMean = economics.uempmed.mean()
letsPlot(economics.toMap()){x="date"; y="uempmed"} +
geomLine(tooltips=layerTooltips()
.line("Unemployment rate:|^y %")
.anchor("top_center")
.minWidth(170)) +
geomHLine(yintercept=unemploymentMean, color="red", linetype="dashed", tooltips=tooltipsNone) +
geomText(label=unemploymentMean.toString(),
labelFormat="{.2f} %",
x=startDate.toEpochMilliseconds(), y=unemploymentMean!! + 0.5,
color="red") +
scaleXDateTime(format="%b %Y") +
scaleYContinuous(format="{} %") +
ylab("unemployment rate") +
ggtitle("The US Unemployment Rates 2000-2016.") +
ggsize(900, 400)