【问题标题】:Visually unequal spaced log2 scaled axis with geom_smooth带有 geom_smooth 的视觉上不等间距的 log2 缩放轴
【发布时间】:2021-05-08 06:08:09
【问题描述】:

我有一个数据集,其中包含按年龄(X 轴)递减的 HR 值(Y 轴),分为两组。

https://www.dropbox.com/s/l2p24llxcvndljl/reproducible_data_for_log2plot.txt?dl=0

我正在尝试创建两个 geom_smooth(method = "loess"),Y 轴 (HR) 采用 log2 比例,间距不均匀,受限于从 1 到 70 的指定中断。

读取数据:

aLotOfHRs <- read.table("https://www.dropbox.com/s/l2p24llxcvndljl/reproducible_data_for_log2plot.txt?dl=1" , header = TRUE , sep = "\t")

我的第一次尝试:

p <- ggplot(aLotOfHRs, aes(x = Age,y = HR,fill=Quantiles,color =Quantiles)) +
    geom_smooth(method = "loess" , formula = y~x) +
    # geom_point() +
    theme_minimal() +
    ylab("HR per 1-SD (log2 scale)") +
    xlim(c(40,72)) +
    scale_y_continuous(trans = scales::log2_trans())

1st Try

不幸的是,比例在视觉上是相等的,但 log2 图是正确的。如果我指定中断、标签和限制:

p <- ggplot(aLotOfHRs, aes(x = Age,y = HR,fill=Quantiles,color =Quantiles)) +
    geom_smooth(method = "loess" , formula = y~x) +
    # geom_point() +
    theme_minimal() +
    ylab("HR per 1-SD (log2 scale)") +
    xlim(c(40,72)) +
    scale_y_continuous(trans = scales::log2_trans()
        , breaks = log2(c(1.1,2,4,8,16,32,64))
        , labels = c(1.1,2,4,8,16,32,64)
        , limits = c(log2(1.1) , log2(70)))
# Note: I can't extend to 1 or it returns
# Error in seq.default(a, b, length.out = n + 1) :'from' must be a finite number

2nd Try

我得到了正确的比例但错误的情节。

迄今为止最好的结果是最后变换坐标,但我仍然无法按照我想要的方式设置中断:

p <- ggplot(aLotOfHRs, aes(x = Age,y = HR,fill=Quantiles,color =Quantiles)) +
    # geom_point() +
    theme_minimal() +
    ylab("HR per 1-SD of PRS (log2 scale)") +
    xlim(c(40,70)) +
    geom_smooth(method = "loess" , formula = y~x) +
    ylim(c(1,70) ) +
    coord_trans( y = "log2")

3d Try

有什么建议吗?

【问题讨论】:

    标签: r ggplot2 smoothing


    【解决方案1】:

    这可能是你所追求的:

    aLotOfHRs <- read.table("https://www.dropbox.com/s/l2p24llxcvndljl/reproducible_data_for_log2plot.txt?dl=1" , header = TRUE , sep = "\t")
    p <- ggplot(aLotOfHRs, aes(x = Age,y = HR,fill=Quantiles,color =Quantiles)) +
        # geom_point() +
        theme_minimal() +
        ylab("HR per 1-SD of PRS (log2 scale)") +
        xlim(c(40,70)) +
        geom_smooth(method = "loess" , formula = y~x)
    p + scale_y_continuous(trans = scales::log2_trans(),breaks = c(1,2,4,8,16,32,64), lim = c(1,100), expand = c(0,0))
    

    这是您的第三个电话,但最后添加了scale_y_continuous() 电话。

    【讨论】:

    • 这绝对是一个改进,谢谢!但是如果我需要特定的休息时间呢?就我而言,1、2、4、8、16、32、64。此外,ylim(c(1,70)) 似乎对限制 y 轴没有影响
    • 我已经编辑了答案。不幸的是,置信区间的上限高于 70。因此将 y 上限设置为 70 将删除该点周围的置信区间。我用expand = c(0,0) 将它设置为(1,100)。但是您可以尝试一下,看看有什么适合您的需求
    猜你喜欢
    • 1970-01-01
    • 2013-06-28
    • 1970-01-01
    • 2015-08-17
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多