%useLatestDescriptors
%use dataframe
%use lets-plot
val shift = 15.0
val targetCities = listOf("Barcelona", "New York City", "Lyon", "Paris", "Amsterdam", "Berlin")
val angles = mapOf(
"Jan" to -shift, "Feb" to -30 - shift, "Mar" to -60 - shift, "Apr" to 90 - shift, "May" to 60 - shift, "Jun" to 30 - shift,
"Jul" to -shift, "Aug" to -30 - shift, "Sep" to -60 - shift, "Oct" to 90 - shift, "Nov" to 60 - shift, "Dec" to 30 - shift,
)
val df = DataFrame.readCSV("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/sunshine_hours.csv")
.filter { "City"<String>() in targetCities }
.gather("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
.into("Month", "Sunshine hours")
.add("Angle") { angles["Month"<String>()] }
.update { "Month"<String>() }.with { it.uppercase() }
.add("Id") { targetCities.indexOf("City"<String>()) }.sortBy("Id").remove("Id")
val dataMap = df.toMap()
df.head(3)
DataFrame: rowsCount = 3, columnsCount = 6
Country | City | Year | Month | Sunshine hours | Angle |
---|---|---|---|---|---|
Spain | Barcelona | 2591.000000 | JAN | 158.000000 | -15.000000 |
Spain | Barcelona | 2591.000000 | FEB | 171.000000 | -45.000000 |
Spain | Barcelona | 2591.000000 | MAR | 206.000000 | -75.000000 |
val fontFamily = "ParaType"
val fontFace = "bold"
val backgroundColor = "#fcf3e4"
val gridColor = "#ccc2a2"
val yBreaks = listOf(100.0, 200.0)
val yBreaksData = mapOf(
"x" to List(yBreaks.size) { "JAN" },
"y" to yBreaks,
"text" to yBreaks.map(Any::toString),
)
letsPlot(dataMap) { x = asDiscrete("Month", levels = angles.keys.toList()); y = "Sunshine hours" } +
geomText(data = yBreaksData, size = 6, vjust = 0,
angle = -shift, family = fontFamily, fontface = fontFace)
{ x = "x"; y = "y"; label = "text" } +
geomBar(stat = Stat.identity, color = "black", fill = "#fbc117",
tooltips = layerTooltips().title("@City").line("@Month: @{Sunshine hours}")) +
geomLabel(y = 330, family = fontFamily, fontface = fontFace, size = 8,
labelPadding = 0, labelR = 0, labelSize = 0, fill = backgroundColor)
{ x = "Month"; label = "Month"; angle = "Angle" } +
facetWrap(facets = "City", order = null) +
coordPolar(start = PI * shift / 180) +
labs(title = "Comparing Sunshine Hours",
caption = "Sunshine duration is expressed in (average) hours per month\n" +
"Data source: " +
"<a href=\"https://www.kaggle.com/datasets/bilalwaseer/sunshine-hours-for-cities-around-the-world\">" +
"sunshine hours for cities around the world" +
"</a>\n" +
"Original plot: " +
"<a href=\"https://www.linkedin.com/feed/update/urn:li:activity:7186216546602524672/\">" +
"Giulia Mezzadri, PhD's Post" +
"</a>") +
ggsize(1000, 850) +
theme(axis = "blank",
plotTitle = elementText(size = 32, family = fontFamily, face = fontFace, margin = listOf(20, 20, 30)),
plotCaption = elementText(family = fontFamily, face = fontFace),
stripText = elementText(size = 20, family = fontFamily, face = fontFace),
plotBackground = elementRect(fill = backgroundColor),
panelGrid = elementLine(color = gridColor, size = 2))