【问题标题】:Trying to plot data from two different datasets with two axes尝试用两个轴绘制来自两个不同数据集的数据
【发布时间】:2020-10-27 01:31:07
【问题描述】:

所以我在以下两个df中使用了化学和降水数据:

    chem_df
    rain_df

我使用 ggplot() 绘制了两个数据集,为了获得 2 个轴,使用了 scale_y_continuous 的 sex.axis 函数,如下所示:

    chem_rain_fig <- ggplot() +
    geom_point(data = chem_df, aes(x = Date, y = Temp)) +
    geom_line(data = rain_df, aes(x = Date, y = Rain)) + 
    scale_y_continuous(name = "Temp", sec.axis = (.~*, name = "Rain"))

但它不断将两个数据集绘制到原始 y 轴,如下所示: Graph with Issue 我只想指出,降雨数据在 0-10 厘米之间,这就是为什么它遵循第一个轴而不是第二个轴,限制为(0,10)

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    This might answer your question

    本质上,您必须手动转换数据并进行缩放以使其大小合适。没有样本数据就无法尝试,但这应该可以,乘以 2 并除以比例:

        chem_rain_fig <- ggplot() +
        geom_point(data = chem_df, aes(x = Date, y = Temp)) +
        geom_line(data = rain_df, aes(x = Date, y = Rain*2)) + 
        scale_y_continuous(name = "Temp", sec.axis = sec_axis(~./2, name = "Rain"))
    

    【讨论】:

    • 谢谢!我会试试看。
    猜你喜欢
    • 2014-02-08
    • 2022-01-02
    • 1970-01-01
    • 2016-10-27
    • 2020-11-12
    • 2019-01-17
    • 1970-01-01
    • 2020-09-25
    • 1970-01-01
    相关资源
    最近更新 更多