我遇到了一個問題,即如前一篇文章how-to-use-facets-with-a-dual-y-axis-ggplot中所述,在藉助第二軸功能的情況下使用兩個不同的數據。ggplot2中使用雙y軸(第二軸)
我正在嘗試使用geom_point
和geom_bar
,但由於geom_bar數據範圍不同,因此在圖上未顯示。
這是我試過的;
point_data=data.frame(gr=seq(1,10),point_y=rnorm(10,0.25,0.1))
bar_data=data.frame(gr=seq(1,10),bar_y=rnorm(10,5,1))
library(ggplot2)
sec_axis_plot <- ggplot(point_data, aes(y=point_y, x=gr,col="red")) + #Enc vs Wafer
geom_point(size=5.5,alpha=1,stat='identity')+
geom_bar(data=bar_data,aes(x = gr, y = bar_y, fill = gr),stat = "identity") +
scale_y_continuous(sec.axis = sec_axis(trans=~ .*15,
name = 'bar_y',breaks=seq(0,10,0.5)),breaks=seq(0.10,0.5,0.05),limits = c(0.1,0.5),expand=c(0,0))+
facet_wrap(~gr, strip.position = 'bottom',nrow=1)+
theme_bw()
因爲可以看出bar_data被刪除。在這種情況下可以將它們一起繪製?
THX
爲了完成這一切工作,您需要在'geom_bar'中將您的bar值除以15,並使您的'limits'降至0而不是從.1開始(因爲bar從0開始)。 – aosmith