【发布时间】:2017-10-10 17:36:02
【问题描述】:
我遇到了一个问题,即在上一篇文章how-to-use-facets-with-a-dual-y-axis-ggplot 中描述的第二个轴函数的帮助下使用两个不同的数据。
我正在尝试使用 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 已被删除。在这种情况下是否可以将它们绘制在一起??
谢谢
【问题讨论】:
-
要使这一切正常工作,您需要将
geom_bar中的条形值除以 15,并使您的limits下降到 0 而不是从 0.1 开始(因为条形图从 0 开始)。