I have some dataset, lets call it plot_data_lines
looking like this:
|Year |City |value |
-----------------------------
|1981 |Bogota |4 |
|1981 |Cali |3 |
|1981 |Lima |2 |
|1981 |Sao Paulo |1 |
|1981 |Santiago |6 |
|1982 |Bogota |4 |
|1982 |Cali |2 |
|1982 |Lima |1 |
|1982 |Sao Paulo |0 |
|1982 |Santiago |4 |
|1983 |Bogota |3 |
|1983 |Cali |4 |
|1983 |Lima |1 |
|1983 |Sao Paulo |6 |
|1983 |Santiago |6 |
|... |... |... |
|2020 |Bogota |5 |
|2020 |Cali |7 |
|2020 |Lima |9 |
|2020 |Sao Paulo |1 |
|2020 |Santiago |3 |
It is already in long format and when i ask for the class of the dataset this is what i get:
> class(plot_data_lines)
[1] "tbl_df" "tbl" "data.frame"
What i need i to make a line plot of a subset of the cities, for example: Cali, Bogota and Lima, but not necessarily these, and not necessarily only three, and this is what I've tried:
plot_data_lines %>%
filter(City %in% c("Lima", "Bogota","Cali)) %>%
ggplot(plot_data_bar, aes(x = "Year", y = value, color = City))
geom_line()
But I get this error:
Error in `ggplot()`:
! Mapping should be created with `aes()` or `aes_()`.
Run `rlang::last_error()` to see where the error occurred.
What went wrong? Can you guys help me?
Edit: This is the plot that I get with Allan Camero's recomendation:
What went wrong this time?
This is a sample of my data in order to be reproducible:
structure(list(Year = c("1981", "1981", "1981", "1981", "1981",
"1981", "1981", "1981", "1981", "1981", "1981", "1981", "1981",
"1981", "1981", "1981", "1981", "1981", "1981", "1981", "1981",
"1981", "1981", "1981", "1981", "1981", "1981", "1981", "1981",
"1981", "1981", "1981", "1981", "1981", "1981", "1981", "1981",
"1981", "1981", "1981", "1981", "1981", "1981", "1981", "1981",
"1981", "1982", "1982", "1982", "1982", "1982", "1982", "1982",
"1982", "1982", "1982", "1982", "1982", "1982", "1982", "1982",
"1982", "1982", "1982", "1982", "1982", "1982", "1982", "1982",
"1982", "1982", "1982", "1982", "1982", "1982", "1982", "1982",
"1982", "1982", "1982", "1982", "1982", "1982", "1982", "1982",
"1982", "1982", "1982", "1982", "1982", "1982", "1982", "1983",
"1983", "1983", "1983", "1983", "1983", "1983", "1983", "1983",
"1983", "1983", "1983", "1983", "1983", "1983", "1983", "1983",
"1983", "1983", "1983", "1983", "1983", "1983", "1983", "1983",
"1983", "1983", "1983", "1983", "1983", "1983", "1983", "1983",
"1983", "1983", "1983", "1983", "1983", "1983", "1983", "1983",
"1983", "1983", "1983", "1983", "1983", "1984", "1984", "1984",
"1984", "1984", "1984", "1984", "1984", "1984", "1984", "1984",
"1984", "1984", "1984", "1984", "1984", "1984", "1984", "1984",
"1984", "1984", "1984", "1984", "1984", "1984", "1984", "1984",
"1984", "1984", "1984", "1984", "1984", "1984", "1984", "1984",
"1984", "1984", "1984", "1984", "1984", "1984", "1984", "1984",
"1984", "1984", "1984"), City = c("Sao Paulo", "Mexico City",
"Lima", "Bogota", "Rio de Janeiro", "Santiago", "Caracas", "Buenos Aires",
"San Salvador", "Brasilia", "Fortaleza", "Guayaquil", "Quito",
"Belo Horizonte", "Medellin", "Cali", "Havana", "Manaus", "Curitiba",
"Maracaibo", "Recife", "Santa Cruz", "Porto Alegre", "Guadalajara",
"Belem", "Puebla", "Goiania", "Cordoba", "Juarez", "Montevideo",
"Leon de los Aldama", "Tijuana", "Barranquilla", "Tegucigalpa",
"Campinas", "Barquisimeto", "Monterrey", "Sao Luis", "Managua",
"Ciudad Nezahualcoyotl", "Maceio", "Callao", "Santo Domingo",
"Guatemala City", "Port-au-Prince", "Rosario", "Sao Paulo", "Mexico City",
"Lima", "Bogota", "Rio de Janeiro", "Santiago", "Caracas", "Buenos Aires",
"San Salvador", "Brasilia", "Fortaleza", "Guayaquil", "Quito",
"Belo Horizonte", "Medellin", "Cali", "Havana", "Manaus", "Curitiba",
"Maracaibo", "Recife", "Santa Cruz", "Porto Alegre", "Guadalajara",
"Belem", "Puebla", "Goiania", "Cordoba", "Juarez", "Montevideo",
"Leon de los Aldama", "Tijuana", "Barranquilla", "Tegucigalpa",
"Campinas", "Barquisimeto", "Monterrey", "Sao Luis", "Managua",
"Ciudad Nezahualcoyotl", "Maceio", "Callao", "Santo Domingo",
"Guatemala City", "Port-au-Prince", "Rosario", "Sao Paulo", "Mexico City",
"Lima", "Bogota", "Rio de Janeiro", "Santiago", "Caracas", "Buenos Aires",
"San Salvador", "Brasilia", "Fortaleza", "Guayaquil", "Quito",
"Belo Horizonte", "Medellin", "Cali", "Havana", "Manaus", "Curitiba",
"Maracaibo", "Recife", "Santa Cruz", "Porto Alegre", "Guadalajara",
"Belem", "Puebla", "Goiania", "Cordoba", "Juarez", "Montevideo",
"Leon de los Aldama", "Tijuana", "Barranquilla", "Tegucigalpa",
"Campinas", "Barquisimeto", "Monterrey", "Sao Luis", "Managua",
"Ciudad Nezahualcoyotl", "Maceio", "Callao", "Santo Domingo",
"Guatemala City", "Port-au-Prince", "Rosario", "Sao Paulo", "Mexico City",
"Lima", "Bogota", "Rio de Janeiro", "Santiago", "Caracas", "Buenos Aires",
"San Salvador", "Brasilia", "Fortaleza", "Guayaquil", "Quito",
"Belo Horizonte", "Medellin", "Cali", "Havana", "Manaus", "Curitiba",
"Maracaibo", "Recife", "Santa Cruz", "Porto Alegre", "Guadalajara",
"Belem", "Puebla", "Goiania", "Cordoba", "Juarez", "Montevideo",
"Leon de los Aldama", "Tijuana", "Barranquilla", "Tegucigalpa",
"Campinas", "Barquisimeto", "Monterrey", "Sao Luis", "Managua",
"Ciudad Nezahualcoyotl", "Maceio", "Callao", "Santo Domingo",
"Guatemala City", "Port-au-Prince", "Rosario"), value = c(-0.00752411164965841,
-0.00858235806970273, -0.0193390181364576, -0.0254733683434182,
-0.0135609910986883, 0.105134246948455, -0.00327911235803224,
0.02163440097074, -0.0079795250800731, -0.0120663651057852, 0.00566322468255433,
-0.00977488904664624, -0.0168848675316369, -0.0196401016591058,
-0.0192114068827001, -0.0128140808793732, -0.014223152965439,
-0.00973446424945887, 0.0375699418169768, -0.02142105793661,
-0.00695816631860418, 0.0110692713467446, 0.0459416198602932,
-0.00522005746178392, 0.0117604524786534, -0.0171166573772657,
-0.0224270599445623, 0.0568574598072727, 0.363782440715696, 0.00176871008818945,
0.00615268511881207, 0.0218960489961891, -0.00677296734458201,
0.00848375774004822, -0.00592202637837714, -0.00823049786018653,
0.0654160956549713, 0.00655848522936036, -0.00811797806585994,
-0.00759894252446815, -0.0101038496148005, -0.0192750800973933,
-0.00774381271740157, -0.00270769939926697, -0.0115319737226118,
0.067601746659629, 0.0412887465245744, 0.0675304707144076, 0.049352729240842,
-0.00515059960494002, 0.0118978209360105, 0.0532868362072011,
-0.0138900894067038, 0.0553103203820288, 0.0144467932412351,
0.0230619422362681, -0.00768032309305345, 0.0295175710261359,
0.0291436275702546, 0.0334635285240448, 0.00297185628988496,
0.00860089159697064, 0.0418153705968756, 0.00884665494515552,
0.0567507349024041, 0.0115747255551046, 0.00494974302483174,
0.0222767386344832, 0.0410441639217649, 0.0339220986643466, -0.00851634191144674,
0.032073082124077, 0.0150209385609767, 0.0917094854315508, 0.136101721332058,
0.050368267606148, 0.0704829723767993, -0.0392054869667851, 0.000438081167034402,
0.00922131908379296, 0.0250832686146108, -0.0160053135734178,
0.105575686170073, -0.00770350237506892, 0.0185934426465312,
0.0622439535708169, 0.007220419413762, 0.0556334333005619, -0.0169255561672605,
0.0204398042931697, -0.00776762518081134, 0.0723950051880403,
0.020920878349233, -0.00995206377715023, 0.0929229547913617,
0.0228930063871382, 0.00842649575171037, -0.0425511033876532,
0.0473487675635951, -0.0093536731191666, 0.00856150354267576,
6.2395130758288e-05, 0.0180391647982547, 0.00166767498637137,
-0.00860498608682273, 0.0105366504728758, 0.00343173349545559,
-0.00792009126743003, -0.019685205307497, 0.0196050922389284,
0.0219901276461645, 0.0189481802235595, 0.0138318843795572, -0.00365097545580035,
-0.0166818970244572, -0.0479954584836083, 0.0184171924206671,
-0.0176179306226417, 0.000719905928180277, -0.00772610628525053,
0.0235675832224525, -0.0158458659538299, -0.0405076597339783,
0.0531548160747947, 0.00775762163745756, 0.026232494634189, 0.0130544024464169,
0.0467360166589372, 0.0193387856213601, 0.0191346924219357, 0.0251196408085357,
-0.0118308797174065, 0.0125649925727593, 0.0979136729415388,
0.0232545093007293, 0.00307768553311674, 0.0308309002997723,
-0.0158154410745328, 0.0304899632424906, -0.0230856315886497,
-0.113105853584711, -0.061531343276225, 0.0243821742105042, 0.15741352761236,
-0.0266363954419561, 0.00930455151957403, -0.0352929141002193,
0.0117437323008912, -0.0177357806200151, -0.0533701909768894,
-0.0723079488380151, 0.0349916330912371, -0.0541181208042864,
-0.034948153391848, -0.000224652317719612, -0.0338468457314207,
0.0481618504964105, -0.0161638266039731, -0.000296376579238326,
0.0431739523962326, 0.0433822174484497, 0.0169865188027782, -0.0287180945332731,
-0.0206794881498912, 0.0142301430755713, 0.033694331269516, -0.446442104059541,
0.00959839940203957, 0.0161915348613291, 0.0114643674721635,
-0.0244942682854959, -0.0382169511934245, 0.0403839854108356,
-0.00260313570491377, -0.202768982864469, -0.0273731843762094,
-0.0320431601672718, -0.0219882334689516, 0.00146763256941326,
-0.122515996840899, -0.0172163590871104, -0.027759749062993,
-0.0226039983152669, 0.0242689613566711)), row.names = c(NA,
-184L), class = c("tbl_df", "tbl", "data.frame"))
CodePudding user response:
You can do:
plot_data_lines %>%
filter(City %in% c("Lima", "Bogota","Cali")) %>%
ggplot(aes(x = as.numeric(Year), y = value, color = City))
geom_line()
labs(x = "Year")
You will get a vertical line if you put x = "Year"
instead of x = Year
:
plot_data_lines %>%
filter(City %in% c("Lima", "Bogota","Cali")) %>%
ggplot(aes(x = "Year", y = value, color = City))
geom_line()