r - 带有 ggplot2 的双比例图:为添加的变量添加图例条目
问题描述
我有两个这种数据框:
dput(plot_1)
plot_1<- structure(list(t_exp = c(479.18330748, 550.09264769, 581.27775562,
602.90277249, 729.74280191, 963.299432, 1186.40357135, 1377.06205603,
1604.97385043, 1977.32706598, 1528.89847687, 2017.83048618, 2558.91142867,
2422.08428098, 2418.86951627, 2248.93633177, 1908.92844943, 479.18330748,
550.09264769, 581.27775562, 602.90277249, 729.74280191, 963.299432,
1186.40357135, 1377.06205603, 1604.97385043, 1977.32706598, 1528.89847687,
2017.83048618, 2558.91142867, 2422.08428098, 2418.86951627, 2248.93633177,
1908.92844943, 479.18330748, 550.09264769, 581.27775562, 602.90277249,
729.74280191, 963.299432, 1186.40357135, 1377.06205603, 1604.97385043,
1977.32706598, 1528.89847687, 2017.83048618, 2558.91142867, 2422.08428098,
2418.86951627, 2248.93633177, 1908.92844943, 479.18330748, 550.09264769,
581.27775562, 602.90277249, 729.74280191, 963.299432, 1186.40357135,
1377.06205603, 1604.97385043, 1977.32706598, 1528.89847687, 2017.83048618,
2558.91142867, 2422.08428098, 2418.86951627, 2248.93633177, 1908.92844943,
479.18330748, 550.09264769, 581.27775562, 602.90277249, 729.74280191,
963.299432, 1186.40357135, 1377.06205603, 1604.97385043, 1977.32706598,
1528.89847687, 2017.83048618, 2558.91142867, 2422.08428098, 2418.86951627,
2248.93633177, 1908.92844943), t_exp_reg = c(26.16551766, 32.54411008,
31.89983946, 33.57790402, 40.57386933, 51.27728681, 72.02726694,
85.89296127, 96.54878305, 125.99423998, 100.51912523, 158.86908483,
208.20248928, 176.42267237, 190.65640004, 176.10542289, 130.7703496,
32.71137458, 39.35283051, 40.59087882, 45.3692821, 60.72202556,
80.76792966, 105.34277987, 116.81838439, 132.64802812, 168.07775832,
112.32696236, 144.44785762, 184.26048785, 181.47032946, 169.40245823,
153.22527354, 144.68736618, 7.74947477, 11.5467708, 15.111152,
16.76515862, 20.21513219, 32.21155115, 49.08098119, 49.34752514,
65.23062284, 90.83448565, 98.68533335, 110.35181638, 155.20134044,
184.11839864, 202.12998344, 202.38291918, 186.14894147, 300.17015871,
338.94189249, 347.69477529, 355.54335399, 419.69931314, 554.38578988,
691.16322191, 839.23036408, 957.08705321, 1162.56265848, 872.12931417,
1206.33027022, 1521.9238857, 1400.98004137, 1300.24106912, 1249.64540352,
1029.14047836, 112.38678176, 127.70704381, 145.98111005, 151.64707376,
188.53246169, 244.6568745, 268.78932144, 285.77282115, 353.45936321,
429.85792355, 345.23774176, 397.83145713, 489.3232254, 479.09283914,
556.43960544, 467.57731264, 418.18131382), year = c(1999L, 2000L,
2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L,
2010L, 2011L, 2012L, 2013L, 2014L, 2015L, 1999L, 2000L, 2001L,
2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L,
2011L, 2012L, 2013L, 2014L, 2015L, 1999L, 2000L, 2001L, 2002L,
2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L,
2012L, 2013L, 2014L, 2015L, 1999L, 2000L, 2001L, 2002L, 2003L,
2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L,
2013L, 2014L, 2015L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L,
2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L,
2014L, 2015L), NO_REGIAO_UF = structure(c(4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L
), .Label = c("REGIAO SUDESTE", "REGIAO SUL", "REGIAO CENTRO OESTE",
"REGIAO NORTE", "REGIAO NORDESTE", "REGIAO NAO DECLARADA"), class = "factor")), row.names = c(NA,
-85L), class = c("data.table", "data.frame"))
plot_tot<-structure(list(t_exp = c(479.18330748, 550.09264769, 581.27775562,
602.90277249, 729.74280191, 963.299432, 1186.40357135, 1377.06205603,
1604.97385043, 1977.32706598, 1528.89847687, 2017.83048618, 2558.91142867,
2422.08428098, 2418.86951627, 2248.93633177, 1908.92844943),
year = 1999:2015, scaled = c(284.99639069633, 327.170034291692,
345.717515109138, 358.579089501427, 434.017757786058, 572.926596136247,
705.618769406107, 819.013746111758, 954.565293478285, 1176.0240146804,
909.319128707865, 1200.1136029187, 1521.9238857, 1440.54533466909,
1438.63333914465, 1337.56482635206, 1135.34363508228)), row.names = c(NA,
-17L), class = c("data.table", "data.frame"))
Plot_tot
有巴西总出口的数据,同时 Plot_1
在区域层面细分了这些数据。我的目标是将这两个度量绘制成一个图表,考虑到它们的比例不同,我使用以下命令:
scl <- max(plot_tot$t_exp)/max(plot_1$t_exp_reg)
ggplot(plot_1, aes(x = year, y = t_exp_reg, color = NO_REGIAO_UF)) +
geom_line() +
labs(title = "Export by Region", x = "Year", y = "Total Export by region (100M US$)", fill = "Region") +
geom_line(data =plot_tot,aes(y = scaled, x = year),inherit.aes = FALSE, color="#362828",size = 0.8,linetype=2)+
scale_y_continuous(breaks=seq(0, 2600, 400), sec.axis = sec_axis(~.*scl, name = "Brazil total exports (100M US$)")) +
scale_x_continuous(breaks = seq(1990,2015,3)) +
labs(color = "Legend")
该图的问题在图例中。虽然此解决方案为我在图例中没有的每个区域生成了一个图例,但总出口线从plot_tot
. 有谁知道我可以如何调整图例并生成一个额外的条目来解释虚线表示总出口?
非常感谢您的帮助
此致
解决方案
您可以通过添加一个单独的键来做到这一点linetype
:
ggplot(plot_1, aes(x = year, y = t_exp_reg, color = NO_REGIAO_UF)) +
geom_line()+
labs(title = "Export by Region", x = "Year",
y = "Total Export by region (100M US$)", fill = "Region") +
geom_line(data = plot_tot,
aes(y = scaled,
x = year,
linetype = factor("Total")),
inherit.aes = FALSE, size = 0.8)+
scale_linetype_manual(values = c(2), breaks = "Total") +
scale_y_continuous(breaks=seq(0, 2600, 400),
sec.axis = sec_axis(~.*scl,
name = "Brazil total exports (100M US$)")) +
scale_x_continuous(breaks = seq(1990, 2015, 3)) +
labs(color = "Regions", linetype = "") +
theme(legend.key.width = unit(30, "points"))
推荐阅读
- python - 多处理 - 内存消耗
- python - Windows 10 找到 pip 但没有找到 python
- java - 需要一个找不到的“com.ganesh.utils.JwtUtils2”类型的 bean
- reactjs - React 父布局取决于子级(必须重新渲染)
- mysql - 使用 Web 服务和 myPHPAdmin 将迁移的 .net 站点连接到 Synology NAS 上的 MySQL 数据库的问题
- c++ - 未定义的符号 str::string 和字符串文字
- javascript - GEOJSON 为什么给出 3 个坐标系,谁能更简单地解释一下它的属性
- python - 导入 keras 和 tensorflow 时出错
- python - PyQT5 QSound 声音之间的延迟
- r - 加入列表列表中的数据