JetBrains / lets-plot-kotlin

Grammar of Graphics for Kotlin
https://lets-plot.org/kotlin/
MIT License
430 stars 36 forks source link

Add possibility to create a pie chart, a bar chart with horizontal bars #68

Closed achifal closed 1 year ago

achifal commented 3 years ago

It would be nice to have pie chart and bar chart with horizontal bars support

alshan commented 3 years ago

Yes, we are planning to have horizontal bars (and some other directional geometries).

As for pie char, this more difficult, but we are planning to have "waffle plot" which can work as a replacement for pie chart.

achifal commented 3 years ago

I would like to ask a couple of questions 1) It would be interesting to know where you see the difficulty of adding a pie chart. I have some sort of workaround. I understand that if I save this to svg then the image size will be quite large. Can you please describe other disadvantages of my approach or maybe you can suggest your own image

achifal commented 3 years ago

2) The second question is a bit off-topic, but still I'm interested in how to do it. Suppose I want to make a chart like this. I managed to do something similar using facet. Is it possible to achieve this effect without using facets? Is it possible to somehow control the distance between bars? image image Thanks in advance for your answers!

alshan commented 3 years ago

On the 2nd, try:

geomBar(stat..., position = Pos.dodge) { x = "category"; ...
alshan commented 3 years ago

In Lets-Plot everything boils down to points in some coordinate system. To implement idiomatic Pie Chart we would have to use polar coordinate system which is not yet supported. Adding support for polar coordinate system is difficult (but also very cool).

But you are right: noneidiomatic solution for pie chart can be done with lesser efforts. Your solution is original but a bit extreme. Could you try to build sectors using geomPolygon?

achifal commented 3 years ago

Yay, this is a cleaner way to do this. Thanks a lot for your answers and help! image

alshan commented 3 years ago

Very cool ) Do you order slices? I've read somewhere that pie chart looks the best when the 1st biggest slice goes to the left from 12 o'clock and all other slices go clockwise in descending order:

image

achifal commented 3 years ago

Good concept). Now I put the slices in descending order except the last slice. The last slice is the sum of all the remaining values to ensure that only the largest X number of slices are displayed, and the rest are combined image

achifal commented 3 years ago

In mathematics, it is possible to move from one coordinate system to another. Converting between polar and Cartesian coordinates looks like this image Let's say we have a semicircle. Here is an example of converting it to polar coordinates polar It seems like a pie chart can be built using this conversion. And when the polar coordinate system is supported, it will be easy to switch to using it. Maybe it is possible to do such an implementation for now to avoid waiting for polar coordinates support. What do you think?)

alshan commented 3 years ago

Would be perfect if the biggest slice was to the left of 12 o'clock ) : image

alshan commented 3 years ago

In mathematics...

Exactly, this is how polar coordinate system should project X, Y etc. aesthetics to the display space. X-axis becomes a circle, bars are translated to slices and all other geometries are distorted accordingly.
https://ggplot2.tidyverse.org/reference/coord_polar.html

achifal commented 2 years ago

@alshan Thanks a lot for adding coordFlip! A bar chart with horizontal bars is now possible 🎉

alshan commented 2 years ago

@KirylBubovich You are very welcome ) I remember about coordPolar but couldn't find a resource to work on it unfortunately.

achifal commented 2 years ago

No worries, thank you for being transparent. I appreciate your work and time)

alshan commented 1 year ago

geomPie added in v4.2.0

achifal commented 1 year ago

@alshan Thank you so much!