首页 > 解决方案 > 使用 scale_y_log10 函数后如何将点作为辅助轴?

问题描述

我想将条形图作为主轴,将点图(百分比值)作为辅助轴。请找到以下数据示例:

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"))

我试图绘制条形图和点图并将它们合并在一起。这是我使用的代码:

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})))

但是,当我想通过使用scale_y_continuous()函数将点图作为具有不同比例的辅助轴时,会导致错误,因为我已经使用scale_y_log10()函数来更改主轴的比例。在这种情况下如何将点图作为次轴?

是否有任何解决方案而不是使用scale_y_continuous()函数?

标签: rggplot2yaxis

解决方案


推荐阅读