1

I want to produce a bar plot as primary axis and point plot (percentage value) as secondary axis. Please find the example of data as below:

df <- structure(list(Species = c("PM10", "Na", "NH4", "K", "Mg", "Cl", 
"NO3", "Oxalate", "MSA", "ssCa", "nssCa", "ssSO4", "nssSO4", 
"Al", "Ti", "V", "Cr", "Mn", "Fe", "Ni", "Cu", "As", "Cd", "Ba", 
"La", "Ce", "Pb"), Species_order = c(1, 2, 3, 4, 5, 6, 7, 8, 
9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 
25, 26, 27), Secondary_conc = c(411.18, 3.432, 17.552, 2.4972, 
0.64528, 0, 18.207, 3.2252, 0, 0.023608, 3.3337, 0.13273, 54.707, 
0.7192, 0.029855, 1.7612e-05, 0, 0.024012, 1.5637, 0, 0.026486, 
0, 0, 0, 1.1561e-06, 4.6873e-06, 6.0361e-06), Secondary_perc = c(16.35872, 
1.506537, 33.72603, 19.34941, 2.373839, 0, 68.52145, 88.5679, 
0, 0.2825614, 34.85788, 0.241084, 26.63269, 7.877051, 7.609215, 
0.09306203, 0, 12.60384, 12.5355, 0, 34.27987, 0, 0, 0, 0.01647077, 
0.03159252, 0.005472207)), row.names = c(NA, -27L), class = c("tbl_df", 
"tbl", "data.frame"))

I have tried to draw bar and point plot and merge them together. Here is the code I used:

ggplot(df) + 
  geom_bar(aes(x=reorder(Species,Species_order), y=Secondary_conc),
           stat = 'identity',fill="cadetblue2") +
  scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),
                labels = trans_format("log10", math_format(10^.x))) + 
  geom_point(aes(x=reorder(Species,Species_order), y=Secondary_perc, color="red")) + 
  theme(axis.title.y = element_text (face="bold", size=8),
        axis.text.x = element_text(face = "bold",size = 8, angle = -45),
        legend.position = "none") + 
  labs(x=NULL,y=expression(bold(ng/m^{3})))

However, when I want to make point plot as secondary axis with different scale by using scale_y_continuous() function, it result error as I already use scale_y_log10() function to change the scale of primary axis. How to make point plot as secondary axis in such condition?

Is there any solution instead of using scale_y_continuous() function?

Vinícius Félix
  • 8,448
  • 6
  • 16
  • 32
AgungGK
  • 85
  • 7
  • Do you mean points are not in log scale? – Park Sep 30 '21 at 05:51
  • Maybe try using [sec_axis](https://ggplot2.tidyverse.org/reference/sec_axis.html) inside scale_y_log10? – zx8754 Sep 30 '21 at 07:34
  • Yes, I don't want to use log scale for second axis. The second axis should in percentage unit without have to convert it into log scale. – AgungGK Sep 30 '21 at 12:37
  • Plotting 2 independent variables on the same axis is something against ggplot2's design. However, you could scale/shift your secondary axis to 'fit' in the primary one. Check this thread for example https://stackoverflow.com/q/3099219/14027775. – marcguery Sep 30 '21 at 15:04
  • @marcguery Thanks for the link, I have take a quick look and it seems useful. I will read more details to that thread. – AgungGK Oct 01 '21 at 10:14
  • 1
    @zx8754 Not sure, I never try it. Will try to learn this too. – AgungGK Oct 01 '21 at 10:15

0 Answers0