[英]How to define common y-axis limits when using facet_grid() in ggplot2
我在這里的第三個帖子。 我正在使用帶有facet_grid()
的ggplot2
中的線條和區域來領導 plot 設計。 代碼運行良好。 這是我使用的數據:
#My data
df <- structure(list(Var = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L), .Label = c("1", "2", "3", "4", "5", "6", "7", "8",
"9", "10", "11", "12"), class = c("ordered", "factor")), Val = c(46.0233614780009,
85.6471698498353, 83.8100037071854, 98.6977939726785, 94.0682111307979,
92.1834012959152, 79.1579962009564, 62.9422475816682, 2.36891501117498,
25.3718703053892, 87.2779565863311, 32.0944497128949, 444.363995105959,
337.84707041923, 93.2718054391444, 171.342949266545, 81.6757546272129,
135.7353850035, 286.496924120001, 450.293861329556, 339.913251576945,
80.7274857070297, 122.17661133036, 370.043645612895), Group = c("Up",
"Up", "Up", "Up", "Up", "Up", "Up", "Up", "Up", "Up", "Up", "Up",
"Down", "Down", "Down", "Down", "Down", "Down", "Down", "Down",
"Down", "Down", "Down", "Down")), row.names = c(NA, -24L), class = "data.frame")
現在這是我的 plot:
library(ggplot2)
library(dplyr)
#Code for the plot
df %>%
mutate(Val=ifelse(Group=='Down',-Val,Val),
Group=factor(Group,levels = c('Up','Down'),ordered = T)) %>%
ggplot(aes(x=Var,y=Val,color=Group,fill=Group,group=Group))+
geom_line(size=1)+geom_area(alpha=0.75)+
facet_grid(Group~.,scales = 'free')+
scale_y_continuous(labels = function(x) abs(x),
expand = c(0,0.1))+
scale_fill_manual(values=c('cyan','tomato'))+
scale_color_manual(values=c('cyan','tomato'))+
theme_bw()
哪個產生了這個嬰兒:
直到這里一切都很好。 我想是否可以修改兩個元素:
如何減少兩個面之間的垂直空間,以便在視覺上只有一個 x 軸(在同一軸上對齊兩個零並減少該空間)。
我想在兩個 y 軸上都有相同的比例,所以代碼被設計成具有鏡像效果。 我的問題是,當我像這樣在 y 軸上設置比例時:
scale_y_continuous(labels = function(x) abs(x),expand = c(0,0.1),limits = c(NA,800))
一切都變了:
如果可能的話,我希望兩個 y 軸具有相同的比例,從 0 到 800 開始,但尊重幅度。 在這種情況下,go 從 0 到 800 向上,從 0 到 -800 向下,但根據標簽進行屏蔽以產生鏡像效果。
非常感謝您的幫助。
對於舊的“隱形點”技巧來說,這看起來是一個理想的工作,可以在你想要的地方設置分面限制:
library(ggplot2)
library(dplyr)
df %>%
mutate(Val = ifelse(Group == 'Down', -Val, Val),
Group = factor(Group, levels = c('Up', 'Down'), ordered = TRUE)) %>%
ggplot(aes(Var, Val, color = Group, fill = Group, group = Group)) +
geom_line(size = 1) +
geom_area(alpha = 0.75) +
geom_point(data = data.frame(Group = factor(c("Down", "Up"), c('Up', 'Down')),
Var = c(1, 1),
Val = c(-800, 800)), alpha = 0) +
facet_grid(Group~., scales = 'free') +
scale_y_continuous(labels = function(x) abs(x),
expand = c(0, 0.1)) +
scale_fill_manual(values = c('cyan', 'tomato')) +
scale_color_manual(values = c('cyan', 'tomato')) +
theme_bw() +
theme(panel.spacing = unit(0, "points"))
不過,公平地指出您在這里並不真正需要方面,並且您可以通過以下方式獲得非常相似的效果:
df %>%
mutate(Val = ifelse(Group == 'Down', -Val, Val),
Group = factor(Group, levels = c('Up', 'Down'), ordered = TRUE)) %>%
ggplot(aes(Var, Val, color = Group, fill = Group, group = Group)) +
geom_line(size = 1) +
geom_area(alpha = 0.75) +
geom_hline(yintercept = 0) +
scale_y_continuous(labels = function(x) abs(x),
expand = c(0, 0.1),
limits = c(-800, 800)) +
scale_fill_manual(values = c('cyan', 'tomato')) +
scale_color_manual(values = c('cyan', 'tomato')) +
theme_bw() +
theme(panel.spacing = unit(0, "points"))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.