Doubts ggplot in bars

Asked

Viewed 92 times

2

Hello, I’m trying to plot a bar graph 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 for the day in mode position = "dodge". I studied, saw that x needs to be a factor/date and the other numerics, so I found quite confusing the explanations on sites and the examples taken in the stack in my head seemed to me quite specific. I would like to understand if possible the logic of this Plot, I will leave the data for help 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’m sorry, I didn’t post a Plot or code, because they were all very unsatisfying.

  • I’m finding the values of v1 and v2 strangers. They appear as factors for me. Is that correct? Or should they be numerical values? And if they are numerical values, 1,276 means "1 comma two hundred and seventy-six" or it means "one thousand two hundred and seventy-six"?

  • 1

    @Thanks for noticing, I didn’t have.

  • @Macus Nunes and @Macrusnunes, I spoke very lazily in the text, v1and v2are numerical yes, are na 3° phrase "I studied, saw that x needs to be a factor/date and the other numerical..." .

1 answer

3


I believe that to create this graph it is necessary to change the data.frame from wide to long. 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 @Marcus Nunes comment I edited the answer with the conversion of the columns factor for numeric. The graph has therefore been modified.

Gráfico de barras de duas variáveis

  • Thanks Rui Barradas, I knew I had to transform the data into other formats, but I didn’t know how to do it. I apologize for the delay and thank you again.

  • @One thing I didn’t do was give other names to the columns of the data frame molten. So there’s a graph in English, which I think is bad. And just do names(molten)[2:3] <- nomes_em_português.

Browser other questions tagged

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