ggsave (.svg) transforms geom_text integer to float
Problem:
I have a (horizontal) stacked and grouped bar chart with the values displayed as text.
p = (ggplot(df_modalsplit_long) + geom_bar(aes(x='Scenario', y='Modal Split', fill='Mode'), position='stack', stat='identity') + facet_grid(y='Model', y_order=0) + theme(legend_position='top')
+ scale_x_discrete_reversed() + ggsize(850, 1000)
+ geom_text(aes(x='Scenario', y='Modal Split', group='Mode', label='Modal Split'), position= position_stack(vjust=0.5))
+ coord_flip())
p.show()
'Modal Split' are my values of type integer.
Displaying the plot with p.show() shows the plot with correct integer values like 35 and 11.
Saving the same plot with ggsave(p, filename='filename.svg', path='.') as an svg file transforms the displayed modal split values to floats with one digit after the decimal point like 35.0 and 11.0.
Saving the same plot as html ggsave(p, filename='filename.html', path='.') is again perfectly fine with pure integer values.
Solution (Workaround):
Add label_format=".0f" to geom_text()
Right now I am not sure if this a weird special case due to faceting, stacking or flipping or if it is common for geom_text.
Tested with the current version 2.5.1
This likely happens because in absence of an explicit formatting pattern we simply fall back on a default number formatting which is platform dependent.
In case of p.show() and ggsave(html) the platform is a JavaScript engine, but in case of ggsave(svg) the platform is your OS.
Apparently, they behave slightly differently.
Probably, a good solution for us would be to guess a good formatting pattern for geom_text() by analysing the data (similar to how it works in tooltips and on axis).
As least the result would be consistent across platforms.
Fixed via https://github.com/JetBrains/lets-plot/commit/d2c896d8fd34df500c07bfe5939e4d29779d0092