How to plot two y axes in the same plot, each y axis contains two sets of information, one is geom_col and one is geom_line?

huangapple go评论60阅读模式
英文:

How to plot two y axes in the same plot, each y axis contains two sets of information, one is geom_col and one is geom_line?

问题

I can help you with the translation:

我有数据:

yeardesc <- seq(2002, 2020, 2)

prop1 <- c(0.6751152, 0.6746591, 0.6913932, 0.74829, 0.7085363, 0.7679045, 0.8328877, 0.7802125, 0.7992271, 0.8180833)
prop2 <- c(0.02150538, 0.07438795, 0.05994291, 0.08120438, 0.09961588, 0.07418163, 0.07314629, 0.1220751, 0.1073002, 0.1470971)
money1 <- c(3783.255, 4512.181, 4237.779, 4163.842, 4267.878, 4708.089, 4244.439, 4407.01, 4193.522, 5140.635)
money2 <- c(382.9129, 643.6798, 606.3505, 600.3303, 713.0684, 405.1913, 463.76, 787.4756, 572.8035, 1010.126)

data <- data.frame(yeardesc, prop1, prop2, money1, money2)

我想制作一个如下图所示的图表:
How to plot two y axes in the same plot, each y axis contains two sets of information, one is geom_col and one is geom_line?

基本上,prop1prop2geom_line 图表(蓝色线),而 money1money2geom_col 图表(红色条形图)。我想要两个具有不同刻度的 y 轴。我该如何做到这一点?

英文:

I have data:

yeardesc&lt;-seq(2002,2020,2)

prop1&lt;-c(0.6751152,0.6746591,0.6913932,0.74829,0.7085363,0.7679045,0.8328877,0.7802125,0.7992271,0.8180833)
prop2&lt;-c(0.02150538,0.07438795,0.05994291,0.08120438,0.09961588,0.07418163,0.07314629,0.1220751,0.1073002,0.1470971)
money1&lt;-c(3783.255,4512.181,4237.779,4163.842,4267.878,4708.089,4244.439,4407.01,4193.522,5140.635)
money2&lt;-c(382.9129,643.6798,606.3505,600.3303,713.0684,405.1913,463.76,787.4756,572.8035,1010.126)

data&lt;-data.frame(yeardesc,prop1,prop2,money1,money2)

I want to make a plot like this:
How to plot two y axes in the same plot, each y axis contains two sets of information, one is geom_col and one is geom_line?

Basically, prop1 and prop2 are geom_line plots (the blue lines), and money1 and money2 are geom_col plots (the red bars). I want to have two y axes with different scales. How do I do this?

答案1

得分: 1

如果你确实必须这样做,那么你需要将数据转换成长格式并使用辅助轴:

库(tidyverse)

数据 %>%
  轴向长(-年描述, names_pattern = "(.*)(\\d)", 
               names_to = c(".value", "系列")) %>%
  ggplot(aes(年描述, 比例, color = paste("比例", 系列)))  +
  几何列(aes(y = 金额 / 6000, fill = paste("金额", 系列)), 
           位置 = "躲避", 颜色 = NA) +
  几何线() +
  几何点() +
  比例_y_连续(sec.axis = sec_axis(~6000 * .x, labels = scales::dollar,
                                         name = "金钱")) +
  比例颜色_manual(NULL, values = c("橙色2", "深天蓝4")) +
  填充颜色_manual(NULL, values = c("金色", "浅蓝色")) +
  标签(x = "年份", y = "比例") +
  主题_minimal(base_size = 16) +
  主题(图例位置 = "底部")

然而,通常不建议在数据可视化中使用次要y轴,因为它们有些令人困惑和随意。可能有更好的方式呈现数据 - 尽管在没有上下文的情况下很难做出明确建议。

英文:

If you really have to do this, then you will need to pivot your data into long format and use a secondary axis:

library(tidyverse)

data %&gt;%
  pivot_longer(-yeardesc, names_pattern = &quot;(.*)(\\d)&quot;, 
               names_to = c(&quot;.value&quot;, &quot;series&quot;)) %&gt;%
  ggplot(aes(yeardesc, prop, color = paste(&quot;prop&quot;, series)))  +
  geom_col(aes(y = money / 6000, fill = paste(&quot;money&quot;, series)), 
           position = &quot;dodge&quot;, color = NA) +
  geom_line() +
  geom_point() +
  scale_y_continuous(sec.axis = sec_axis(~6000 * .x, labels = scales::dollar,
                                         name = &quot;Money&quot;)) +
  scale_color_manual(NULL, values = c(&quot;orange2&quot;, &quot;deepskyblue4&quot;)) +
  scale_fill_manual(NULL, values = c(&quot;gold&quot;, &quot;lightblue&quot;)) +
  labs(x = &quot;Year&quot;, y = &quot;Proportion&quot;) +
  theme_minimal(base_size = 16) +
  theme(legend.position = &quot;bottom&quot;)

How to plot two y axes in the same plot, each y axis contains two sets of information, one is geom_col and one is geom_line?

However, secondary y axes are generally discouraged in data visualization, as they are somewhat confusing and arbitrary. There may be better ways to present your data - though it's difficult to make a firm suggestion without context.

huangapple
  • 本文由 发表于 2023年6月13日 02:02:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76459215.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定