Questions about ggplot in bars

2
Hello, I am trying to plot a graph in bars geom_bar , where X corresponds to the days of the month, in this case February, and y refers to the value of two variables v1 e v2 referring to the day in position = "dodge" . I studied, I saw that x needs to be a factor / date and the other numerical ones, so I found the explanations on websites very confusing, and the examples on the stack in my head seemed quite specific. I would like to understand if possible the logic of this plot, I will leave the data for help form and example.

data via dput:

structure(list(dia = structure(1:28, .Label = c("01/02/2018", 
"02/02/2018", "03/02/2018", "04/02/2018", "05/02/2018", "06/02/2018", 
"07/02/2018", "08/02/2018", "09/02/2018", "10/02/2018", "11/02/2018", 
"12/02/2018", "13/02/2018", "14/02/2018", "15/02/2018", "16/02/2018", 
"17/02/2018", "18/02/2018", "19/02/2018", "20/02/2018", "21/02/2018", 
"22/02/2018", "23/02/2018", "24/02/2018", "25/02/2018", "26/02/2018", 
"27/02/2018", "28/02/2018"), class = "factor"), v1 = structure(c(3L, 
12L, 22L, 21L, 9L, 5L, 2L, 4L, 7L, 25L, 19L, 25L, 20L, 1L, 13L, 
8L, 23L, 24L, 17L, 15L, 11L, 10L, 6L, 27L, 26L, 18L, 16L, 14L
), .Label = c("1.014", "1.382", "1.436", "1.462", "1.470", "1.479", 
"1.496", "1.572", "1.618", "1.619", "1.682", "1.692", "1.791", 
"1.839", "1.860", "1.876", "1.883", "1.943", "461", "511", "589", 
"662", "695", "735", "774", "791", "873"), class = "factor"), 
v2 = structure(c(2L, 9L, 21L, 20L, 11L, 5L, 1L, 3L, 4L, 25L, 
18L, 24L, 19L, 28L, 12L, 7L, 23L, 22L, 16L, 14L, 10L, 8L, 
6L, 27L, 26L, 17L, 15L, 13L), .Label = c("1.276", "1.301", 
"1.328", "1.344", "1.348", "1.358", "1.460", "1.468", "1.499", 
"1.515", "1.538", "1.677", "1.708", "1.741", "1.749", "1.755", 
"1.788", "429", "482", "535", "575", "646", "658", "697", 
"744", "752", "778", "924"), class = "factor")), .Names = c("dia", 
"v1", "v2"), row.names = c(NA, -28L), class = "data.frame")

I apologize, I have not posted a plot or code, as all have been very insatiable.

    
asked by anonymous 02.03.2018 / 18:54

1 answer

2

I believe that to create this graphic it is necessary to change the data.frame from the wide format to the long format. See Reshaping data.frame from wide to long format .

library(ggplot2)

dados$dia <- as.Date(dados$dia, "%d/%m/%Y")
dados$v1 <- as.numeric(as.character(dados$v1))
dados$v2 <- as.numeric(as.character(dados$v2))


agg <- aggregate(cbind(v1, v2) ~ dia, dados, sum)
molten <- reshape2::melt(agg, id = "dia")

g <- ggplot(molten, aes(x = dia, y = value, fill = variable))
g + geom_bar(position = "dodge", stat = "identity")

Note
After reading the comment from @Marcus Nunes I edited the answer by converting the columns factor to numeric . The graph was therefore modified.

    
03.03.2018 / 19:11