Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ggsave (.svg) transforms geom_text integer to float #626

Closed
schakalakka opened this issue Nov 3, 2022 · 2 comments
Closed

ggsave (.svg) transforms geom_text integer to float #626

schakalakka opened this issue Nov 3, 2022 · 2 comments
Labels

Comments

@schakalakka
Copy link

schakalakka commented Nov 3, 2022

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

@alshan
Copy link
Collaborator

alshan commented Nov 5, 2022

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.

@alshan alshan added this to the 2023Q1 milestone Dec 28, 2022
@alshan alshan modified the milestones: 2023Q1, 2023Q2 Mar 30, 2023
@alshan alshan modified the milestones: 2023Q2, 2023Q3 Jun 28, 2023
@alshan alshan modified the milestones: 2023Q3, 2023Q4 Sep 29, 2023
@alshan alshan modified the milestones: 2023Q4, 2024Q1 Dec 29, 2023
@alshan alshan modified the milestones: 2024Q1, 2024Q2 Mar 29, 2024
@alshan alshan added the * label Apr 13, 2024
@alshan alshan modified the milestones: 2024Q2, 2024Q3 Jun 30, 2024
@IKupriyanov-HORIS
Copy link
Collaborator

Fixed via d2c896d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants