浮动条形图,辅助轴上有趋势线

时间:2017-08-31 12:22:20

标签: r ggplot2

我只是试图用R / GGPLOT2创建可视化。我试图实现的图表是一个浮动条形图(一个条形图从一个最小值到最大值)。但是,重叠在此之上我想要一个在辅助轴上的趋势线。到目前为止,这是我的尝试:

# first, your data
table1 <- read.table(text = 'X  A  B  C  D  E  F  G  H  I  J  K  L
                  1      "BAR TOP" 31.5 31.8 30.3 28.0 24.9 24.4 21.7 20.9 24.5 25.4 26.0 28.7
                  2      "TREND VALUE" 1000 1345 1234 1456 1324 1765 1567 1345 1556 1334 1224 1556
                  3      "BAR BOTTOM"  4.0  5.6  4.1 -1.3  0.0 -3.1 -2.6 -1.4 -0.8  2.0  2.7  4.1', header =T)


library(reshape2)
library(ggplot2)
# reshape to wide format (basically transposing the data.frame)
w <- dcast(melt(table1), variable~X)
p<-ggplot(w, aes(x=variable,ymin = `BAR BOTTOM`, 
          ymax = `BAR TOP`, lower = `BAR BOTTOM`, 
          upper = `BAR TOP`, middle = `BAR BOTTOM`)) + 
geom_boxplot(stat = 'identity') 

p <- p + labs(y = "BAR RANGE",
          x = "VARIABLE",
          colour = "Parameter")
p <- p + theme(legend.position = c(0.8, 0.9))
p

这是我想要它们的条形码,但是我在使用TREND VALUE值作为辅助轴上的趋势线时遇到了麻烦。有什么建议或指示吗?

2 个答案:

答案 0 :(得分:2)

我建议手动将TREND VALUE转换为所需范围,并指定具有相同转换的sec.axis。我还将使用geom_rect()代替geom_boxplot()

p <- ggplot(w) +
  aes(ymin = `BAR BOTTOM`,
      ymax = `BAR TOP`,
      xmin = as.numeric(variable) - .3,
      xmax = as.numeric(variable) + .3,
      x = as.numeric(variable),
      # Here we (roughly) transform `TREND VALUE` into range of BAR values
      y = `TREND VALUE`/100 
  ) +
  geom_rect(fill = 'white', col = 'black')+
  geom_line() + 
  scale_x_continuous(labels = levels(w$variable),
                     breaks = 1:nlevels(w$variable))+
  # Specification for secondary axis - inverse transform of `TREND VALUE`
  scale_y_continuous(sec.axis = ~.*100) 

结果情节:

p

enter image description here

修改

回答评论:我们几乎可以指定任何转换:

x值转换为范围,mn - 新的最小值,mx - 新的最大值:

trans_foo <- function(x, mn, mx) {
  (mx - mn)*((x - min(x))/(max(x) - min(x))) + mn
}

后退转型:

itrans_foo <- function(y, min_x, max_x, mn, mx) {
  min_x + ((y - mn)*(max_x - min_x))/(mx - mn)
}

现在将此功能与mx = 0mn = 30一起使用(反转BAR轴的最小值和最大值)&amp; scale_y_reverse(),我们将得到反转的主轴和正常的次轴:

p <- ggplot(w) +
  aes(
    ymin = `BAR BOTTOM`,
    ymax = `BAR TOP`,
    xmin = as.numeric(variable) - .3,
    xmax = as.numeric(variable) + .3,
    x = as.numeric(variable),
    y = trans_foo(
      `TREND VALUE`,
      30, 0)
  ) +
  geom_rect(fill = "white", col = "black") +
  geom_line() +
  scale_x_continuous(
    labels = levels(w$variable),
    breaks = 1:nlevels(w$variable)) +
  labs(
    y = "BAR RANGE",
    x = "VARIABLE",
    colour = "Parameter") +
  # Using scale_y_reverse will reverse the primary axis and
  # reverse the reversed secondary axis making it normal
  scale_y_reverse(sec.axis = sec_axis(
    trans = ~itrans_foo(
      .,
      min(w$`TREND VALUE`),
      max(w$`TREND VALUE`),
      30, 0
    ),
    name = "TREND"))

p

enter image description here

答案 1 :(得分:1)

您可以尝试使用geom_smooth添加黄土平滑线。为了使它在同一轴上,只需重新调整它。

    p<-ggplot(w, aes(x=variable,ymin = `BAR BOTTOM`, 
             ymax = `BAR TOP`, lower = `BAR BOTTOM`, 
             upper = `BAR TOP`, middle = `BAR BOTTOM`)) + 
             geom_boxplot(stat = 'identity') 
             + geom_smooth(aes(x = as.numeric(w$variable), y = w$`TREND VALUE`/100))

这不会产生不同的轴,但除以100便于解释。

See example