簡體   English   中英

ggplot2反轉輔助y軸

[英]ggplot2 Reversing secondary y axis

圖示例

我正在創建一個圖形,在分數的主要y軸(縮放比例為0-5)和次要y軸PCR結果(縮放比例為0-45)上顯示兩個線圖。 但是,我需要反轉第二個y軸,以使45位於y軸的底部(45 =陰性PCR結果)。 我可以繪制次要y軸,但不知道如何反轉它。

鏈接到一些虛擬數據

https://www.filehosting.org/file/details/813109/redditexample.csv

library(data.table)
library(ggplot2)
library(lubridate)

#Read the file you want to use for a graph
data <- fread("redditexample.csv", na.strings = c("", NA))
data$V1 <- lubridate::dmy(data$V1)

plot <- ggplot(data) +
        geom_line(aes(x = V1, y = AveScore, col = type)) +
        geom_point(aes(x = V1, y = PCR)) +
        scale_y_continuous('PCR', sec.axis = sec_axis(trans = "reverse")

也嘗試過:

scale_y_continuous('PCR', sec.axis = sec_axis(~ . *-1 + 50))

關鍵是要轉換第二個y軸變量以生成圖形,然后將其轉換回以進行標記。 使用您的數據,我使用以下代碼生成了圖形:

library(tidyverse)
ggplot(data) +
geom_line(aes(x = V1, y = AveScore, col = type)) +
geom_point(aes(x = V1, y = (50-PCR)/10)) +
scale_y_continuous('AveScore', sec.axis = sec_axis(~ 50 - .*10, name = "PCR"))                    

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM