简体   繁体   English

如何在R中的小节之间获取刻度线?

[英]How to get tick marks between the bars in R?

I'd like to get the tick marks between consumption bars, such as in http://hante.home.xs4all.nl/plugwise_dagoverzicht_281009_2.jpg . 我想在消耗条之间打勾,例如在http://hante.home.xs4all.nl/plugwise_dagoverzicht_281009_2.jpg中

It's more logical to get the consumption between two dates, or two times... 在两个日期或两次之间获取消耗量是更合乎逻辑的...

However, I don't see how to do that in R. Anybody? 但是,我在R中看不到该怎么做。有人吗?

I do have such an example table, called data: 我确实有一个示例表,称为数据:

       date  nombre  delta 
 2014-02-01       0      0 
 2014-02-08     120    120 
 2014-02-15     205     85 
 2014-02-22     280     75 
 2014-02-29     350     70 

The code I managed so far to write down is: 到目前为止,我管理下来的代码是:

data$date <- as.POSIXct(data$date, format = "%Y-%m-%d")
data <- data[-2]
barplot(height=data[,2], names=data[,1], las=2)

But the "ticks" are just below the bars... as you can see on http://imgur.com/0WE7ouI . 但是“滴答声”就在条形图的正下方...正如您在http://imgur.com/0WE7ouI上看到的那样。

You didn't provide any code whatsoever. 您没有提供任何代码。 How is your data represented? 您的数据如何表示? How are you plotting the bars? 您如何绘制条形图?

If you are using package lattice 's barchart(..., horizontal=FALSE) , you can manually provide new axis tickslabels using the argument scales=list(x=list(at=seq_len(numBars+1) - 0.5, labels=yourLabels))) where numBars is the number of bins on your x-axis. 如果您使用的是包装latticebarchart(..., horizontal=FALSE) ,则可以使用scales=list(x=list(at=seq_len(numBars+1) - 0.5, labels=yourLabels))) ,其中numBars是x轴上的bin数。 See ?barchart . 参见?barchart

If you are using the more primitive barplot , you can draw an offset axis yourself: 如果您使用的是更原始的barplot ,则可以自己绘制偏移轴:

mids <- barplot(x, axisnames=FALSE)
axis(1, at = mids - (mids[2]-mids[1])/2, labels = yourLabels)

Either way, obviously you can shift to the right instead by changing offset signs, or add both outside ticks for numBins +1 labels. 无论哪种方式,显然您都可以向右移动而不是更改偏移量标志,或者为numBins +1标签添加两个外部刻度。

EDIT : With the code+data you added in your edit: 编辑 :使用您在编辑中添加的代码和数据:

mids <- barplot(height=data[,2], axisnames=FALSE)
axis(1, at = mids - (mids[2]-mids[1])/2, labels = data[,1], las=2)

or eg writing it and adding an end date: 或者例如编写并添加结束日期:

mstep <- (mids[2] - mids[1]) / 2
axis(1, at=c(mids[1] - mstep, mids + mstep), labels=c(data[,1], endDate), las=2)

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

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