【问题标题】:Vertical profile in r plot()r plot()中的垂直轮廓
【发布时间】:2017-01-19 11:24:24
【问题描述】:

我正在构建水柱的垂直剖面图。我的问题是这些点连接在 x 观察上,而不是 y 观察上。在 ggplot 下,我知道 geom_path 可以做到这一点,但我不能使用 ggplot,因为我想添加几个 x 轴。因此我使用情节()。 所以这是我尝试过的:

Storfjorden <- read.delim("C:/Users/carvi/Desktop/Storfjorden.txt")
smooF=smooth.spline(Storfjorden$Fluorescence,Storfjorden$Depth,spar=0.50)
plot(Storfjorden$Fluorescence,Storfjorden$Depth,ylim=c(80,0),type="n")
lines(smooF)

Resulting plot

如您所见,这些点通过 x 个观测值连接起来。但是要观察垂直剖面,我希望通过 y 观察看到它们之间的联系。我尝试按深度对它们进行排序(使用 order()),但并不影响结果。有人有线索吗?

作为替代方案,如果有人知道如何在单个图上绘制具有不同轴的不同线(温度、盐度、荧光),那么我可以使用 geom_path ()。谢谢!

**我有一个你可能会回答的新问题,在 ggplot 中有没有办法制作 geom_smooth(),但是观察结果是按照它们出现的顺序而不是 x 轴连接的?

ggplot(melteddf,aes(y=Depth,x=value))+geom_path()+facet_wrap
+(~variable,nrow=1,scales="free‌​_x")+scale_y_reverse‌​()
+geom_smooth(span=‌​0.5,se=FALSE) 

我尝试使用 smooth.spline,但无法识别 geom_path 中的对象。谢谢!

【问题讨论】:

    标签: plot ggplot2


    【解决方案1】:

    ggplot2 使在单个绘图上绘制多个 x 轴变得困难是有原因的——它通常会导致难以阅读(或更糟的是,误导性的)图表。如果您有一个激励示例说明您的示例为什么属于这些类别之一,它可能会让我们帮助您了解更多细节。不过,下面是两种可能会有所帮助的解决方法。

    这里有一个快速 MWE 来解决这个问题——如果你给我们一些看起来像你的实际数据的东西可能会更有帮助,但这至少可以得到非常不同的规模(尽管没有结构,图比较乱)。

    请注意,我使用dplyr 进行多次操作,并使用reshape2melt 将数据转换为长格式以便于绘图。

    library(dplyr)
    library(reshape2)
    
    df <-
      data.frame(
        depth = runif(20, 0, 100) %>% round %>% sort
        , measureA = rnorm(20, 10, 3)  
        , measureB = rnorm(20, 50, 10)
        , measureC = rnorm(20, 1000, 30)
      )
    
    
    meltedDF <-
      df %>%
      melt(id.vars = "depth")
    

    第一种选择是简单地使用构面将数据彼此相邻绘制:

    meltedDF %>%
      ggplot(aes(y = depth
                 , x = value)) +
      geom_path() +
      facet_wrap(~variable
                 , nrow = 1
                 , scales = "free_x") +
      scale_y_reverse()
    

    第二个是标准化数据,然后绘制它。在这里,我使用的是 z 分数,但如果您有理由使用其他东西(例如,缩放到以您正在使用的任何变量的“适当”数量为中心),您可以更改该公式:

    meltedDF %>%
      group_by(variable) %>%
      mutate(Standardized = (value - mean(value)) / sd(value)  ) %>%
      ggplot(aes(y = depth
                 , x = Standardized
                 , col = variable)) +
      geom_path() +
      scale_y_reverse()
    

    如果您需要绘制多个站点,这里有一些站点示例数据:

    df <-
      data.frame(
        depth = runif(60, 0, 100) %>% round %>% sort
        , measureA = rnorm(60, 10, 3)  
        , measureB = rnorm(60, 50, 10)
        , measureC = rnorm(60, 1000, 30)
        , site = sample(LETTERS[1:3], 60, TRUE)
      )
    
    
    meltedDF <-
      df %>%
      melt(id.vars = c("site", "depth"))
    

    您可以使用facet_grid(我的偏好):

    meltedDF %>%
      ggplot(aes(y = depth
                 , x = value)) +
      geom_path() +
      facet_grid(site~variable
                 , scales = "free_x") +
      scale_y_reverse()
    

    或在标准化图中添加facet_wrap

    meltedDF %>%
      ggplot(aes(y = depth
                 , x = value)) +
      geom_path() +
      facet_grid(site~variable
                 , scales = "free_x") +
      scale_y_reverse()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-30
      • 1970-01-01
      • 1970-01-01
      • 2023-02-14
      • 2018-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多