简体   繁体   English

使用基本r图形的x轴连续条形图

[英]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. 我希望按比例缩放小图上的x轴,以准确表示何时进行测量。

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: 我使用以下代码来生成分组的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. R默认情况下将条形图等距隔开,我一直在尝试找到一种解决方法。 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. 我已经看到了几种其他使用ggplot2的解决方案,但我正在为硕士论文制作图,并希望使条形图的外观与我使用基本R图形创建的其他图保持一致。 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: 如果你想获得什么很简单,就是将匹配基本主题的主题+ theme_bw()ggplot2将实现这一点:

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. 如您所说,如果唯一要实现的是相似的外观,并且您在ggplot2使用theme_bw()绘制了工作图,则应生成的图与通过标准绘制机制得出的图无法区分。 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. 使用箱形图可能更容易,因为可以通过使用at参数将它们放置在给定的x位置。 For base barplots this cannot be done, but you can use rectangle instead to replicate the barplot look. 对于基本的条形图,无法完成此操作,但是可以改用rectangle来复制barplot外观。 Error bars can be added using arrows or segments . 可以使用arrowssegments添加误差线。

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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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