【问题标题】:R - Plot with two y axis, bars and points not alignedR - 带有两个 y 轴、条形和点未对齐的绘图
【发布时间】:2016-11-07 13:19:20
【问题描述】:

对于一个客户,我正在尝试用两个 y 轴组合条形图和线图(带点)。 问题:我的条和点未对齐。

背景:我们有几台机器,正在测量它们的开关数量以及每台机器运行的时间。我们希望将这两种信息放在一个绘图中以节省空间,因为我们有几台机器。

数据按天或小时汇总。以下是一些示例数据:

date <- seq(as.Date("2016-10-01"), as.Date("2016-10-10"), "day")
counts <- c(390, 377, 444, NA, NA, NA, NA, 162, 166, 145)
runtime <- c(56.8, 59.4, 51.0, NA, NA, NA, NA, 38.5, 40.9, 43.4)
df <- data.frame(date = date, counts = counts, runtime = runtime)

到目前为止,这是我尝试过的:

par(mar = c(3,4,4,4) + 0.3)    
barplot(df$runtime, col = "palegreen2", border = "NA", ylab = "runtime in [%]", 
    ylim = c(0,100), font.lab = 2)
par(new = TRUE)
ymax <- max(df$counts, na.rm = TRUE) * 1.05
plot(df$date, df$counts, type = "n", xlab = "", ylab = "", yaxt = "n", 
    main = "Machine 1", ylim = c(0, ymax))
abline(v = date, col = "red", lwd = 2.5)
lines(df$date, df$counts, col = "blue", lwd = 2)
points(df$date, df$counts, pch = 19, cex = 1.5)
axis(4)
mtext("Number of switching operations", side = 4, line = 3, font = 2)

我在这里找到了两个轴的灵感:http://robjhyndman.com/hyndsight/r-graph-with-two-y-axes/

我可以怎样做才能让条的中间与线图的点对齐?

【问题讨论】:

  • 对不起,我好像把图片弄错了,把开头的“你好”丢了:)
  • 这个SO post 可能会为您指明正确的方向。
  • 感谢您提供这个有趣的链接。我试图将箱线图作为对象保护并使用线/点图的值。这似乎行得通。但是如何保持我的日期或日期/时间格式 x 轴?
  • 查看SO post,了解如何根据需要重新标记 x 轴
  • 这真的很棘手。我用我自己的数据和以下编辑过的代码尝试了它: bg

标签: r bar-chart


【解决方案1】:

您遇到的问题是对 barplot 之后的第二个 plot 函数的调用。这是移动/调整绘图画布的大小,导致后续点发生变化。

这是一个快速解决方法,它只是将点和线重新缩放到条形图上。它将条形图保存为一个对象,该对象存储条形中点的 x 轴位置。然后,当您使用 'bp' 作为 x 轴变量绘制 abline、线和点时,它们将正确对齐。

ymax <- max(df$counts, na.rm = TRUE) * 1.05

par(mar=c(4.1,5.1,2.1,5.1))
bp <- barplot(df$runtime, col = "palegreen2", border = "NA", ylab = "runtime in [%]", 
              ylim = c(0,100), font.lab = 2, xlim=c(0.2,12), )

barplot(df$runtime, col = "palegreen2", ylab = "runtime in [%]", border="NA",
    ylim = c(0,100), font.lab = 2)

abline(v = bp, col = "red", lwd = 2.5)
lines(bp, df$counts/ymax*100, col = "blue", lwd = 2)
points(bp, df$counts/ymax*100, pch = 19, cex = 1.5)
axis(4,at=c(0,20,40,60,80,100), labels=c("0","100","200","300","400","500"))
mtext("Number of switching operations", side = 4, line = 3, font = 2)
axis(1, at=bp, labels=df$date)

【讨论】:

    【解决方案2】:

    @emilliman:感谢您的耐心和投入!您的绘图并不完全正确,因为第二个 y 轴的缩放比例不适合点的值,但您的想法帮助我找到了解决方案!

    这是我的新代码:

    library(plyr)
    ymax <- max(df$counts, na.rm = TRUE)
    ymax_up <- round_any(ymax, 100, f = ceiling)
    ylab <- ymax_up/5 * c(0:5)
    
    par(mar = c(3,4,4,4) + 0.3)
    bp <- barplot(df$runtime, col = "palegreen2", border = "NA", ylab = "runtime in [%]", 
        ylim = c(0,100), font.lab = 2, main = "Machine 1")
    abline(v = bp, col = "red", lwd = 2.5)
    lines(bp, 100/ymax_up * df$counts, col = "blue", lwd = 2)
    points(bp, 100/ymax_up * df$counts, pch = 19, cex = 1.5)
    
    axis(4,at=c(0,20,40,60,80,100), labels= as.character(ylab))
    mtext("Number of switching operations", side = 4, line = 3, font = 2)
    xlab <- as.character(df$date, format = "%b %d")
    axis(1, at=bp, labels = xlab)
    abline(h = c(0,100))
    

    (http://i.imgur.com/9YtYGSD.png)

    也许这对遇到此问题的其他人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-02
      • 2019-06-01
      • 1970-01-01
      • 2019-01-23
      • 2015-06-25
      • 2019-07-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多