[R] bar plot with non-zero starting level

Xianwen Chen xnwncn at gmail.com
Mon May 20 16:54:37 CEST 2013


Hi Jim,

Thank you for the suggestion. I think overlapped rectangles will well 
present the message. I'm now trying ggplot2.

Here is the code:

require(ggplot2)
rect_MNL_Delta <- data.frame(
         xmin <- c(1, 3, 5, 7, 9, 11, 13),
         xmax <- xmin + 1,
         ymin <- c(16.7026, 14.9968, 16.0630, 17.7510, -5.01694, 
-.44146, 1.45884),
         ymax <- c(21.1602, 18.7613, 19.1367, 23.6730, 2.26564, 3.08630, 
3.39865)
)
rect_GMNL <- data <- data.frame(
         xmin <- c(1, 3, 5, 7, 9, 11, 13),
         xmax <- xmin + 1,
         ymin <- c(17.20, 16.32, 15.86, 18.12, -8.86, -0.03, 0.95),
         ymax <- c(20.12, 18.60, 18.14, 22.29, 3.03, 3.57, 2.81)
)

ggplot() +
         geom_rect(data = rect_MNL_Delta, aes(xmin = xmin, xmax = xmax, 
ymin = ymin, ymax = ymax), fill = "blue", alpha = 0.1) +
         geom_rect(data = rect_GMNL, aes(xmin = xmin, xmax = xmax, ymin 
= ymin, ymax = ymax), fill = "green", alpha = 0.1)

The problem is that the second geom_rect() fully covered the first 
geom_rect(), instead of overlapping. Another issue is that I don't know 
how to set labels on x-axis, instead of the current ticks. I would like 
to have for instance 'Farmed cod' under the first rectangle.

Can you provide me some hints? Thank you.

Kind regards,

Xianwen

Jim Lemon wrote:
> On 05/19/2013 09:19 AM, Xianwen Chen wrote:
>> Hi,
>>
>> I want to plot grouped bars to compare 95% confidence interval estimates
>> from two models. Each bar represents a 95% confidence interval estimate
>> of a coefficient from one of the two models. Each group represents
>> confidence interval estimates of the same coefficient from the two 
>> models.
>>
>> I think such a bar plot will nicely present whether 95% confidence
>> interval estimates of the same coefficient from the two models overlap.
>> All these confidence intervals do not start from the x axis.
>>
>> I searched bar plot examples on Google. I found methods to plot bars in
>> groups by barplot(). I could only specify one offset for all confidence
>> interval estimates of the same model. I could not individually specify
>> an offset for each confidence interval estimate. Can someone please help
>> me on how I may proceed with individual offset for each of the bar in
>> the plot?
>>
> Hi Xianwen,
> It seems that you want to line up either one or both of the estimates 
> for each coefficient and then display the confidence intervals as bars 
> around the estimates. If so, your ordinate (y-axis) won't be very 
> interpretable unless you add one for each pair of coefficients. What I 
> would initially suggest is that you display the coefficient values 
> along a horizontal line and use "error bars" to show the confidence 
> intervals. Here is an example:
>
> coef<-c(3.6,3.2,5.7,6.0,1.2,1.3)
> CIs<-c(1.2,1.4,2.7,2.6,3.1,2.9)
> plot(0,xlim=c(0.5,3.5),ylim=range(c(-CIs,CIs)),type="n",
>  xlab="Coefficients",ylab="95% confidence intervals",xaxt="n")
> axis(1,at=c(0.8,1.2,1.8,2.2,2.8,3.2),
>  labels=c("A1","A2","B1","B2","C1","C2"))
> abline(h=0)
> library(plotrix)
> dispersion(c(0.8,1.2,1.8,2.2,2.8,3.2),rep(0,6),ulim=CIs,interval=TRUE)
> boxed.labels(c(0.8,1.2,1.8,2.2,2.8,3.2),0,coef)
>
> Jim



More information about the R-help mailing list