简体   繁体   中英

Visually unequal spaced log2 scaled axis with geom_smooth

I have a dataset containing descending HRs values (Y-axis) over Age (X-axis), stratified in two groups.

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

I am trying to create two geom_smooth(method = "loess") with the Y-axis (HR) in log2 scale with uneven spacing, limited by specified breaks from 1 to 70.

Reading the data:

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

My first attempt:

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

The scale is unfortunately visually equal but the log2 plot is correct. If I specify breaks, labels and limits:

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

I obtain the right scale but the wrong plot.

The best result so far has been obtained transforming the coordinates last, but still I can't set the breaks the way I want them:

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

Any suggestions?

This might be what you are after:

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))

在此处输入图像描述

This is your 3rd one, but with the added scale_y_continuous() call at the end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM