r - 使用facet_wrap时在ggplot2中使用具有不同比例因子的辅助y轴

标签 r ggplot2 plot axis facet-wrap

假设我有以下数据:

library(ggplot2)
library(ggthemes)

data = structure(list(origin = c("ARG", "ARG", "ARG", "ARG", "CHL", 
"CHL", "CHL", "CHL", "COL", "COL", "COL", "COL", "MEX", "MEX", 
"MEX", "MEX"), date = c(2012, 2013, 2014, 2015, 2012, 2013, 2014, 
2015, 2012, 2013, 2014, 2015, 2012, 2013, 2014, 2015), reer = c(99.200680735245, 
88.1100217095859, 91.138945064955, 38.2318792759958, 97.1355065168361, 
96.1872670893033, 93.6345905776444, 92.1029850680499, 101.123844098755, 
94.173001658586, 77.1226216761908, 59.6337376438912, 98.0983258996167, 
97.6713495865999, 92.2842729861424, 86.2605669691898), x_r = c(0.0874733578362671, 
0.0815610804254794, 0.0783917054809495, 0.0579932868099816, 0.178204232427659, 
0.16321408066481, 0.170084977520404, 0.151329817378872, 0.0498810245214703, 
0.0429419825495197, 0.0383271589817956, 0.0413797639710004, 0.246549060641858, 
0.242694346464116, 0.236773340112642, 0.269553103263527)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -16L), groups = structure(list(
    origin = c("ARG", "CHL", "COL", "MEX"), .rows = list(1:4, 
        5:8, 9:12, 13:16)), row.names = c(NA, -4L), class = c("tbl_df", 
"tbl", "data.frame")))

我正在尝试使用 facet_wrap 进行绘图以及使用 sec.axis 的辅助 y Axis scale_y_continuous 的选项.到目前为止,我得到的是以下内容:
scale = 500

ggplot(data, aes(x = date)) +
  geom_line(aes(y = x_r), size = 2, color = "red") + 
  geom_line(aes(y = reer/scale), size = 2, color = "blue") +
  facet_wrap(.~origin, ncol = 4, scales = "free_y") + 
  scale_y_continuous(
    name = "X/GDP",
    sec.axis = sec_axis(~.*scale, name = "REER")
  ) +   
  theme_bw() +
  theme(
    axis.title.y = element_text(color = "red", size = 13),
    axis.title.y.right = element_text(color = "blue", size = 13)
  ) +
  ggtitle("Export Ratio and Real Effective Exchange Rate")

enter image description here

然而,scale对于所有国家/地区,我使用的因子都是常数(比例 = 500),我希望每个国家/地区都有不同的比例因子。类似 scaleFactor1 = max(x_r)/max(reerr) .我知道什么sec.axis scale_y_continuous 的选项make 是主要 y Axis 的线性组合,但我希望每个国家/地区都不同。我已经尝试了以下方法,但它不起作用:
data = data %>% 
  group_by(origin) %>% 
  mutate(scaleFactor = max(x_r)/max(reerr)) %>% 
  mutate(reer_2 = reerr/scaleFactor)

ggplot(data, aes(x = date)) +
  geom_line(aes(y = x_r), size = 2, color = "red") + 
  geom_line(aes(y = reer), size = 2, color = "blue") +
  facet_wrap(.~origin, ncol = 4, scales = "free_y") + 
  scale_y_continuous(
    name = "X/GDP",
    sec.axis = sec_axis(~.*scaleFactor, name = "REER")
  ) +   
  theme_bw() +
  theme(
    axis.title.y = element_text(color = "red", size = 13),
    axis.title.y.right = element_text(color = "blue", size = 13)
  ) +
  ggtitle("Export Ratio and Real Effective Exchange Rate")

最佳答案

一种方法是拆分国家/地区的数据并创建单独的图,将它们保存到列表中。然后使用 牛图包以网格布局绘制它们,类似于 facet_wrap来自 ggplot .

这是您创建绘图的代码,减去 facet_wrap,并创建 scaleFactor,以及标题的 Country 对象。

myPlot <- function(data){

  scaleFactor <- max(data$reer) / max(data$x_r)
  Country <- data$origin

  p <- ggplot(data, aes(x = date)) +
    geom_line(aes(y = x_r), size = 2, color = "red") + 
    geom_line(aes(y = reer/scaleFactor), size = 2, color = "blue") +
    #facet_wrap(.~origin, ncol = 4, scales = "free_y") + 
    scale_y_continuous(
      name = "X/GDP",
      sec.axis = sec_axis(~.*scaleFactor, name = "REER")
    ) +   
    theme_bw() +
    theme(
      axis.title.y = element_text(color = "red", size = 13),
      axis.title.y.right = element_text(color = "blue", size = 13)
    ) +
    ggtitle(Country)
  p
}

现在 split原产地和用途数据lapply调用 myPlot功能。
data2 <- split(data, data$origin)
p_lst <- lapply(data2, myPlot)

制作标题图并使用 plot_grid将它们排列在网格中。
p0 <- ggplot() + labs(title="Export Ratio and Real Effective Exchange Rate")

library(cowplot)

p1 <- plot_grid(plotlist=p_lst, ncol=2)
pp <- plot_grid(p0, p1, ncol=1, rel_heights=c(0.1, 1))

enter image description here

关于r - 使用facet_wrap时在ggplot2中使用具有不同比例因子的辅助y轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61565304/

相关文章:

R代码: columns to rows i. e团体到个人

r - 将负二项式参数从 R 转换为 Excel

r - ggplot2-将 geom_segment 扩展到绘图的可见范围之外

r 向条形图添加线条或线段

r - 在数据后面绘制网格,而不是在 R 前面

r - 如何格式化R中SPEI包的硬编码绘图函数的x轴?

r - 具有非完整网格的曲面图

r - 在 Markdown 文件上使用 knit 但忽略一大块代码 - 采购 knitr 文件

r - 将图例添加到多直方图 ggplot

r - 如何从OSRM解码编码的折线并绘制路线几何图形?