【问题标题】:Line graph over Bar Chart ggplot2 R条形图ggplot2 R上的折线图
【发布时间】:2021-01-06 12:35:41
【问题描述】:

我正在尝试使用ggplot2bar plotdouble-y-axis 上绘制line graph

以下是条形图的示例数据和代码:

library(ggplot2)
data <- data.frame("groups" = c('AA','BB','CC', 'AA','BB','CC', 'AA','BB','CC'), 
                   "something" = c('aaaaaa', 'bbbbb', 'cccccc', 'dddddddd', 'eeeeeee', 'ffffffff', 'gggggggggg', 'hhhhhhhhh', 'iiiiiiiii'),
               "value1" = c(1.1, 2.4, 3.5, 5, 4.1, 3, 2.5, 1.4, 4.5),
               "value2" = c(2, 25, 10, 4, 15, 5, 3, 4, 8))
p1 <- ggplot(data, aes(x = something,
                   y = value1,
                   fill = groups)) + geom_bar(stat = "identity",  position = "dodge") + 
  xlab("something") + ylab("groups") + labs(fill = "groups") +
  theme_bw() + theme(aspect.ratio = 2/1,
                 axis.title.x = element_text(face="bold"),
                 axis.title.y = element_text(face="bold"),
                 axis.text.x = element_text(angle = 90, vjust = 0.2, hjust=1)) + coord_equal(1/0.2)

我正在尝试将line 添加到上述bar chart。但是,y-axis 两边的scale 显示相同。

p1 + geom_line(aes(x = something, y = value2), size = 1, color="red", group = 1) +
  scale_y_continuous(sec.axis = sec_axis(~., name = "Count")) +
  geom_point(aes(x = something, y = value2), size = 1, color="blue", group = 1) +
   theme(axis.line.y.right = element_line(color = "red"),
    axis.ticks.y.right = element_line(color = "red"),
    axis.text.y.right = element_text(color = "red"),
    axis.title.y.right = element_text(color = "red"))

当我试图操纵y 值时,如biostats 所示。但是,我仍然没有得到正确的linebarsvalues

p1 + geom_line(aes(x = something, y = 0.1*value2), size = 1, color="red", group = 1) +
   scale_y_continuous(sec.axis = sec_axis(~./0.1, name = "Count")) +
   geom_point(aes(x = something, y = 0.1*value2), size = 1, color="blue", group = 1) +
   theme(axis.line.y.right = element_line(color = "red"),
    axis.ticks.y.right = element_line(color = "red"),
    axis.text.y.right = element_text(color = "red"),
    axis.title.y.right = element_text(color = "red"))

我想为每个对应的axis 绘制正确的linebar

【问题讨论】:

  • 你对这个情节到底有什么问题?对我来说,你的情节和 sec 轴看起来不错。 sec 轴的范围从 0 到 50,并显示 value2 的正确比例。

标签: r ggplot2 plot dplyr


【解决方案1】:

引入第二个轴有两个问题。第一个更笼统。双轴图表上的比例是任意的,导致读者对两个测量值的关系做出潜在的错误假设。

如果你想继续使用双轴图表,第二个问题在 Ggplot2 中。 sec.axis() 不会构建一个全新的轴,而是基于另一个轴转换/构建轴。在您的情况下,第二个比例是第一个比例的 5 倍,因此第二个轴需要乘以 5。为了调整第二个图形(线图)的范围,我们需要将其除以 5。

这段代码:

ggplot(data, aes(x = something,
                 y = value1,
                 fill = groups)) + 
        geom_bar(stat = "identity",  position = "dodge") + 
        geom_line(aes(x = something, y = value2/5), size = 1, color="red", group = 1) +
        geom_point(aes(x = something, y = value2/5), size = 1, color="blue", group = 1) +
        scale_y_continuous(sec.axis = sec_axis(~.*5, name = "Count")) +
        scale_x_discrete(labels = c("aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii"), ,
                         guide = guide_axis(n.dodge = 2)) +
        labs(title = "Overlay of bar and lineplot",
             x = "something", 
             y = "groups", 
             fill = "groups") +
        theme_bw() + 
        theme(aspect.ratio = 2/1,
              axis.title.x = element_text(face="bold"),
              axis.title.y = element_text(face="bold"),
              axis.line.y.right = element_line(color = "red"),
              axis.ticks.y.right = element_line(color = "red"),
              axis.text.y.right = element_text(color = "red"),
              axis.title.y.right = element_text(color = "red"))

结果在这个

【讨论】:

    【解决方案2】:

    在您的情节上尝试这些更改。变量的比例因子似乎存在问题:

    library(ggplot2)
    #Data
    data <- data.frame("groups" = c('AA','BB','CC', 'AA','BB','CC', 'AA','BB','CC'), 
                       "something" = c('aaaaaa', 'bbbbb', 'cccccc', 'dddddddd', 'eeeeeee', 'ffffffff', 'gggggggggg', 'hhhhhhhhh', 'iiiiiiiii'),
                       "value1" = c(1.1, 2.4, 3.5, 5, 4.1, 3, 2.5, 1.4, 4.5),
                       "value2" = c(2, 25, 10, 4, 15, 5, 3, 4, 8))
    #Scale factor
    sf <- max(data$value1)/max(data$value2)
    #Plot
    ggplot(data, aes(x = something,
                           y = value1,
                           fill = groups)) + geom_bar(stat = "identity",  position = "dodge") + 
      xlab("something") + ylab("groups") + labs(fill = "groups") +
      theme_bw() + theme(aspect.ratio = 2/1,
                         axis.title.x = element_text(face="bold"),
                         axis.title.y = element_text(face="bold"),
                         axis.text.x = element_text(angle = 90, vjust = 0.2, hjust=1)) + 
      coord_equal(1/sf)+
      geom_line(aes(x = something, y = value2*sf), size = 1, color="red", group = 1) +
      scale_y_continuous(sec.axis = sec_axis(~./sf, name = "Count")) +
      geom_point(aes(x = something, y = value2*sf), size = 1, color="blue", group = 1) +
      theme(axis.line.y.right = element_line(color = "red"),
            axis.ticks.y.right = element_line(color = "red"),
            axis.text.y.right = element_text(color = "red"),
            axis.title.y.right = element_text(color = "red"))
    

    输出:

    你的代码中的原始情节是下一个:

    【讨论】: