4

I am trying to adjust geom_text labels so that they don't overlap. Adjusting the labels using nudge_x, nudge_y and size does not give me a very satisfying result. I came across the adjust_text package but I don't manage to implement it to my code.

Right now my graph looks like that:

Graph

I tried adding adjust_text =True (see below) which gives me the following error. "Parameters {'adjust_text'}, are not understood by either the geom, stat or layer."

(ggplot(nutrient_rel_item, aes(y='Share_calories', x='Share_biomass', color='Type',show_legend=False))
+stat_smooth( aes(y='Share_calories', x='Share_biomass'),method='lm',inherit_aes=False) 
+geom_text(aes(label='Abb'),data=nutrient_rel_item, nudge_x=0.1, nudge_y=0.1, size=4, adjust_text =True)
+facet_wrap('~CCC1',nrow=2)
+ scale_x_log10(labels=lambda l: ["%d%%" % (v * 100) for v in l])
+ scale_y_log10(labels=lambda l: ["%d%%" % (v * 100) for v in l])
+ geom_point(size=0.1) 
+ geom_path(aes(group='Item'), arrow= arrow(angle = 15, length= 0.1, type = "closed"))
+ labs(x='x', y='y')
+ theme(legend_position='none'))
polarpanda
  • 43
  • 5

1 Answers1

4

You need to pass a dictionary with options to adjust text so first define it with the required options such as:

adjust_text_dict = {
    'expand_points': (2, 2),
    'arrowprops': {
        'arrowstyle': '->',
        'color': 'red'
    }
}

and then enable it using your ggplot command as above but change the line

+geom_text(aes(label='Abb'),data=nutrient_rel_item, nudge_x=0.1, nudge_y=0.1, size=4, adjust_text =True)

to

+geom_text(aes(label='Abb'),data=nutrient_rel_item, nudge_x=0.1, nudge_y=0.1, size=4, adjust_text=adjust_text_dict)

Having said that the error message you get indicates that plotnine does not recognize the option itself. Note that you need at least version 0.6.0 to get it to run.

Azrael3000
  • 1,847
  • 12
  • 23