[R] ggplot adjust two y-axis

CALUM POLWART po|c1410 @end|ng |rom gm@||@com
Sat Nov 25 14:22:41 CET 2023


This is doable. But it's also considered a data visualisation hell! So
Hadley didn't make it easy.

I can provide more details on how to do it later (replying from phone just
now). BUT there is very much a question of should you be plotting like
that. It's probably worth giving it serious thought before figuring out how.


The brief summary of 'how' is to convert the y2 axis values to the y axis
values. Plot it, and add a y2 axis.

So if you were plotting 3 values on y1 with values of 20, 30 and 40. And
the y2 axis should have 150, 350 and 550 for example  then you decide what
150 should be on y1 (is that 20 or perhaps 15??) {This choice changes your
visualisation and hence one of the reasons it can be bad}. If 150 should
appear as 20 you divide all your y2 values by the same proportion (150/20)
and plot them. Then simply add a second y-axis with the scale reversed.





On Sat, 25 Nov 2023, 09:30 , <sibylle.stoeckli using gmx.ch> wrote:

> Dear Charles-Edouard
>
> Thanks a lot.
> So no way in R to just simply have one ggplot with to axis as in Excel
> (attachment)?
>
> Kind regards
> Sibylle
>
> -----Original Message-----
> From: Charles-Édouard Giguère <ce.giguere using gmail.com>
> Sent: Friday, November 24, 2023 3:14 PM
> To: sibylle.stoeckli using gmx.ch; r-help using r-project.org
> Subject: RE: [R] ggplot adjust two y-axis
>
> You could also use more simply facet_wrap(~ Studien_Flaeche).
> Charles-Édouard
>
> -----Message d'origine-----
> De : Charles-Édouard Giguère <ce.giguere using gmail.com> Envoyé : 24 novembre
> 2023 09:11 À : sibylle.stoeckli using gmx.ch; r-help using r-project.org Objet : RE:
> [R]
> ggplot adjust two y-axis
>
> Hi Sibylle,
> For that kind of data with two different scales, I generally use two graphs
> that I name gg1 and gg2 and join them using gridExtra::grid.arrange(gg1,
> gg2). This way, the red part of your graph is easier to interpret.
> Have a nice day,
> Charles-Édouard
>
> -----Message d'origine-----
> De : R-help <r-help-bounces using r-project.org> De la part de
> sibylle.stoeckli using gmx.ch Envoyé : 24 novembre 2023 05:52 À :
> r-help using r-project.org Objet : [R] ggplot adjust two y-axis
>
> Dear R-users
>
> Is it possible to adjust two y-axis in a ggplot differently?
> - First y axis (0-60)
> - Second y axis (0-2500)
>
>
> ### Figure 1
> ggplot(Fig1,aes(BFF,Wert,fill=Studien_Flaeche))+
>   geom_bar(stat="identity",position='dodge')+
>   scale_y_continuous(name="First Axis", sec.axis=sec_axis(trans=~.*50,
> name="Second Axis"))+
>   scale_fill_brewer(palette="Set1")
>
> Thanks a lot
> Sibylle
>
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list