[英]Plotting discrete predictions with probability intervals - ggplot2
I need to plot some discrete predictions with probability intervals in ggplot2, but I'm having some problems. 我需要在ggplot2中用概率区间绘制一些离散预测,但是我遇到了一些问题。
I have the following data.frame 我有以下data.frame
city pred min.80 max.80
BH 100 50 150
RJ 120 80 140
SP 90 80 100
I want a plot with the cities on y-axis and the predicted values on x-axis. 我想要一个y轴上城市的图和x轴上的预测值。 For each discrete value of y, there should be a horizontal bar with its range being the min.80 and max.80 values.
对于y的每个离散值,应该有一个水平条,其范围是min.80和max.80值。 My idea is to use
geom_rect
from ggplot2
for doing it. 我的想法是使用
geom_rect
的ggplot2
来做到这一点。
I've tried the following code, but the problem is that I'm converting the discrete variable to continuous in order to plot it, and I lose their values on the label. 我尝试了下面的代码,但问题是我将离散变量转换为连续变量以便绘制它,并且我在标签上丢失了它们的值。
> ggplot(df) + geom_rect(aes(xmin=min.80, xmax=max.80, ymin=as.numeric(city)-0.4,
+ ymax=as.numeric(city)+0.4))
Is there another way to do it? 还有另一种方法吗?
I suggest you use the geom pointrange
or crossbar
: 我建议你使用geom
pointrange
或crossbar
:
ggplot(df, aes(x=city)) +
geom_pointrange(aes(ymin=min.80, ymax=max.80, y=pred)) +
coord_flip()
ggplot(df, aes(x=city)) +
geom_crossbar(aes(ymin=min.80, ymax=max.80, y=pred)) +
coord_flip()
I think you want to keep the y axis as a factor ( y=city
). 我想你想把y轴保持为一个因子(
y=city
)。 This kind of (estimate+interval) data is probably is better done with something like geom_pointrange
. 使用
geom_pointrange
东西可能更好地完成这种(估计+间隔)数据。 After all, the "height" of the rectangle doesn't have an interpretation. 毕竟,矩形的“高度”没有解释。
If you have to have the errorbars be horizontal, I've done this before in two ways: 如果您必须将错误栏设置为水平,我之前已经通过两种方式完成了此操作:
coord_flip()
coord_flip()
coord_flip()
, it was a bit limited, so I sometimes also recreated the geom_pointrange()
functionality by combining geom_hline()
with geom_point()
. coord_flip()
,它有点受限,因此我有时也会通过将geom_hline()
与geom_point()
相结合geom_hline()
重新创建geom_pointrange()
功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.