【问题标题】:How to plot a 2- y axis chart with bars side by side without re-scaling the data如何在不重新缩放数据的情况下并排绘制带有条形的 2-y 轴图表
【发布时间】:2019-06-22 03:38:42
【问题描述】:

我的数据如下:

structure(list(Year = 1994:2016, Kcalpd = c(86L, 91L, 98L, 107L, 
116L, 126L, 123L, 112L, 103L, 102L, 103L, 92L, 77L, 59L, 43L, 
29L, 19L, 14L, 13L, 12L, 12L, 10L, 9L), Thtonnes = c(728.364, 
757.467, 780.423, 792.756, 701.685, 720.71, 677.292, 761.649, 
668.218, 679.042, 974.355, 1005.035, 1123.09, 1055.07, 1092.498, 
1100.654, 899.767, 1018.462, 1046.096, 1084.173, 1158.217, 802.194, 
276.773)), row.names = c(NA, -23L), class = "data.frame", .Names = c("Year", 
"Kcalpd", "Thtonnes"))

而且,我的代码如下:

scaleFactor <- max(wfd$Thtonnes) / max(wfd$Kcalpd)

ggplot(wfd, aes(x=Year)) +
  geom_col(aes(y=Thtonnes), fill="blue") +
  geom_col(aes(y=Kcalpd * scaleFactor), fill="red") +
  scale_y_continuous(name="Thtonnes", sec.axis=sec_axis(~./scaleFactor, name="Kcalpd")) +
  theme(
    axis.title.y.left=element_text(color="blue"),
    axis.text.y.left=element_text(color="blue"),
    axis.title.y.right=element_text(color="red"),
    axis.text.y.right=element_text(color="red")
  )

此代码改编自站点:Plot with 2 y axes, one y axis on the left, and another y axis on the right,由威震天提供。此 Web 链接为在一个图形中绘制条形图和线形图提供了一个很好的解决方案。但是,它不会导致我将两个不同比例的数据绘制为一张图中的条形图。因此,到目前为止,我已经达到了以下阶段。 问题是我的酒吧是一个在另一个之后,就像下面给出的图片一样。我希望它们每年都被并排绘制。

见下图:

这个问题与我和 NortonGon 提到的链接完全不同。不重复的原因如下: 1)没有绘制两个不同比例的条形图。大多数情况下,这是通过刻面完成的。 2)我已经证明可以绘制两个不同的条形图,而无需在 2 个不同的 y 轴上重新缩放。 3)如果重复标签被删除,那么我也将代码上传。

【问题讨论】:

  • 可能不是您想要的,但一种方法是 melt() 您的数据,然后使用 facet_wrap() 在不同的窗格中绘制这两个变量。您可以允许scales = "free_y" 单独“放大”每个窗格。
  • 我不想走那条路。实际上,问题来自不同规模的数据。如果我不必担心比例,我可以轻松地将它们并排绘制。但是,那么一类条形将几乎不可见。
  • 您链接的问题中广泛解决了该主题。除非另有说明,正如您可以从 Hadley 的回答中看到的那样,简短的回答是无法完成。尝试浏览那里的所有答案。
  • 要么可以,否则不重复。

标签: r ggplot2


【解决方案1】:

最后用代码回答如下,

上图的代码如下,

A) 数据

structure(list(Year = 1994:2016, Kcalpd = c(86L, 91L, 98L, 107L, 
116L, 126L, 123L, 112L, 103L, 102L, 103L, 92L, 77L, 59L, 43L, 
29L, 19L, 14L, 13L, 12L, 12L, 10L, 9L), Tonnes = c(728364, 757467, 
780423, 792756, 701685, 720710, 677292, 761649, 668218, 679042, 
974355, 1005035, 1123090, 1055070, 1092498, 1100654, 899767, 
1018462, 1046096, 1084173, 1158217, 802194, 276773)), row.names = c(NA, 
-23L), class = "data.frame", .Names = c("Year", "Kcalpd", "Tonnes"
))

B) 代码

scaleFactor <- max(wfd$Thtonnes) / max(wfd$Kcalpd)

ggplot(wfd, aes(x=Year,  width=.4)) +
  geom_col(aes(y=Thtonnes), fill="blue", position = position_nudge(x = -.4)) +
  geom_col(aes(y=Kcalpd * scaleFactor), fill="red") +
  scale_y_continuous(name="Thtonnes (Rice + Wheat)", sec.axis=sec_axis(~./scaleFactor, name="Kcal per day")) +
  scale_x_continuous(breaks = seq(1994, 2016, 4)) +
  theme(
    axis.title.y.left=element_text(color="blue"),
    axis.text.y.left=element_text(color="blue"),
    axis.title.y.right=element_text(color="red"),
    axis.text.y.right=element_text(color="red")
  ) +
  labs(title = "Food Security in Venezuela, Cereals Production and Food Gap", x = element_blank())

不涉及出价交易。只需增加条形之间的间隙并将一种类型移动该边距即可使它们并排对齐。

【讨论】:

    【解决方案2】:

    这是我在评论中引用的解决方案:

    library(ggplot2)
    library(data.table)
    
    wfd <- structure(list(Year = 1994:2016, Kcalpd = c(86L, 91L, 98L, 107L, 
                                                       116L, 126L, 123L, 112L, 103L, 102L, 103L, 92L, 77L, 59L, 43L, 
                                                       29L, 19L, 14L, 13L, 12L, 12L, 10L, 9L), Thtonnes = c(728.364, 
                                                                                                            757.467, 780.423, 792.756, 701.685, 720.71, 677.292, 761.649, 
                                                                                                            668.218, 679.042, 974.355, 1005.035, 1123.09, 1055.07, 1092.498, 
                                                                                                            1100.654, 899.767, 1018.462, 1046.096, 1084.173, 1158.217, 802.194, 
                                                                                                            276.773)), row.names = c(NA, -23L), class = "data.frame", .Names = c("Year", 
                                                                                                                                                                                 "Kcalpd", "Thtonnes"))
    
    
    
    wfd.m <- melt(wfd, id.vars = 1)
    
    ggplot(wfd.m, aes(Year, value, fill = variable)) +
      geom_col(position = "dodge") +
      scale_fill_manual(values = c("Thtonnes" = "blue", "Kcalpd" = "red")) +
      facet_wrap(~variable, ncol = 1, scales = "free_y") +
      theme(legend.position = "none") +
      ylab("your label here")
    

    reprex package (v0.2.1) 于 2019 年 1 月 28 日创建

    【讨论】:

    • 谢谢,恐怕这不是我想要的,刻面不是我选择的解决方案。我希望他们在一个情节中。
    猜你喜欢
    • 2021-11-22
    • 2011-07-07
    • 2013-07-24
    • 1970-01-01
    • 1970-01-01
    • 2015-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多