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 lets-plot
//%use krangl
LetsPlot.getInfo() // This prevents Krangl from loading an obsolete version of Lets-Plot classes.
Lets-Plot Kotlin API v.4.1.1. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.2.5.1.
%use krangl
var economics = DataFrame.readCSV("https://vincentarelbundock.github.io/Rdatasets/csv/ggplot2/economics.csv")
import java.text.SimpleDateFormat
val yyyyMmDd = SimpleDateFormat("yyyy-MM-dd")
economics = economics.addColumn("date") { it["date"].map<String> {yyyyMmDd.parse(it)}}
economics.head(3)
date | pce | pop | psavert | uempmed | unemploy | |
---|---|---|---|---|---|---|
1 | Sat Jul 01 00:00:00 EDT 1967 | 506.7 | 198712 | 12.6 | 4.5 | 2944 |
2 | Tue Aug 01 00:00:00 EDT 1967 | 509.8 | 198911 | 12.6 | 4.7 | 2945 |
3 | Fri Sep 01 00:00:00 EDT 1967 | 515.6 | 199113 | 11.9 | 4.6 | 2958 |
Shape: 3 x 7.
import java.util.Date
val startDate = yyyyMmDd.parse("2000-01-01")
economics = economics.filterByRow { (it["date"] as Date).compareTo(startDate) >= 0 }
val p = letsPlot(economics.toMap()){x="date"; y="uempmed"} +
geomLine() +
ylab("unemployment rate") +
ggsize(900, 400)
p + scaleXDateTime()
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(
yyyyMmDd.parse("2001-01-01"),
yyyyMmDd.parse("2016-01-01")
)
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.toInstant().toEpochMilli(), 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)