在ggplot2中 - 如何确保geom_errorbar在使用xlim()控制x轴时显示所有点的栏限制 [英] In ggplot2 - how to ensure geom_errorbar displays bar limits for all points when controlling x-axis with xlim()
问题描述
我使用 ggplot2
来产生一个相对于整数值预测变量的相当简单的比例图。我使用geom_errorbar来显示每个点估算的不确定性。
例如
require(ggplot2)
mydata< - data.frame(my_x = 70:99,
my_y = seq(0.7,0.3,length.out = 30),
my_lower = seq (mydata,aes(x = my_x))
(0.6,0.2, + geom_point(aes(y = my_y))+
geom_errorbar(aes(ymin = my_lower,ymax = my_upper))+
xlim(70,80)
出于美学原因,我使用xlim()来设置x轴限制(与您一样)。但是这会去除水平线,指示在最小和最大x值处误差线的极限。我认为这是因为 ggplot2
试图绘制一条位于绘图区域之外的线条(函数调用会打印 geom_path
关于缺失值)。
badggplot http://i59.tinypic.com/2nlvlsn.png
我可以事先清理不需要的行的数据,或者包含<$ c $在 ggplot
调用中调用c> subset 语句,但是当放大图时,我觉得应该是更清晰的解决方案。任何想法?
有点乱码,但它似乎工作:
ggplot(mydata,aes(x = my_x))+ geom_point(aes(y = my_y))+
geom_errorbar(aes(ymin = my_lower,ymax = my_upper))+
coord_cartesian(xlim = c(69.5,80.5))
I am using ggplot2
to produce a fairly simple plot of a proportion against an integer valued predictor. I am using geom_errorbar to display uncertainty for each point estimate.
e.g.
require(ggplot2)
mydata <- data.frame(my_x = 70:99,
my_y = seq(0.7,0.3,length.out=30),
my_lower = seq(0.6,0.2,length.out=30),
my_upper = seq(0.8,0.4,length.out=30))
ggplot(mydata,aes(x=my_x)) + geom_point(aes(y=my_y)) +
geom_errorbar(aes(ymin=my_lower, ymax=my_upper)) +
xlim(70,80)
For largely aesthetic reasons I am using xlim() to set the x-axis limits (as you do). But this removes the horizontal lines indicating the limits of the error bars at the min and max x-values. I presume this is because ggplot2
is trying to draw a line which lies outside of the plot region (the function call prints some warnings from geom_path
about missing values).
badggplot http://i59.tinypic.com/2nlvlsn.png
I could clean the data of the unwanted rows beforehand or include a subset
statement in the ggplot
call, but I feel like there is/should be a cleaner solution when zooming into a plot. Any ideas?
Bit hacky but it seems to work:
ggplot(mydata,aes(x=my_x)) + geom_point(aes(y=my_y)) +
geom_errorbar(aes(ymin=my_lower, ymax=my_upper)) +
coord_cartesian(xlim=c(69.5,80.5))
这篇关于在ggplot2中 - 如何确保geom_errorbar在使用xlim()控制x轴时显示所有点的栏限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!