gpt4 book ai didi

r - ggplot2 中带有 geom_line 和 geom_point 的图例

转载 作者:行者123 更新时间:2023-12-05 00:13:22 27 4
gpt4 key购买 nike

我有以下图表。如您所见,它由两条用 geom_line 制作的实线和由 geom_point 制作的灰线点组成。该图正是我需要的样子,但是,图例并未显示灰线也有点 - 需要包括在内,因为 图像将以黑白打印。

output

这是我正在使用的代码

library(ggplot2)
library(reshape2)

ggplot(base_fin, aes(x = fecha, y = valor)) +
geom_line(aes(colour = Serie, linetype= Serie, size = Serie)) +
facet_wrap(~ indicador_f, scales = "free") + theme_bw() +
theme(legend.title = element_blank(),
legend.position="bottom",
text=element_text(family="Calibri"),
plot.caption=element_text(size =8)) +
labs( x =" ",
y = "Balance %",
caption = "Fuente: Fedesarrollo - Encuesta de Opinión Empresarial ") +
geom_point(data = pick(~ Serie == "Desestacionalizada"),
size = 1,
shape=21,
fill = "#696969") +
scale_color_manual(values=c("#696969","#A52A2A")) +
scale_linetype_manual(values=c("solid", "solid")) +
scale_size_manual(values = c(0.7, 0.7))

函数 pick 取自这个 post 并且是
pick <- function(condition){
function(d) d %>% filter_(condition)
}

这是 base_fin 对象的 dput 版本。
structure(list(Serie = c("Original", "Original", "Original", 
"Original", "Original", "Original", "Original", "Original", "Original",
"Original", "Original", "Original", "Original", "Original", "Original",
"Original", "Original", "Original", "Original", "Original", "Original",
"Original", "Original", "Original", "Original", "Original", "Original",
"Original", "Original", "Original", "Original", "Original", "Original",
"Original", "Original", "Original", "Original", "Desestacionalizada",
"Desestacionalizada", "Desestacionalizada", "Desestacionalizada",
"Desestacionalizada", "Desestacionalizada", "Desestacionalizada",
"Desestacionalizada", "Desestacionalizada", "Desestacionalizada",
"Desestacionalizada", "Desestacionalizada", "Desestacionalizada",
"Desestacionalizada", "Desestacionalizada", "Desestacionalizada",
"Desestacionalizada", "Desestacionalizada", "Desestacionalizada",
"Desestacionalizada", "Desestacionalizada", "Desestacionalizada",
"Desestacionalizada", "Desestacionalizada", "Desestacionalizada",
"Desestacionalizada", "Desestacionalizada", "Desestacionalizada",
"Desestacionalizada", "Desestacionalizada", "Desestacionalizada",
"Desestacionalizada", "Desestacionalizada", "Desestacionalizada",
"Desestacionalizada", "Desestacionalizada", "Desestacionalizada",
"Original", "Original", "Original", "Original", "Original", "Original",
"Original", "Original", "Original", "Original", "Original", "Original",
"Original", "Original", "Original", "Original", "Original", "Original",
"Original", "Original", "Original", "Original", "Original", "Original",
"Original", "Original", "Original", "Original", "Original", "Original",
"Original", "Original", "Original", "Original", "Original", "Original",
"Original", "Desestacionalizada", "Desestacionalizada", "Desestacionalizada",
"Desestacionalizada", "Desestacionalizada", "Desestacionalizada",
"Desestacionalizada", "Desestacionalizada", "Desestacionalizada",
"Desestacionalizada", "Desestacionalizada", "Desestacionalizada",
"Desestacionalizada", "Desestacionalizada", "Desestacionalizada",
"Desestacionalizada", "Desestacionalizada", "Desestacionalizada",
"Desestacionalizada", "Desestacionalizada", "Desestacionalizada",
"Desestacionalizada", "Desestacionalizada", "Desestacionalizada",
"Desestacionalizada", "Desestacionalizada", "Desestacionalizada",
"Desestacionalizada", "Desestacionalizada", "Desestacionalizada",
"Desestacionalizada", "Desestacionalizada", "Desestacionalizada",
"Desestacionalizada", "Desestacionalizada", "Desestacionalizada",
"Desestacionalizada", "Original", "Original", "Original", "Original",
"Original", "Original", "Original", "Original", "Original", "Original",
"Original", "Original", "Original", "Original", "Original", "Original",
"Original", "Original", "Original", "Original", "Original", "Original",
"Original", "Original", "Original", "Original", "Original", "Original",
"Original", "Original", "Original", "Original", "Original", "Original",
"Original", "Original", "Original", "Desestacionalizada", "Desestacionalizada",
"Desestacionalizada", "Desestacionalizada", "Desestacionalizada",
"Desestacionalizada", "Desestacionalizada", "Desestacionalizada",
"Desestacionalizada", "Desestacionalizada", "Desestacionalizada",
"Desestacionalizada", "Desestacionalizada", "Desestacionalizada",
"Desestacionalizada", "Desestacionalizada", "Desestacionalizada",
"Desestacionalizada", "Desestacionalizada", "Desestacionalizada",
"Desestacionalizada", "Desestacionalizada", "Desestacionalizada",
"Desestacionalizada", "Desestacionalizada", "Desestacionalizada",
"Desestacionalizada", "Desestacionalizada", "Desestacionalizada",
"Desestacionalizada", "Desestacionalizada", "Desestacionalizada",
"Desestacionalizada", "Desestacionalizada", "Desestacionalizada",
"Desestacionalizada", "Desestacionalizada"), indicador = c("Pedidos",
"Pedidos", "Pedidos", "Pedidos", "Pedidos", "Pedidos", "Pedidos",
"Pedidos", "Pedidos", "Pedidos", "Pedidos", "Pedidos", "Pedidos",
"Pedidos", "Pedidos", "Pedidos", "Pedidos", "Pedidos", "Pedidos",
"Pedidos", "Pedidos", "Pedidos", "Pedidos", "Pedidos", "Pedidos",
"Pedidos", "Pedidos", "Pedidos", "Pedidos", "Pedidos", "Pedidos",
"Pedidos", "Pedidos", "Pedidos", "Pedidos", "Pedidos", "Pedidos",
"Pedidos", "Pedidos", "Pedidos", "Pedidos", "Pedidos", "Pedidos",
"Pedidos", "Pedidos", "Pedidos", "Pedidos", "Pedidos", "Pedidos",
"Pedidos", "Pedidos", "Pedidos", "Pedidos", "Pedidos", "Pedidos",
"Pedidos", "Pedidos", "Pedidos", "Pedidos", "Pedidos", "Pedidos",
"Pedidos", "Pedidos", "Pedidos", "Pedidos", "Pedidos", "Pedidos",
"Pedidos", "Pedidos", "Pedidos", "Pedidos", "Pedidos", "Pedidos",
"Pedidos", "Existencias", "Existencias", "Existencias", "Existencias",
"Existencias", "Existencias", "Existencias", "Existencias", "Existencias",
"Existencias", "Existencias", "Existencias", "Existencias", "Existencias",
"Existencias", "Existencias", "Existencias", "Existencias", "Existencias",
"Existencias", "Existencias", "Existencias", "Existencias", "Existencias",
"Existencias", "Existencias", "Existencias", "Existencias", "Existencias",
"Existencias", "Existencias", "Existencias", "Existencias", "Existencias",
"Existencias", "Existencias", "Existencias", "Existencias", "Existencias",
"Existencias", "Existencias", "Existencias", "Existencias", "Existencias",
"Existencias", "Existencias", "Existencias", "Existencias", "Existencias",
"Existencias", "Existencias", "Existencias", "Existencias", "Existencias",
"Existencias", "Existencias", "Existencias", "Existencias", "Existencias",
"Existencias", "Existencias", "Existencias", "Existencias", "Existencias",
"Existencias", "Existencias", "Existencias", "Existencias", "Existencias",
"Existencias", "Existencias", "Existencias", "Existencias", "Existencias",
"Expectativas", "Expectativas", "Expectativas", "Expectativas",
"Expectativas", "Expectativas", "Expectativas", "Expectativas",
"Expectativas", "Expectativas", "Expectativas", "Expectativas",
"Expectativas", "Expectativas", "Expectativas", "Expectativas",
"Expectativas", "Expectativas", "Expectativas", "Expectativas",
"Expectativas", "Expectativas", "Expectativas", "Expectativas",
"Expectativas", "Expectativas", "Expectativas", "Expectativas",
"Expectativas", "Expectativas", "Expectativas", "Expectativas",
"Expectativas", "Expectativas", "Expectativas", "Expectativas",
"Expectativas", "Expectativas", "Expectativas", "Expectativas",
"Expectativas", "Expectativas", "Expectativas", "Expectativas",
"Expectativas", "Expectativas", "Expectativas", "Expectativas",
"Expectativas", "Expectativas", "Expectativas", "Expectativas",
"Expectativas", "Expectativas", "Expectativas", "Expectativas",
"Expectativas", "Expectativas", "Expectativas", "Expectativas",
"Expectativas", "Expectativas", "Expectativas", "Expectativas",
"Expectativas", "Expectativas", "Expectativas", "Expectativas",
"Expectativas", "Expectativas", "Expectativas", "Expectativas",
"Expectativas", "Expectativas"), valor = c(-17.17171717, -27.77777778,
-20.1320132, -18.58974359, -23.33333333, -20.66666667, -25.33333333,
-18.87417219, -16.0130719, -6.774193548, -3.986710963, -5.555555556,
-12.75167785, -22.48322148, -8.783783784, -20.06688963, -13.75838926,
-15.43624161, -18.27242525, -15.89403974, -10.63122924, -16.82847896,
-18.33333333, -10.33333333, -17.20779221, -24.83221477, -25.08143322,
-25.97402597, -39.03225806, -32.6984127, -36.6, -36.8, -24.9,
-31.3, -26.1, -20.1, -30.6, -19.4257298154394, -25.9703685089816,
-20.3861344496961, -19.9101495485428, -20.2239880538989, -14.7113316572407,
-18.4819137673646, -16.1717368034689, -14.5959502677081, -10.1431943447449,
-9.49293856715717, -14.3431833224798, -15.0175314884738, -19.1589828272007,
-12.6622028387228, -17.2785369642709, -14.7137454056914, -10.9863208231779,
-11.4260631196951, -10.2079867573836, -12.2402042667793, -20.1852851357549,
-22.3325699889125, -20.6210295709765, -17.9890424366566, -23.0052463035128,
-25.33605349585, -28.7634410505091, -32.8858545079308, -29.7448456266512,
-29.7526508392999, -31.1156337087039, -26.5237131647522, -33.1488467400888,
-31.5932131466922, -30.3890229099213, -29.88786713631, 1.219512195,
8.097165992, 3.375527426, 5.555555556, 7.594936709, 4.979253112,
8.032128514, 7.723577236, 6.772908367, 3.585657371, 0.803212851,
2.409638554, -5.761316872, 0, -2.868852459, 3.629032258, 1.176470588,
4.6692607, 3.984063745, 8.661417323, -0.396825397, 1.526717557,
5.416666667, 2.352941176, -1.176470588, 2.857142857, 6.692913386,
5.303030303, 10.546875, 15.11627907, 10, 10.7, 9.1, 7.2, 7.1,
3.6, -3.6, 6.35643479390351, 10.6459166732001, 5.22344247559281,
5.67243419069118, 6.3263552672748, 2.29230191391349, 4.42003999148902,
5.07647767727737, 5.30771779345801, 3.47331159793118, 1.31249391251287,
3.91085347572511, -0.297051262491389, 2.27599773853472, -0.946351168644105,
4.05145498630362, -0.392452138283862, 2.01810682125391, 0.477707150907406,
5.25257945478279, -1.18446517966137, 1.46378983700101, 5.48183798990593,
4.14461402558536, 4.2191872798374, 5.53413370570711, 8.62207440746222,
5.77040428025131, 8.64092656975902, 12.5352275433627, 6.52539967912879,
7.21936961650841, 8.27293183570152, 6.80422490588575, 7.360242701794,
5.38182228563597, 1.63457896220321, 11.94539249, 38.28382838,
45.48494983, 36.4516129, 30.74324324, 28.28282828, 33.55704698,
26.75585284, 31.56146179, 29.12621359, 11.0367893, -1.973684211,
10.77441077, 38.46153846, 37.16216216, 36.12040134, 32.66666667,
35.5704698, 33.44481605, 41.13712375, 34.00673401, 25.72347267,
7.216494845, 0.67114094, 12.54125413, 33.33333333, 31.57894737,
28.66449511, 24.02597403, 21.3592233, 30.6, 29.8, 28.3, 28.3,
7.1, -6.4, 12.7, 19.6082854187009, 28.3975943937634, 36.7646031275808,
32.8922669367486, 27.7187265106525, 26.9910138247318, 28.0425905989078,
18.908960340379, 23.8288780341071, 28.8238115639326, 26.7623103887625,
22.4829956248288, 18.8169769197174, 28.5770502232341, 28.4130406430057,
30.4136428782344, 31.6073593808502, 34.1917179768746, 27.8173747579929,
33.1807504629413, 26.3231570813892, 25.2681111602547, 22.9790320315865,
25.2451317852747, 20.7348942514042, 23.4654135930006, 22.8591878864177,
24.9294286030083, 20.9981164515538, 20.0199851054753, 24.9196082768083,
21.8203205863293, 20.6515258913999, 27.6879437780452, 22.8389127771605,
18.2480968382284, 20.9244154820531), fecha = structure(c(16405,
16436, 16467, 16495, 16526, 16556, 16587, 16617, 16648, 16679,
16709, 16740, 16770, 16801, 16832, 16861, 16892, 16922, 16953,
16983, 17014, 17045, 17075, 17106, 17136, 17167, 17198, 17226,
17257, 17287, 17318, 17348, 17379, 17410, 17440, 17471, 17501,
16405, 16436, 16467, 16495, 16526, 16556, 16587, 16617, 16648,
16679, 16709, 16740, 16770, 16801, 16832, 16861, 16892, 16922,
16953, 16983, 17014, 17045, 17075, 17106, 17136, 17167, 17198,
17226, 17257, 17287, 17318, 17348, 17379, 17410, 17440, 17471,
17501, 16405, 16436, 16467, 16495, 16526, 16556, 16587, 16617,
16648, 16679, 16709, 16740, 16770, 16801, 16832, 16861, 16892,
16922, 16953, 16983, 17014, 17045, 17075, 17106, 17136, 17167,
17198, 17226, 17257, 17287, 17318, 17348, 17379, 17410, 17440,
17471, 17501, 16405, 16436, 16467, 16495, 16526, 16556, 16587,
16617, 16648, 16679, 16709, 16740, 16770, 16801, 16832, 16861,
16892, 16922, 16953, 16983, 17014, 17045, 17075, 17106, 17136,
17167, 17198, 17226, 17257, 17287, 17318, 17348, 17379, 17410,
17440, 17471, 17501, 16405, 16436, 16467, 16495, 16526, 16556,
16587, 16617, 16648, 16679, 16709, 16740, 16770, 16801, 16832,
16861, 16892, 16922, 16953, 16983, 17014, 17045, 17075, 17106,
17136, 17167, 17198, 17226, 17257, 17287, 17318, 17348, 17379,
17410, 17440, 17471, 17501, 16405, 16436, 16467, 16495, 16526,
16556, 16587, 16617, 16648, 16679, 16709, 16740, 16770, 16801,
16832, 16861, 16892, 16922, 16953, 16983, 17014, 17045, 17075,
17106, 17136, 17167, 17198, 17226, 17257, 17287, 17318, 17348,
17379, 17410, 17440, 17471, 17501), class = "Date"), indicador_f = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 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, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Pedidos",
"Existencias", "Expectativas"), class = "factor")), .Names = c("Serie",
"indicador", "valor", "fecha", "indicador_f"), row.names = c(NA,
-222L), class = "data.frame")

我很感激任何帮助。

提前致谢!

最佳答案

您可以制作 shape = Serie整个情节的美学,然后使用 scale_shape其中“原始”的形状是 NA并且没有绘制。

ggplot(base_fin, aes(x = fecha, y = valor, shape = Serie)) +
geom_line(aes(colour = Serie, linetype= Serie, size = Serie)) +
facet_wrap(~ indicador_f, scales = "free") + theme_bw() +
theme(legend.title = element_blank(),
legend.position="bottom",
plot.caption=element_text(size =8)) +
labs( x =" ",
y = "Balance %",
caption = "Fuente: Fedesarrollo - Encuesta de Opinión Empresarial ") +
geom_point(data = pick(~ Serie == "Desestacionalizada"),
size = 1,
fill = "#696969") +
scale_color_manual(values=c("#696969","#A52A2A")) +
scale_linetype_manual(values=c("solid", "solid")) +
scale_size_manual(values = c(0.7, 0.7)) +
scale_shape_manual(values = c(21, NA))

GGplot 使用 aes()映射以找出图例的外观以及如何组合它们,因此此类问题通常需要映射美学。

结果:

enter image description here

PS:一旦像这样设置了情节,您现在不再需要覆盖 geom_point()中的数据, 所以 pick()不再需要。

关于r - ggplot2 中带有 geom_line 和 geom_point 的图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48777422/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com