Reputation: 9
I just cant find a way to put a legend to a simple plot. (I'm really new in R)
This is the plot. I need to identify each line.
my data:
Fecha // Docente I // Docente II // Docente III // Docente IV // Docente V // Docente VI
2013-09-30 // 83.6723810 // 86.1640476 // 89.7185714 // 93.2509524 // 107.4069048 // 124.0100000
2013-10-31 // 77.7624358 // 80.0780669 // 83.3814834 // 86.6643654 // 99.8204992 // 115.2441140
2014-08-30 // 59.9288106 // 61.7136302 // 64.2593328 // 66.7895324 // 76.9284385 // 88.8149572
2014-09-01 // 59.9649293 // 61.7498651 // 64.3078666 // 66.8287472 // 76.9736700 // 88.8736376
2015-05-01 // 20.7348881 // 21.3520896 // 22.2366045 // 23.1082836 // 26.6162313 // 30.7310448
2015-11-01 // 12.1753399 // 12.5377551 // 13.0571343 // 13.5689764 // 15.6288118 // 18.0449933
2016-03-01 // 14.6043478 // 15.0391304 // 15.4069565 // 15.6600000 // 18.7469565 // 21.6452174
2016-06-01 // 16.9797794 // 17.4852941 // 18.2068015 // 18.9237132 // 21.7959559 // 25.1663603
2016-10-01 // 12.2737081 // 12.6394094 // 13.1607041 // 13.6791596 // 15.7552527 // 18.1930721
2017-02-01 // 6.6072389 // 6.8041086 // 7.0848618 // 7.3639032 // 8.4815358 // 9.7928589
2017-06-01 // 3.7889024 // 3.9018293 // 4.0628049 // 4.2228049 // 4.8637805 // 5.6157317
2017-10-01 // 0.8294410 // 0.8709026 // 0.9231591 // 0.9877890 // 1.0766784 // 1.2274120
2018-01-01 // 0.1687545 // 0.1771905 // 0.1878198 // 0.2009709 // 0.2190547 // 0.2497228
2018-03-01 // 7.3296962 // 7.6955894 // 8.1573399 // 8.7285811 // 9.5142462 // 10.0158413
Upvotes: 0
Views: 43
Reputation: 78917
First bring your data in long format with pivot_longer
.
then use color
:
library(tidyverse)
df1 <- df %>%
pivot_longer (
cols = starts_with("Doc"),
names_to = "Docente",
values_to = "values"
)
ggplot(df1, aes(x= Fecha, y=values, color=Docente)) +
geom_line() +
theme_bw()
data:
df <- structure(list(Fecha = structure(c(15978, 16009, 16312, 16314,
16556, 16740, 16861, 16953, 17075, 17198, 17318, 17440, 17532,
17591), class = "Date"), DocenteI = c(83.672381, 77.7624358,
59.9288106, 59.9649293, 20.7348881, 12.1753399, 14.6043478, 16.9797794,
12.2737081, 6.6072389, 3.7889024, 0.829441, 0.1687545, 7.3296962
), DocenteII = c(86.1640476, 80.0780669, 61.7136302, 61.7498651,
21.3520896, 12.5377551, 15.0391304, 17.4852941, 12.6394094, 6.8041086,
3.9018293, 0.8709026, 0.1771905, 7.6955894), DocenteIII = c(89.7185714,
83.3814834, 64.2593328, 64.3078666, 22.2366045, 13.0571343, 15.4069565,
18.2068015, 13.1607041, 7.0848618, 4.0628049, 0.9231591, 0.1878198,
8.1573399), DocenteIV = c(93.2509524, 86.6643654, 66.7895324,
66.8287472, 23.1082836, 13.5689764, 15.66, 18.9237132, 13.6791596,
7.3639032, 4.2228049, 0.987789, 0.2009709, 8.7285811), DocenteV = c(107.4069048,
99.8204992, 76.9284385, 76.97367, 26.6162313, 15.6288118, 18.7469565,
21.7959559, 15.7552527, 8.4815358, 4.8637805, 1.0766784, 0.2190547,
9.5142462), DocenteVI = c(124.01, 115.244114, 88.8149572, 88.8736376,
30.7310448, 18.0449933, 21.6452174, 25.1663603, 18.1930721, 9.7928589,
5.6157317, 1.227412, 0.2497228, 10.0158413)), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -14L))
Upvotes: 1
Reputation: 26484
Here is a potential solution using pivot_longer() to rearrange the data and make plotting easier:
library(tidyverse)
df <- structure(list(Fecha = structure(c(15978, 16009, 16312, 16314,
16556, 16740, 16861, 16953, 17075, 17198, 17318, 17440, 17532,
17591), class = "Date"), `Docente I` = c(83.672381, 77.7624358,
59.9288106, 59.9649293, 20.7348881, 12.1753399, 14.6043478, 16.9797794,
12.2737081, 6.6072389, 3.7889024, 0.829441, 0.1687545, 7.3296962
), `Docente II` = c(86.1640476, 80.0780669, 61.7136302, 61.7498651,
21.3520896, 12.5377551, 15.0391304, 17.4852941, 12.6394094, 6.8041086,
3.9018293, 0.8709026, 0.1771905, 7.6955894), `Docente III` = c(89.7185714,
83.3814834, 64.2593328, 64.3078666, 22.2366045, 13.0571343, 15.4069565,
18.2068015, 13.1607041, 7.0848618, 4.0628049, 0.9231591, 0.1878198,
8.1573399), `Docente IV` = c(93.2509524, 86.6643654, 66.7895324,
66.8287472, 23.1082836, 13.5689764, 15.66, 18.9237132, 13.6791596,
7.3639032, 4.2228049, 0.987789, 0.2009709, 8.7285811), `Docente V` = c(107.4069048,
99.8204992, 76.9284385, 76.97367, 26.6162313, 15.6288118, 18.7469565,
21.7959559, 15.7552527, 8.4815358, 4.8637805, 1.0766784, 0.2190547,
9.5142462), `Docente VI` = c(124.01, 115.244114, 88.8149572,
88.8736376, 30.7310448, 18.0449933, 21.6452174, 25.1663603, 18.1930721,
9.7928589, 5.6157317, 1.227412, 0.2497228, 10.0158413)), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -14L), spec = structure(list(
cols = list(Fecha = structure(list(format = ""), class = c("collector_date",
"collector")), `Docente I` = structure(list(), class = c("collector_double",
"collector")), `Docente II` = structure(list(), class = c("collector_double",
"collector")), `Docente III` = structure(list(), class = c("collector_double",
"collector")), `Docente IV` = structure(list(), class = c("collector_double",
"collector")), `Docente V` = structure(list(), class = c("collector_double",
"collector")), `Docente VI` = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1L), class = "col_spec"))
df %>%
pivot_longer(-Fecha) %>%
ggplot(aes(x = Fecha, y = value,
colour = name, group = name)) +
geom_line() +
theme_bw(base_size = 18) +
scale_y_continuous(labels = scales::dollar,
name = "Sueldos") +
scale_color_brewer(palette = "Accent",
name = "Docentes")
Created on 2021-07-30 by the reprex package (v2.0.0)
Upvotes: 1