简体   繁体   中英

Barplot with continuous x axis using base r graphics

I am looking to scale the x axis on my barplot to time, so as to accurately represent when measurements were taken.

I have these data frames:

    > Botcv
        Date Average       SE
1 2014-09-01     4.0 1.711307
2 2014-10-02     5.5 1.500000

> Botc1
        Date Average        SE
1 2014-10-15   2.125 0.7180703
2 2014-11-12   1.000 0.4629100
3 2014-12-11   0.500 0.2672612

> Botc2
        Date Average        SE
1 2014-10-15   3.375 1.3354708
2 2014-11-12   1.750 0.4531635
3 2014-12-11   0.625 0.1829813

I use this code to produce a grouped barplot:

covaverage <- c(Botcv$Average,NA,NA,NA)
c1average <- c(NA,NA, Botc1$Average)
c2average <- c(NA,NA, Botc2$Average)
date <- c(Botcv$Date, Botc1$Date)

averagematrix <- matrix(c(covaverage,c1average, c2average), nrow=3, ncol=5, byrow=TRUE)

barplot(averagematrix,date, xlab="Date", ylab="Average", axis.lty=1, space=NULL,width=3,beside=T, ylim=c(0.00,6.00))

R plots the bars equal distances apart by default and I have been trying to find a workaround for this. I have seen several other solutions that utilise ggplot2 but I am producing plots for my masters thesis and would like to keep the appearance of my barplots in line with other graphs that I have created using base R graphics. I also want to add error bars to the plot. If anyone could provide a solution then I would be very grateful!! Thanks!

If what you want to get is simply the theme that will match the base theme the + theme_bw() in ggplot2 will achieve this:

data(mtcars)
require(ggplot2)
ggplot(mtcars, aes(factor(cyl), mpg)) +
    geom_boxplot() + 
    theme_bw()

Result

ggplot2箱形图

Alternative

boxplot(mpg~cyl,data=mtcars)

正常

If, as you said, the only thing you want to achieve is similar look, and you have working plot in the ggplot2 using the theme_bw() should produce plots that are indistinguishable from what would be derived via the standard plotting mechanism. If you feel so inclined you may tweak some minutiae details like font sizes, thickness of graph borders or visualisation of outliers.

Perhaps you can use this as a start. It is probably easier to use boxplots, as they can be put at a given x position by using the at argument. For base barplots this cannot be done, but you can use rectangle instead to replicate the barplot look. Error bars can be added using arrows or segments .

bar_w = 1 # width of bars
offset = c(-1,1) # offset to avoid overlapping
cols = grey.colors(2) # colors for different types

# combine into a single data frame
d = data.frame(rbind(Botc1, Botc2), 'type' = c(1,1,1,2,2,2))

# set up empty plot with sensible x and y lims
plot(as.Date(d$Date), d$Average, type='n', ylim=c(0,4))

# draw data of data frame 1 and 2
for (i in unique(d$type)){

    dd = d[d$type==i, ]
    x = as.Date(dd$Date)
    y = dd$Average

    # rectangles
    rect(xleft=x-bar_w+offset[i], ybottom=0, xright=x+bar_w+offset[i], ytop=y, col=cols[i])
    # errors bars
    arrows(x0=x+offset[i], y0=y-0.5*dd$SE, x1=x+offset[i], y1=y+0.5*dd$SE, col=1, angle=90, code=3, length = 0.1)
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM