在条形图上添加第二条y轴和线

时间:2020-07-07 12:30:59

标签: r ggplot2

以下是我的数据

df <- data.frame(Lab = c("Queen II", "MMH", "Berea", "Maluti", "Motebang"),
             Expected = c(13200, 5280, 5280, 2640, 5280),
             Actual = c(8759, 761, 2263, 2210, 6100),
             utili_pct = c(66.35, 14.41, 42.86, 83.71, 115.53))

我尝试绘制一个条形图,其中包括在图表上的一条线。

第1步

# I Converted numeric variable "Actual" to a factor 
df$Actualx <- as.factor(df$Actual)

这是为了让我可以绘制具有两个因子变量和一个数字的图表 因此,我整理数据并以这种方式运行图,但是轴比例变得无序。

tidy_Data = df %>% gather(key, value, Actualx, Expected)

ggplot(tidy_Data, aes(x=Lab, y=value, fill=key)) +
         geom_bar(stat = "identity", position = position_dodge(0.8)) `

富特莫尔, 我试图添加一条线utili_ptc和第二条轴,但是比例尺给我带来了困难, 这条线与条不对齐。

ggplot(tidy_Data, aes(x=Lab, y=value, fill=key)) +
   geom_bar(stat = "identity", position = position_dodge(0.8)) + 
   geom_line(aes(x=Lab, y=utili_pct), color = "green", group = 1)

2 个答案:

答案 0 :(得分:1)

这里有一篇关于为何使用次要y轴的文章被皱眉

ggplot with 2 y axes on each side and different scales

为回答您的特定问题,我进行了快速搜索,找到了这篇文章,并用它来构建您的图表。请看看它是否可以理解

https://rpubs.com/kohske/dual_axis_in_ggplot2

  library(ggplot2)
  library(gtable)
  library(grid)

  grid.newpage()

  # two plots
  p1 <- ggplot(tidy_Data, aes(x=Lab, y=value, fill=key)) +
        geom_bar(stat = "identity", position = position_dodge(0.8)) +
        theme(legend.position = 'top')

  p2 <- ggplot(tidy_Data, aes(x = 1:10, y = utili_pct)) + geom_line() + 
        theme_bw() +
        theme(panel.background = element_rect(fill = NA))

  g1 <- ggplot_gtable(ggplot_build(p1))
  g2 <- ggplot_gtable(ggplot_build(p2))            

  pp <- c(subset(g1$layout, name == "panel", se = t:r))
  g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, 
                       pp$l, pp$b, pp$l)

  # axis tweaks
  ia <- which(g2$layout$name == "axis-l")
  ga <- g2$grobs[[ia]]
  ax <- ga$children[[2]]
  ax$widths <- rev(ax$widths)
  ax$grobs <- rev(ax$grobs)
  ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
  g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
  g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

 # draw it
 grid.draw(g)

enter image description here

答案 1 :(得分:0)

非常感谢您的指导。我终于可以使用您的代码以想要的方式绘制图表。

library("tidyverse")

library("ggplot2")

library("gtable")

library("grid")

df <- data.frame(Lab = c("Queen II", "MMH", "Berea", "Maluti", "Motebang"), Actual = c(8759, 761, 2263, 2210, 5100), utili_pct = c(66.35, 14.41, 42.86, 83.71, 96.59), Expected = c(13200, 5280, 5280, 2640, 5280),stringsAsFactors = F)

整理数据

tidy_Data <- df%>% gather(key,value, Actual, Expected)

grid.newpage()

两个地块

p1 <- ggplot(tidy_Data, aes(x=Lab, y=value, fill=key)) + geom_bar(stat = "identity", position = position_dodge(0.8)) + theme(legend.position = "bottom")

p2 <- ggplot(df, aes(x=1:5, y=utili_pct)) + geom_line() + ylim(10,100) + theme_bw() + theme(panel.background = element_rect(fill = NA))

提取gtable

g1 <- ggplot_gtable(ggplot_build(p1)) g2 <- ggplot_gtable(ggplot_build(p2))

第二个图的面板与第一个图的面板重叠

pp <- c(subset(g1$layout, name == "panel", se = t:r)) g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t,pp$l, pp$b, pp$l)

轴调整

ia <- which(g2$layout$name == "axis-l") ga <- g2$grobs[[ia]] ax <- ga$children[[2]] ax$widths <- rev(ax$widths) ax$grobs <- rev(ax$grobs) ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm") g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1) g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

绘制

grid.draw(g)

Desired plot