我正在尝试创建与折线图结合的条形图,但是在将所有图形居中时遇到麻烦,因此它与x轴匹配。我想要条形标签(必须对其进行审查;))和以x轴刻度为中心的两个折线图的点。
我真的不明白为什么x轴刻度线正确地以条为中心,但是其他所有内容都以这种奇怪的方式偏离了中心(尽管我使用相同的变量x来定位它们)。
我如何将它们集中在一起。
这是我绘制的代码(其产生的图像如下):
#### base sytem
par(mar = rep(4, 4))
barData <- con
y <- lineData <- CPL
z <- CPLmax
x <- barplot(barData,
axes = FALSE,
col = "green",
xlab = "",
ylab = "",
ylim = c(0, max(con) * 1.1))
axis(1, at = x, labels = timeline)
axis(4, at = NULL)
par(new = TRUE)
plot(x = x, y = y, type = "b", col = "blue", axes = FALSE, xlab = "", ylab = "", ylim = c(0, max(CPL) * 1.1))
lines(x = x, y = z, type = "b", col = "red", axes = FALSE, ylab = "", ylim = c(0, max(CPL) * 1.1))
axis(2, at = NULL)
text(x = x, y = 3, labels = barData, pos = 1 )
abline(v= x, col="purple")
print(x)
print(y)
box()
作为@JTT答案的替代方法,您可以像这样修复xlim参数:
con <- setNames(runif(12), seq(as.Date("2014-01-01"), as.Date("2014-12-31"), "month"))
xlim <- c(0, length(con)*1.25)
x <- barplot(con, col = "green", xlab = "", ylab = "", ylim = c(0, max(con) * 1.1), axes = FALSE, xlim = xlim)
par(new = TRUE)
plot(x = x, type = "b", y = runif(12), xlim = xlim, xlab = "", ylab = "", axes = FALSE)
abline(v = x, col = "red")
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句