I need to plot some discrete predictions with probability intervals in ggplot2, but I'm having some problems.
I have the following 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. For each discrete value of y, there should be a horizontal bar with its range being the min.80 and max.80 values. My idea is to use geom_rect
from ggplot2
for doing it.
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
:
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
). This kind of (estimate+interval) data is probably is better done with something like 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()
, it was a bit limited, so I sometimes also recreated the geom_pointrange()
functionality by combining geom_hline()
with geom_point()
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.