英文:
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)
基本上,prop1
和 prop2
是 geom_line
图表(蓝色线),而 money1
和 money2
是 geom_col
图表(红色条形图)。我想要两个具有不同刻度的 y
轴。我该如何做到这一点?
英文:
I have data:
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)
I want to make a plot like this:
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 %>%
pivot_longer(-yeardesc, names_pattern = "(.*)(\\d)",
names_to = c(".value", "series")) %>%
ggplot(aes(yeardesc, prop, color = paste("prop", series))) +
geom_col(aes(y = money / 6000, fill = paste("money", series)),
position = "dodge", color = NA) +
geom_line() +
geom_point() +
scale_y_continuous(sec.axis = sec_axis(~6000 * .x, labels = scales::dollar,
name = "Money")) +
scale_color_manual(NULL, values = c("orange2", "deepskyblue4")) +
scale_fill_manual(NULL, values = c("gold", "lightblue")) +
labs(x = "Year", y = "Proportion") +
theme_minimal(base_size = 16) +
theme(legend.position = "bottom")
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论