【问题标题】:ggplot dual reversed y axis and geom_hline intercept calculationggplot双反转y轴和geom_hline截距计算
【发布时间】:2018-01-23 02:49:06
【问题描述】:

我在 ggplot2 中创建了一个带有两个 y 轴的折线图,并且只希望将一个数据集(蓝色)绘制在反向轴上,并希望将另一个数据集(红色)绘制在与第一个不同的比例上。但是,我正在使用的代码反转两个轴,虽然第二个 y 轴已被编码为具有不同的比例,但第二个数据集(红色)是使用第一个 y 轴的比例绘制的。此外,我创建了一条线(绿色),我必须确定蓝线在哪里截取它。我知道这个问题的后半部分之前已经被问过并得到了回答,但是在那篇文章中指出该解决方案实际上并不起作用。任何输入都会有所帮助!谢谢!我提供了一个示例数据集,因为我的数据集太大而无法重新创建。

time<-c(1,2,3,4,5,6,7,8,9,10)
height<-c(100,330,410,570,200,270,230,390,400,420)
temp<-c(37,33,14,12,35,34,32,28,26,24)
tempdf<-data.frame(time,height,temp)

makeplot<-ggplot(tempdf,aes(x=time)) + geom_line(aes(y=height),color="Blue") 
    + geom_line(aes(y=temp),color="Red")+
    scale_y_continuous(sec.axis=sec_axis(~./100,name = 
    "Temperature"),trans="reverse")+
    geom_hline(aes(yintercept=250), color="green")

【问题讨论】:

  • 为什么要在反向轴上设置高度?这表明它是深度。
  • 请一次问一个问题。计算两条线之间的交点与绘图本身几乎没有关系。
  • @neilfws 是的,在我的真实数据中我正在处理深度,我只是认为由于某种原因,这个例子更容易讨论高度。
  • @Axeman 我将这些问题集中在一起,看看是否有某种方法可以使用 ggplot 找到截距,因为折线图本身不是函数,因此我无法仅插入数字来计算截距,但是如果无法使用 ggplot 完成此操作,我会将问题分开。
  • 好的。我认为交叉点在ggplot 之外解决得更好。关于轴的另一个问题非常棘手,因为ggplot 没有像您尝试使用它们那样正确支持双轴(仅具有相同比例的线性变换,例如英寸和厘米)。这是设计使然。

标签: r ggplot2


【解决方案1】:

ggplot 只会做 1:1 的轴转换,如果它翻转一个轴,则会同时翻转两个轴,所以你需要找出一个方程来平移你的轴。乘以(或除以)负数会将您的温度轴翻转回标准递增刻度。这两个方程可用于获得相同规模的样本数据。

height = temp*(-10) + 600
temp = (height - 600)/(-10)

现在,您可以将方程式合并到绘图代码中,第一个将温度数据转换为适合高度刻度的数字,第二个将您的次轴数字转换为显示温度的刻度。

makeplot<-ggplot(tempdf,aes(x=time)) + 
  geom_line(aes(y=height),color="blue") + 
  geom_line(aes(y = (temp*(-10)) + 600), color="red")+
  scale_y_continuous(sec.axis=sec_axis(~(.-600)/(-10),name = 
                                     "Temperature"),trans="reverse")+
  geom_hline(aes(yintercept=250), color="green")
makeplot

【讨论】:

    【解决方案2】:

    暂时忽略线的交点问题,这里有几个双轴的替代方案。首先,方面:

    library(tidyverse)
    library(scales)
    tempdf %>% 
      # convert height to depth
      mutate(height = -height) %>% 
      rename(depth = height) %>% 
      gather(key, value, -time) %>% 
      ggplot(aes(time, value)) + 
        geom_line() + 
        facet_grid(key ~ ., scales = "free_y") + 
        scale_x_continuous(breaks = pretty_breaks()) +
        theme_bw()
    

    其次,用彩色点表示每个深度的温度:

    tempdf %>% 
      mutate(height = -height) %>% 
      rename(depth = height) %>% 
      ggplot(aes(time, depth)) + 
        geom_line() + 
        geom_point(aes(color = temp), size = 3) + 
        geom_hline(yintercept = -250, color = "blue") +
        scale_color_gradient2(midpoint = 25, 
                              low = "blue", 
                              mid = "yellow", 
                              high = "red") + 
        scale_x_continuous(breaks = pretty_breaks()) + 
        theme_dark()
    

    【讨论】:

      【解决方案3】:

      另一种选择是路径图:

      ggplot(tempdf, aes(height, temp)) + 
        geom_path() + 
        geom_point(aes(fill = time), size = 8, shape = 21) + 
        geom_text(aes(label = time)) + 
        viridis::scale_fill_viridis()
      

      【讨论】:

      • 非常感谢,这些都很棒,肯定会对我以后的分析有所帮助!
      猜你喜欢
      • 2020-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多