lets-plot-kotlin icon indicating copy to clipboard operation
lets-plot-kotlin copied to clipboard

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

Open achifal opened this issue 4 years ago • 14 comments

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

achifal avatar Apr 21 '21 12:04 achifal

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.

alshan avatar Apr 21 '21 21:04 alshan

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 avatar Apr 22 '21 18:04 achifal

  1. 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!

achifal avatar Apr 22 '21 18:04 achifal

On the 2nd, try:

geomBar(stat..., position = Pos.dodge) { x = "category"; ...

alshan avatar Apr 23 '21 01:04 alshan

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?

alshan avatar Apr 23 '21 20:04 alshan

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

achifal avatar Apr 25 '21 16:04 achifal

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

alshan avatar Apr 27 '21 00:04 alshan

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 avatar Apr 28 '21 20:04 achifal

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?)

achifal avatar Apr 28 '21 21:04 achifal

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

alshan avatar Apr 29 '21 21:04 alshan

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

alshan avatar Apr 29 '21 21:04 alshan

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

achifal avatar Nov 07 '21 09:11 achifal

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

alshan avatar Nov 07 '21 23:11 alshan

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

achifal avatar Nov 08 '21 05:11 achifal

geomPie added in v4.2.0

alshan avatar Dec 30 '22 20:12 alshan

@alshan Thank you so much!

achifal avatar Jan 01 '23 18:01 achifal