Problem using ggplot by group

Asked

Viewed 50 times

5

This is my dataframe:

structure(list(ind = structure(c(177L, 177L, 177L, 177L, 177L, 
177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 
177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 
177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 
177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 
177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 
177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 
177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 
177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 
177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 
177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 177L, 
177L, 177L, 177L, 177L, 177L), .Label = c("01/04/2015", "01/06/2015", 
"01/07/2015", "01/09/2015", "01/10/2015", "01/12/2015", "02/01/2015", 
"02/02/2015", "02/03/2015", "02/04/2015", "02/06/2015", "02/07/2015", 
"02/09/2015", "02/10/2015", "02/12/2015", "03/02/2015", "03/03/2015", 
"03/06/2015", "03/07/2015", "03/08/2015", "03/09/2015", "03/11/2015", 
"03/12/2015", "04/02/2015", "04/03/2015", "04/05/2015", "04/08/2015", 
"04/09/2015", "04/11/2015", "04/12/2015", "05/01/2015", "05/02/2015", 
"05/03/2015", "05/05/2015", "05/06/2015", "05/08/2015", "05/10/2015", 
"05/11/2015", "06/01/2015", "06/02/2015", "06/03/2015", "06/04/2015", 
"06/05/2015", "06/07/2015", "06/08/2015", "06/10/2015", "06/11/2015", 
"07/01/2015", "07/04/2015", "07/05/2015", "07/07/2015", "07/08/2015", 
"07/10/2015", "07/12/2015", "08/01/2015", "08/04/2015", "08/05/2015", 
"08/06/2015", "08/07/2015", "08/09/2015", "08/10/2015", "08/12/2015", 
"09/01/2015", "09/02/2015", "09/03/2015", "09/04/2015", "09/06/2015", 
"09/09/2015", "09/10/2015", "09/11/2015", "09/12/2015", "10/02/2015", 
"10/03/2015", "10/04/2015", "10/06/2015", "10/07/2015", "10/08/2015", 
"10/09/2015", "10/11/2015", "10/12/2015", "11/02/2015", "11/03/2015", 
"11/05/2015", "11/06/2015", "11/08/2015", "11/09/2015", "11/11/2015", 
"11/12/2015", "12/01/2015", "12/02/2015", "12/03/2015", "12/05/2015", 
"12/06/2015", "12/08/2015", "12/11/2015", "13/01/2015", "13/02/2015", 
"13/03/2015", "13/04/2015", "13/05/2015", "13/07/2015", "13/08/2015", 
"13/10/2015", "13/11/2015", "14/01/2015", "14/04/2015", "14/05/2015", 
"14/07/2015", "14/08/2015", "14/09/2015", "14/10/2015", "14/12/2015", 
"15/01/2015", "15/04/2015", "15/05/2015", "15/06/2015", "15/07/2015", 
"15/09/2015", "15/10/2015", "15/12/2015", "16/01/2015", "16/03/2015", 
"16/04/2015", "16/06/2015", "16/07/2015", "16/09/2015", "16/10/2015", 
"16/11/2015", "16/12/2015", "17/03/2015", "17/04/2015", "17/06/2015", 
"17/07/2015", "17/08/2015", "17/09/2015", "17/11/2015", "17/12/2015", 
"18/02/2015", "18/03/2015", "18/05/2015", "18/06/2015", "18/08/2015", 
"18/09/2015", "18/11/2015", "18/12/2015", "19/01/2015", "19/02/2015", 
"19/03/2015", "19/05/2015", "19/06/2015", "19/08/2015", "19/10/2015", 
"19/11/2015", "20/01/2015", "20/02/2015", "20/03/2015", "20/04/2015", 
"20/05/2015", "20/07/2015", "20/08/2015", "20/10/2015", "21/01/2015", 
"21/05/2015", "21/07/2015", "21/08/2015", "21/09/2015", "21/10/2015", 
"21/12/2015", "22/01/2015", "22/04/2015", "22/05/2015", "22/06/2015", 
"22/07/2015", "22/09/2015", "22/10/2015", "22/12/2015", "23/01/2015", 
"23/02/2015", "23/03/2015", "23/04/2015", "23/06/2015", "23/07/2015", 
"23/09/2015", "23/10/2015", "23/11/2015", "23/12/2015", "24/02/2015", 
"24/03/2015", "24/04/2015", "24/06/2015", "24/07/2015", "24/08/2015", 
"24/09/2015", "24/11/2015", "25/02/2015", "25/03/2015", "25/05/2015", 
"25/06/2015", "25/08/2015", "25/09/2015", "25/11/2015", "26/01/2015", 
"26/02/2015", "26/03/2015", "26/05/2015", "26/06/2015", "26/08/2015", 
"26/10/2015", "26/11/2015", "27/01/2015", "27/02/2015", "27/03/2015", 
"27/04/2015", "27/05/2015", "27/07/2015", "27/08/2015", "27/10/2015", 
"27/11/2015", "28/01/2015", "28/04/2015", "28/05/2015", "28/07/2015", 
"28/08/2015", "28/09/2015", "28/10/2015", "28/12/2015", "29/01/2015", 
"29/04/2015", "29/05/2015", "29/06/2015", "29/07/2015", "29/09/2015", 
"29/10/2015", "29/12/2015", "30/01/2015", "30/03/2015", "30/04/2015", 
"30/06/2015", "30/07/2015", "30/09/2015", "30/10/2015", "30/11/2015", 
"30/12/2015", "31/03/2015", "31/07/2015", "31/08/2015"), class = "factor"), 
    date = structure(c(23L, 106L, 66L, 31L, 114L, 71L, 32L, 5L, 
    88L, 48L, 12L, 95L, 56L, 21L, 104L, 64L, 26L, 109L, 76L, 
    35L, 116L, 82L, 41L, 1L, 84L, 53L, 11L, 94L, 60L, 20L, 103L, 
    69L, 30L, 113L, 75L, 40L, 120L, 81L, 46L, 4L, 87L, 47L, 19L, 
    102L, 62L, 29L, 112L, 73L, 39L, 119L, 80L, 45L, 8L, 91L, 
    52L, 14L, 97L, 54L, 15L, 98L, 72L, 33L, 115L, 79L, 44L, 7L, 
    90L, 51L, 10L, 93L, 59L, 18L, 101L, 67L, 24L, 107L, 68L, 
    43L, 3L, 86L, 50L, 9L, 92L, 58L, 17L, 100L, 61L, 28L, 111L, 
    70L, 37L, 118L, 78L, 38L, 6L, 89L, 49L, 13L, 96L, 57L, 22L, 
    105L, 65L, 27L, 110L, 77L, 36L, 117L, 83L, 42L, 2L, 85L, 
    55L, 16L, 99L, 63L, 25L, 108L, 74L, 34L), .Label = c("01/02/2020", 
    "01/02/2025", "01/04/2023", "02/01/2021", "02/03/2019", "02/03/2024", 
    "02/07/2022", "02/10/2021", "03/06/2023", "03/09/2022", "04/04/2020", 
    "04/05/2019", "04/05/2024", "04/12/2021", "05/02/2022", "05/04/2025", 
    "05/08/2023", "05/11/2022", "06/03/2021", "06/06/2020", "06/07/2019", 
    "06/07/2024", "06/10/2018", "07/01/2023", "07/06/2025", "07/09/2019", 
    "07/09/2024", "07/10/2023", "08/05/2021", "08/08/2020", "08/12/2018", 
    "09/02/2019", "09/04/2022", "09/08/2025", "09/11/2019", "09/11/2024", 
    "09/12/2023", "10/02/2024", "10/07/2021", "10/10/2020", "11/01/2020", 
    "11/01/2025", "11/03/2023", "11/06/2022", "11/09/2021", "12/12/2020", 
    "13/02/2021", "13/04/2019", "13/04/2024", "13/05/2023", "13/08/2022", 
    "13/11/2021", "14/03/2020", "15/01/2022", "15/03/2025", "15/06/2019", 
    "15/06/2024", "15/07/2023", "15/10/2022", "16/05/2020", "16/09/2023", 
    "17/04/2021", "17/05/2025", "17/08/2019", "17/08/2024", "17/11/2018", 
    "17/12/2022", "18/02/2023", "18/07/2020", "18/11/2023", "19/01/2019", 
    "19/03/2022", "19/06/2021", "19/07/2025", "19/09/2020", "19/10/2019", 
    "19/10/2024", "20/01/2024", "21/05/2022", "21/08/2021", "21/11/2020", 
    "21/12/2019", "21/12/2024", "22/02/2020", "22/02/2025", "22/04/2023", 
    "23/01/2021", "23/03/2019", "23/03/2024", "23/07/2022", "23/10/2021", 
    "24/06/2023", "24/09/2022", "25/04/2020", "25/05/2019", "25/05/2024", 
    "25/12/2021", "26/02/2022", "26/04/2025", "26/08/2023", "26/11/2022", 
    "27/03/2021", "27/06/2020", "27/07/2019", "27/07/2024", "27/10/2018", 
    "28/01/2023", "28/06/2025", "28/09/2019", "28/09/2024", "28/10/2023", 
    "29/05/2021", "29/08/2020", "29/12/2018", "30/04/2022", "30/11/2019", 
    "30/11/2024", "30/12/2023", "31/07/2021", "31/10/2020"), class = "factor"), 
    meses = 1:120, values = c(12.09, 12.24285616, 12.32523777, 
    12.4175892, 12.49, 12.55609513, 12.60608153, 12.64508917, 
    12.66, 12.67, 12.68, 12.67291657, 12.67, 12.66, 12.64108285, 
    12.61097318, 12.59177228, 12.5619576, 12.53300743, 12.50567336, 
    12.44803261, 12.3953477, 12.3462815, 12.32180909, 12.294111, 
    12.27226535, 12.25, 12.22783631, 12.21069901, 12.18312071, 
    12.15810836, 12.13179377, 12.10739136, 12.08369738, 12.06, 
    12.05246023, 12.04, 12.03, 12.02, 12.01566151, 12.01, 12.0040305, 
    12, 11.98887922, 11.96806883, 11.94815773, 11.93, 11.92385859, 
    11.91, 11.9, 11.8955347, 11.88492663, 11.87497743, 11.8560007, 
    11.84359218, 11.82798632, 11.80860077, 11.79230126, 11.78, 
    11.77430286, 11.76698351, 11.76, 11.75296816, 11.7452531, 
    11.73598231, 11.71688083, 11.70346756, 11.68776681, 11.66660186, 
    11.65457132, 11.64, 11.63330237, 11.63, 11.63, 11.62235668, 
    11.62, 11.62, 11.61270054, 11.61, 11.61, 11.61, 11.60301511, 
    11.6, 11.6, 11.59220472, 11.59, 11.59, 11.58480377, 11.58, 
    11.58, 11.58, 11.58, 11.57248072, 11.57, 11.57, 11.57, 11.56220687, 
    11.56, 11.56, 11.56, 11.55398858, 11.55, 11.55, 11.55, 11.55, 
    11.54, 11.54, 11.54, 11.53687782, 11.53348684, 11.53015706, 
    11.53, 11.53, 11.53, 11.52721843, 11.52402531, 11.52088685, 
    11.52, 11.52, 11.52)), row.names = 1801:1920, class = "data.frame")

The original dataframe is much larger. Therefore, for example, I used only a group that weighs for the code to run and generate the Plot:

ggplot(df[df$ind=="23/01/2015",],aes(x=date, y=values, colour=ind,group=1)) + geom_line()

This is not the correct chart. It would be a smoother chart, first rising and then falling.

Where am I going wrong?

1 answer

6


Notice that your column date is as Factor, so the levels are totally out of order.

str(df)
#'data.frame':  120 obs. of  4 variables:
# $ ind   : Factor w/ 246 levels "01/04/2015","01/06/2015",..: 177 177 177 177 #177 177 177 177 177 177 ...
# $ date  : Factor w/ 120 levels "01/02/2020","01/02/2025",..: 23 106 66 31 114 #71 32 5 88 48 ...
# $ meses : int  1 2 3 4 5 6 7 8 9 10 ...
# $ values: num  12.1 12.2 12.3 12.4 12.5 ...

To solve this turn this column to the correct format.

df$date <- as.Date(df$date,"%d/%m/%Y")

library(ggplot2)
ggplot(df[df$ind=="23/01/2015",], aes(x=date, y=values, colour=ind,group=1)) + 
  theme(axis.text.x = element_text(angle=90, hjust=1, colour="black", size=rel(1)), 
        axis.text.y = element_text(hjust=1, colour="black", size=rel(1))) +
  geom_line() 

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.