首页 > 解决方案 > R:使用 scale_y_continuous(sec.axis) 和 coord_trans(log10) 绘制两个 y 轴图

问题描述

我正在尝试绘制一个 2y 轴图;左边是实际值,右边是百分比值。除此之外,我需要在左侧 y 轴上应用 coord_trans 以便更好地可视化小值。但是,当我这样做时,右侧的标签不会出现。

这里的数据(示例

这里的代码

DAXIS <- ggplot(x1, aes(hour, value_T, colour=season, linetype = variable)) +
  geom_line(size = 1) +
  scale_linetype_manual(c("var"), values=c("solid", "dashed", "dotted"))+ # here to change one name
  geom_point(aes(shape = season), size = 1)+
  labs(x = "hour", y = "T") +
  scale_x_continuous(breaks = c(0, 6, 12, 18, 23), labels= c(0, 6, 12, 18, 24))+
  scale_y_continuous("T",
                 sec.axis = sec_axis(~./2.341598, name = "     [%] ", 
                                     breaks=c(0.2135294, 0.4270588, 0.6405882, 0.8541176,1),
                                     labels = function(b) { paste0(round(b * 100, 0), "%")}))+
  #coord_trans(y = "log10", breaks=c(0.5,1,1.5,1.903738), labels = c(0.5,1,1.5,1.9))+ # attemp 1
  #coord_trans(y = "log10")+  # attemp 2
  scale_color_aaas()+
  theme_bw()+
  theme(legend.direction = "horizontal", legend.position = "bottom", legend.key = element_blank(), 
    legend.background = element_rect(fill = "white", colour = "gray30")) +
  theme(legend.position="bottom", 
    text=element_text(size=18), 
    axis.text.x = element_text(size=15),
    axis.text.y = element_text(size=15))
DAXIS

这里没有 coord_trans 的输出 没有 coord_trans

这里是 coord_trans 的输出 与 coord_trans

很感谢任何形式的帮助

标签: rggplot2dplyr

解决方案


推荐阅读