我遇到了一个问题,即如前一篇文章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