You could also use more simply facet_wrap(~ Studien_Flaeche). Charles-?douard -----Message d'origine----- De?: Charles-?douard Gigu?re <ce.giguere at gmail.com> Envoy??: 24 novembre 2023 09:11 ??: sibylle.stoeckli at gmx.ch; r-help at 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 at r-project.org> De la part de sibylle.stoeckli at gmx.ch Envoy??: 24 novembre 2023 05:52 ??: r-help at 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
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 at gmail.com> Sent: Friday, November 24, 2023 3:14 PM To: sibylle.stoeckli at gmx.ch; r-help at 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 at gmail.com> Envoy??: 24 novembre 2023 09:11 ??: sibylle.stoeckli at gmx.ch; r-help at 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 at r-project.org> De la part de sibylle.stoeckli at gmx.ch Envoy??: 24 novembre 2023 05:52 ??: r-help at 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 -------------- next part -------------- A non-text attachment was scrubbed... Name: Picture1.pdf Type: application/pdf Size: 166974 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20231124/e46f62df/attachment.pdf>
Hi, I don't know the axis mecanism well enough in ggplot but using the original barplot function you can add an axis on the right using the axis function. Here is an example: test <- as.table(matrix(c(2,10,3,11), 2,2)) barplot(test, beside = TRUE, col = scales::brewer_pal(palette = 1)(2)) axis(4, at = c(0, 5, 10), labels = c(0,50,100)) -----Message d'origine----- De?: sibylle.stoeckli at gmx.ch <sibylle.stoeckli at gmx.ch> Envoy??: 24 novembre 2023 09:27 ??: 'Charles-?douard Gigu?re' <ce.giguere at gmail.com>; r-help at r-project.org Objet?: RE: [R] ggplot adjust two y-axis 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 at gmail.com> Sent: Friday, November 24, 2023 3:14 PM To: sibylle.stoeckli at gmx.ch; r-help at 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 at gmail.com> Envoy??: 24 novembre 2023 09:11 ??: sibylle.stoeckli at gmx.ch; r-help at 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 at r-project.org> De la part de sibylle.stoeckli at gmx.ch Envoy??: 24 novembre 2023 05:52 ??: r-help at 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