首页 文章

ggplot2辅助轴奇怪的输出

提问于
浏览
1

我正在尝试两个用ggplot2制作一个双y轴图 . 但是,主y轴文本值已更改(和限制),其中一个变量显示错误("mean" variable) . Edit: "mean"变量的文本标签范围从0.55到0.75,很难看出可变性 . 但是,在该图的原始步骤(p < - p geom_line(aes(y = mean_d,color = "mean"))geom_point(aes(y = mean_d,color = "mean")))中,范围从0.7757到0.7744 . 它应该显示为原始步骤(也许它必须与ggplot调用中的数据操作?)此外,是否可以协调axis-y1文本与轴-y2文本显示在相同的水平线?

# dput(coeff.mean)
coeff.mean <- structure(list(individuals = c(5L, 18L, 31L, 43L, 56L, 69L, 82L, 
95L, 108L, 120L, 133L, 146L, 159L, 172L, 185L, 197L, 210L, 223L, 
236L, 249L, 262L, 274L, 287L, 300L, 313L, 326L, 339L, 351L, 364L, 
377L), mean_d = c(0.775414405190575, 0.774478867355839, 0.774632679560057, 
0.774612015422181, 0.774440717600404, 0.774503749029999, 0.774543337328481, 
0.774536584528457, 0.774518615875444, 0.774572944896752, 0.774553554507719, 
0.774526346948343, 0.774537645238366, 0.774549039219398, 0.774518593880137, 
0.77452848368359, 0.774502654364311, 0.774527249259969, 0.774551190425812, 
0.774524221826879, 0.774514765537317, 0.774541221078135, 0.774552621147008, 
0.774546365564095, 0.774540310535789, 0.774540468208943, 0.774548658706833, 
0.77454534219406, 0.774541081476004, 0.774541996470423), var_d = c(0.000438374265308954, 
0.000345714068446388, 0.000324909665783972, 0.000318897997146887, 
0.000316077108040133, 0.000314032075708385, 0.000310447758209298, 
0.000310325171003455, 0.000311927176741998, 0.000309622062319051, 
0.000308772480851544, 0.000308388263293765, 0.000306838067001956, 
0.000307838047303517, 0.000307737478217495, 0.000306351076037266, 
0.000307288393036824, 0.000306717640522594, 0.000306768886331324, 
0.000306897320278579, 0.000307154374510682, 0.000306352361061403, 
0.000306998606721366, 0.000306434828650204, 0.000305865398401208, 
0.000306061994682725, 0.000305934443005304, 0.000305853730364841, 
0.000306181262913308, 0.000306820996289535)), .Names = c("individuals", 
"mean_d", "var_d"), row.names = c(NA, -30L), class = c("tbl_df", 
"tbl", "data.frame"))

p <- ggplot(coeff.mean, aes(x=individuals))
p <- p + geom_line(aes(y = mean_d, colour = "mean")) + geom_point(aes(y = mean_d, colour = "mean"))
p <- p + geom_line(aes(y = var_d*(max(mean_d)/max(var_d)), colour = "var")) + geom_point(aes(y = var_d*(max(mean_d)/max(var_d)), colour = "var")) 
p <- p + scale_y_continuous(sec.axis = sec_axis(~.*(max(coeff.mean$var_d)/max(coeff.mean$mean_d)), name = "var"))
p <- p + scale_colour_manual(values = c("black", "grey"))
p <- p + labs(y = "mean", x = "Resampled", colour = "Statistic")
print(p)

我很感激任何建议 .

enter image description here

2 回答

  • 2

    在这里,我展示了使用facet作为双轴绘图的替代方案 . 我知道它没有回答原来的问题,对不起!

    library(ggplot2)
    library(tidyr)
    
    # Convert data to long form with tidyr::gather()
    long_dat = gather(data=coeff.mean, key="stat", value="stat_value", mean_d, var_d)
    
    head(long_dat)
    # A tibble: 6 x 3
    #   individuals   stat stat_value
    #         <int>  <chr>      <dbl>
    # 1           5 mean_d  0.7754144
    # 2          18 mean_d  0.7744789
    # 3          31 mean_d  0.7746327
    # 4          43 mean_d  0.7746120
    # 5          56 mean_d  0.7744407
    # 6          69 mean_d  0.7745037
    
    p2 = ggplot(long_dat, aes(x=individuals, y=stat_value, colour=stat)) + 
         geom_point() + 
         geom_line() + 
         scale_colour_manual(values=c(mean_d="black", var_d="grey40")) +
         facet_grid(stat ~ ., scales="free_y")
    
    ggsave("faceted_plot.png", plot=p2, height=4, width=6, dpi=150)
    

    enter image description here

  • 2

    这更清楚地显示了我的评论指出的内容:您不需要乘法缩放 var_d ,您需要添加它 .

    library(dplyr)
    
    coeff.mean %>% 
      ggplot(aes(individuals, mean_d)) +
      geom_point(aes(color = "mean_d")) + geom_line(aes(color = "mean_d")) +
      geom_point(aes(individuals, var_d+0.7745, color = "var_d")) + 
      geom_line(aes(individuals, var_d+0.7745, color = "var_d")) +
      scale_y_continuous(sec.axis = sec_axis(trans = ~ . - 0.7745))
    

    enter image description here

    当然,由于各种原因,这个数字存在问题 . 很难解释肯定 .

    如果您想要乘法和加法地缩放,可以尝试 scales::rescale ,一次将 var_d 缩放到 mean_d 的范围,然后再次将缩放的 var_d 缩放回原始范围 .

    coeff.mean %>% 
      mutate(var_rescaled = scales::rescale(var_d, to = range(mean_d))) %>% 
      ggplot(aes(individuals, mean_d)) +
      geom_point(aes(color = "mean_d")) + geom_line(aes(color = "mean_d")) +
      geom_point(aes(y = var_rescaled, color = "var_d")) + 
      geom_line(aes(y = var_rescaled, color = "var_d")) +
      scale_y_continuous(sec.axis = 
        sec_axis(trans = ~scales::rescale(., to = range(coeff.mean$var_d)),
                 breaks = function(values) {scales::pretty_breaks(n=5)(values)},
                 name = "var_d"))
    

    enter image description here

    这个也有问题 . 特别是,由于 mean_dvar_d 的最高值同时为 individual ,因此它们在该点重叠 .

相关问题