【问题标题】:Trying to recreate plot with tresholds in ggplot尝试在 ggplot 中创建带有阈值的图
【发布时间】:2020-01-25 21:47:57
【问题描述】:

所以基本上我试图在 ggplot 中重新创建这个情节,以匹配我的主题:

我已经非常接近了:

但我无法在我的情节中重新创建门槛。我怎么可能把它添加到我的 ggplot 中?以下是原始绘图函数的源代码:

function (data, option = c("alpha", "xi", "quantile"), start = 15,end = NA, 
  reverse = FALSE, p = NA, ci = 0.95, auto.scale = TRUE, labels = TRUE, ...) 
    {
        if (is.timeSeries(data)) 
            data <- as.vector(series(data))
        data <- as.numeric(data)
        ordered <- rev(sort(data))
        ordered <- ordered[ordered > 0]
        n <- length(ordered)
        option <- match.arg(option)
        if ((option == "quantile") && (is.na(p)))
            stop("\nInput a value for the probability p.\n")
        if ((option == "quantile") && (p < 1 - start/n)) {
            cat("Graph may look strange !! \n\n")
            cat(paste("Suggestion 1: Increase `p' above", format(signif(1 - 
                start/n, 5)), "\n"))
            cat(paste("Suggestion 2: Increase `start' above ", ceiling(length(data) * 
                (1 - p)), "\n"))
        }
        k <- 1:n
        loggs <- logb(ordered)
        avesumlog <- cumsum(loggs)/(1:n)
        xihat <- c(NA, (avesumlog - loggs)[2:n])
        alphahat <- 1/xihat
        y <- switch(option, alpha = alphahat, xi = xihat, quantile = ordered * 
            ((n * (1 - p))/k)^(-1/alphahat))
        ses <- y/sqrt(k)
        if (is.na(end)) 
            end <- n
        x <- trunc(seq(from = min(end, length(data)), to = start))
        y <- y[x]
        ylabel <- option
        yrange <- range(y)
        if (ci && (option != "quantile")) {
            qq <- qnorm(1 - (1 - ci)/2)
            u <- y + ses[x] * qq
            l <- y - ses[x] * qq
            ylabel <- paste(ylabel, " (CI, p =", ci, ")", sep = "")
            yrange <- range(u, l)
        }
        if (option == "quantile") 
            ylabel <- paste("Quantile, p =", p)
        index <- x
        if (reverse) 
            index <- -x
        if (auto.scale) {
            plot(index, y, ylim = yrange, type = "l", xlab = "", 
                ylab = "", axes = FALSE, ...)
        }
        else {
            plot(index, y, type = "l", xlab = "", ylab = "", axes = FALSE, 
                ...)
        }
        axis(1, at = index, labels = paste(x), tick = FALSE)
        axis(2)
        threshold <- findthreshold(data, x)
        axis(3, at = index, labels = paste(format(signif(threshold, 
            3))), tick = FALSE)
        box()
        if (ci && (option != "quantile")) {
            lines(index, u, lty = 2, col = 2)
            lines(index, l, lty = 2, col = 2)
        }
        if (labels) {
            title(xlab = "Order Statistics", ylab = ylabel)
            mtext("Threshold", side = 3, line = 3)
        }
        return(invisible(list(x = index, y = y)))
    }

感谢您的帮助!

【问题讨论】:

    标签: r ggplot2 plot statistics


    【解决方案1】:

    我认为您正在寻找 scale_x_continuous()sec.axis 参数。为了确保一切都排好,我必须创建一个函数来查找每个第 n 个值。希望这会有所帮助

    set.seed(1234)
    
    df <- tibble(
      x = 1:50,
      threshold = round(1+rnorm(1:50), 2),
      base_y = c(0.1, runif(49, -1, 2)/5),
      mid =  cumsum(base_y),
      upper = mid + 5,
      lower = mid - 5
    )
    
    
    every_nth <- function(x, n) {
      x[seq(0, length(x), n)]
    }
    
    
    ggplot(df, aes(x = x)) +
      geom_ribbon(aes(ymin = lower, ymax = upper), alpha = 0.2) +
      geom_line(aes(y = mid)) +
      scale_x_continuous(
        breaks = every_nth(df$x, 10),
        sec.axis = dup_axis(
          labels = every_nth(df$threshold, 10),
          name = "Threshold"
        )
      ) +
      theme_minimal()
    
    

    【讨论】:

      猜你喜欢
      • 2022-01-16
      • 2015-10-15
      • 2014-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多