[英]Dual plot sharing the same x-axis in ggplot2
我有這種格式的數據集。
head(sppec.p)
Wavelength_.nm. Mean Lower Upper Species Feature_selection Count
1 400 0.1467549 0.1290778 0.2758327 ALAL T-test 0
2 405 0.1454303 0.1271905 0.2726207 ALAL T-test 0
3 410 0.1464431 0.1290472 0.2754903 ALAL T-test 0
4 415 0.1468586 0.1298166 0.2766753 ALAL T-test 0
5 420 0.1485061 0.1310419 0.2795480 ALAL T-test 0
6 425 0.1517019 0.1342690 0.2859708 ALAL T-test 0
我最初計划制作雙軸圖,但是在這里搜索之后,我意識到在ggplot
中這是“不可能的”,許多人建議不要使用此類圖。 因此,我已經解決了多方面的問題。 我嘗試先進行各個圖,但是我無法弄清楚如何使用facet_wrap
和scales = "free_y"
來獲得x軸相同但y軸不同的2個圖。
各個圖的代碼:
myplot<-ggplot(sppec.p, aes(x=Wavelength_.nm., y=Mean, group=Species, linetype = Species,
ymin = Lower, ymax = Upper)) +
geom_ribbon(alpha = 0.2,fill='gray') +
geom_line() + theme_bw() +scale_fill_grey(start = 0.1, end = 0.9) +
opts(panel.background = theme_rect(fill='grey80')) +
ylim(0,3)
myplot + theme_bw ()+ opts(axis.line = theme_segment(colour = "black"),
panel.grid.major = theme_blank(),
panel.grid.minor = theme_blank())
...產生
第二張圖:
cum.totals<-ggplot(sppec.p, aes(Wavelength_.nm., y=Count, fill = factor(Feature_selection))) + geom_bar(stat="identity")
cum.totals + theme_bw() +scale_fill_grey(start = 0.1, end = 0.9)+
opts(axis.line = theme_segment(colour = "black"),
panel.grid.major = theme_blank(),
panel.grid.minor = theme_blank(),
panel.border = theme_blank(),
panel.background = theme_blank())
產生的圖:
盡管兩個y軸在數值上都相等,但是它們表示不同的值。
我對ggplot
不太熟悉,我想知道如何使用facet_wrap
或其他任何可以讓我在共享同一a軸但在2個方面共享2個圖的功能。 是否可以使我的代碼更高效?
因此,最終輸出的布局基本上是這樣的:
當您將同一類型的圖表分為多個類別時,“ Facet
很有用。 您的情況有所不同。 查看各種facet_grid
和facet_wrap
上的幫助條目,以了解區別。 這可以幫助您入門-
ggplot(mtcars, aes(mpg, wt)) + geom_point() + facet_grid(. ~ cyl)
我不是兩個不同的y軸相對於同一x軸的忠實擁護者(如果我正確理解的話,這就是您要在其中進行的操作)。 相反,請嘗試使用參數gridExtra::arrangeGrob
ncol = 1
gridExtra::arrangeGrob
將圖表垂直堆疊在另一個圖表的頂部,從而使相同的x軸彼此重疊,並達到類似的效果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.