[英]R ggplot: How to define group dependent y-axis breaks using facetted ggplots?
我有 40 個組(由 short_ID 定義)並且想要生成 40 個不同的圖,這些圖為每個 short_ID 使用不同的 y 尺度中斷。 我希望 y 尺度的中斷為 (1) 均值-2SD,(2) 均值和 (3) 均值 + 2SD。
我有一個名為 Dataplots 的數據集,其中包含我的 X 和 Y 變量以及分組變量“short_ID”。 我創建了額外的向量 M$SD11 (=mean-2SD)、M$mean 和 M$SD22 (=mean+2SD) 來定義中斷和 M$short_ID 作為分組變量。 下面的代碼部分有效,但問題是我不知道如何使中斷依賴於組(即依賴於 short_ID)。 當我運行下面的代碼時,我得到所有圖的相同 y 軸中斷,即例如向量 M$SD22 的最大值,而不是每個圖的不同 M$SD22 值。 所以我想我需要添加一些東西
"scale_y_continuous(breaks=c(M$SD11, M$mean, M$SD22)", for example "scale_y_continuous(group=M$short_ID, breaks=c(M$SD11, M$mean, M$SD22)" but this does not work.
有人知道我可以做什么來為我的不同組(即 short_ID)定義不同的休息時間嗎? 如何更改下面的代碼來做到這一點? 非常感謝!
Dataplot <- ggplot(data = Dataplots, aes(x = Measure, y = Amylase_u, group = short_ID)) + geom_line() + facet_wrap(~ short_ID) + scale_y_continuous(breaks=c(M$SD11, M$mean, M$SD22))
我添加了一個“Dataplots”和“M”的例子。 出於示例的目的,我只包含了兩個組(即 short_ID),而不是我實際擁有的 40 個。 因此,此示例需要生成 2 個圖,每個 short_ID 一個圖,每個組的 y 軸中斷點不同。
數據圖示例:
dput(Dataplots) structure(list(short_ID = c(1111, 1111, 1111, 1111, 2222, 2222, 2222, 2222), Measure = c(1, 2, 3, 4, 1, 2, 3, 4), Amylase_u = c(81.561, 75.648, 145.25, 85.246, 311.69, 261.74, 600.93, 291.39)), .Names = c("short_ID", "Measure", "Amylase_u"), row.names = c(NA, -8L), class = "data.frame", codepage = 65001L)
M的例子:
dput(M) structure(list(SD11 = c(162, 682), mean = c(97, 366), SD22 = c(32, 51), short_ID = c(1111, 2222)), .Names = c("SD11", "mean", "SD22", "short_ID"), row.names = 1:2, class = "data.frame")
@Mark 我一直在嘗試將您的建議應用於我的完整數據集,但似乎無法做到正確。 我總共有 61 個地塊。 我開始於:
myPlots <-
lapply(unique(Dataplots$short_ID), function(thisID){
Dataplots %>%
filter(short_ID == thisID) %>%
ggplot(aes(x = Measure, y = Amylase_u)) +
geom_line() +
scale_y_continuous(breaks= M %>%
filter(short_ID == thisID) %>%
select(mean) %>%
as.numeric()
) +
ggtitle(thisID)
})
(如您所見,我決定僅在 y 軸上采用主題均值,並決定放棄 SD。)然后我繼續您最后的牛圖建議:
plot_grid(ggdraw() + draw_label("Amylase_u", angle = 90), plot_grid(
plot_grid(plotlist = lapply(myPlots, function(x){x + theme(axis.title = element_blank())}))
, ggdraw() + draw_label("Measurement")
, ncol = 1
, rel_heights = c(0.9, .1))
, nrow = 1, rel_widths = c(0.05, 0.95))
然而,這會產生 61 個圖,y 軸上有主題平均值,但沒有測量其中的測量值(因此圖表本身丟失)。 我想可能有一個 ')' 放錯了地方,所以我試過:
plot_grid(
ggdraw() + draw_label("Amylase_u", angle = 90)
, plot_grid(
plot_grid(plotlist = lapply(myPlots, function(x){x +theme(axis.title = element_blank())}))
, ggdraw() + draw_label("Measurement")
, ncol = 1
, rel_heights = c(0.9, .1)
, nrow = 1
, rel_widths = c(0.05, 0.95)))
這確實給了我圖表,但它們很小而且布局很糟糕(Rplot2)。 我也嘗試調整相對高度和寬度,但即使在閱讀幫助文件后也不太明白我應該如何調整它們。
再次感謝!
最后,我刪除了每個圖頂部的 IDnumbers,因為它們並不是真正必要的,這已經大大改善了圖 (Rplot3),但仍然需要調整布局。
我的理解是,這在facet
功能中仍然是不可能的。 但是,您可以使用cowplot
包自己完成。
首先,循環您的想法(在lapply
)並生成您想要的每個子圖。 請注意,我使用dplyr
進行管道和過濾。
myPlots <-
lapply(unique(Dataplots$short_ID), function(thisID){
Dataplots %>%
filter(short_ID == thisID) %>%
ggplot(aes(x = Measure, y = Amylase_u)) +
geom_line() +
scale_y_continuous(breaks= M %>%
filter(short_ID == thisID) %>%
select(SD11, mean, SD22) %>%
as.numeric()
) +
ggtitle(thisID)
})
然后,從帶有繪圖列表的cowplot
調用函數plot_grid
:
plot_grid(plotlist = myPlots)
給出:
一些注意事項:
cowplot
自動加載自己的默認樣式,因此請使用theme_set
返回您喜歡的樣式 由於我不確定您的目標是什么,這是另一種選擇。 如果您只想繪制與平均值的偏差(以標准差表示)以使更改具有可比性,您只需計算組內列的 z 分數並繪制結果。 再次使用dplyr
:
Dataplots %>%
group_by(short_ID) %>%
mutate(scaledAmylase = as.numeric(scale(Amylase_u)) ) %>%
ggplot(aes(x = Measure
, y = scaledAmylase)) +
geom_line() +
facet_wrap(~short_ID)
給
或者,如果平均值/標准差是在其他地方計算/定義的(並存儲在M
)而不是直接來自數據,您可以使用M
而不是數據進行縮放:
Dataplots %>%
left_join(M) %>%
mutate(scaledAmylase = (Amylase_u - mean) / ((SD22 - mean) / 2) ) %>%
ggplot(aes(x = Measure
, y = scaledAmylase)) +
geom_line() +
facet_wrap(~short_ID)
給
而且,因為我不能單獨留下足夠好,這里是plot_grid
方法的一個版本,它刪除了重復的軸標題並只包含它們一次(就像facet_wrap
一樣)。 如上所述,增加子圖的數量或縱橫比將迫使您在這里調整相對值:
plot_grid(
ggdraw() + draw_label("Amylase_u", angle = 90)
, plot_grid(
plot_grid(plotlist = lapply(myPlots, function(x){x + theme(axis.title = element_blank())}))
, ggdraw() + draw_label("Measurement")
, ncol = 1
, rel_heights = c(0.9, .1))
, nrow = 1
, rel_widths = c(0.05, 0.95)
)
給
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.