Arrange bars of a bar graph in R

Asked

Viewed 1,440 times

4

I’m filling the following data into the R to generate a bar chart:

structure(c(5483L, 28034L, 7995L, 5199L, 6785L, 7452L, 7692L), .Dim = 7L, .Dimnames = structure(list(c("dom", "qua", "qui", "sáb", "seg", "sex", "ter")), .Names = ""), class = "table")

I’m generating the following chart:

coresBasicas <- c(1:7)
DataDeAcessoPlot<- barplot(table(DataDeAcesso[,4]),
                           main = "Exibições de Páginas por Dias da Semana",
                           ylim = c(0,30000),
                           xlab="Dias da Semana",
                           col = coresBasicas,
                           ylab="Exibições de Páginas",
                           cex.axis = 0.6,
                           #cex.names = 0.6,
                           las=1
)
text(x=DataDeAcessoPlot, y=table(DataDeAcesso[,4]), label=table(DataDeAcesso[,4]), pos = 3, xpd=NA)

I would like to know how to organize the columns of the graphic in the following order: gift, mon, ter, qua, qui, fri, Sáb. Thanks from now on!

1 answer

6


The first thing to do is to get a vector of abbreviated days of the week. For this I will use the functions Sys.Dat and weekdays. Then I order and apply the inverse function (again order).

# Hoje é segunda-feira, para começar no domingo tem que ser -1:5
y <- weekdays(Sys.Date() + -1:5, abbreviate = TRUE)
DDA <- DataDeAcesso[order(order(y))]

And just adapt the graph to use this vector DDA.

coresBasicas <- 1:7
DataDeAcessoPlot <- barplot(DDA,
                           main = "Exibições de Páginas por Dias da Semana",
                           ylim = c(0,30000),
                           xlab = "Dias da Semana",
                           col = coresBasicas,
                           ylab = "Exibições de Páginas",
                           cex.axis = 0.6,
                           #cex.names = 0.6,
                           las=1
)
text(x = DataDeAcessoPlot[, 1], y = DDA, label = DDA, pos = 3, xpd = NA)

Important issue.
In the above code I use the function Sys.Date to get a Monday. Now that changes every day and the rest stops working. You should not force the user to change the system date just to get it right with the date the question was asked and answered, the date this code was developed. So the best solution is to find a fixed day, which, by the way, is for example a Sunday to serve as a basis for the date vector from Sunday to Saturday, as asked in the question. For this just change a line of code above.

y <- weekdays(as.Date("2017-11-05") + 0:6, abbreviate = TRUE)

We get the vector

y
#[1] "dom" "seg" "ter" "qua" "qui" "sex" "sáb"

And the rest of the code is exactly the same and always works, today, tomorrow, next year, etc.

  • I can’t seem to... After executing the code to assign the variable y the organized days of the week, the next command to assign to the error DDA, with the following message: Error in [.data frame.(DataDeAcesso, order(order(y))) : &#xA; undefined columns selected I have tried to specify the column that corresponds to the days of the week (column 4) but still does not give...

  • trial DDA <- DataDeAcesso[order(order(y)),]

  • It didn’t work... when I executed the command DDA <- DataDeAcesso[order(order(y)),] the DDA data frame is only 7 lines. From what I understand, I would use the y variable to sort the days of the week, which are in DataDeAcesso[,4] if I do not specify column 4 at some point, how the lines will be arranged?

  • @Fábiojunioalvessousa It is better to edit the question and post the output of dput(DataDeAcesso). So we can see how this variable is structured.

  • @Noisy Now I understand and it worked! I took the dput(table(DataDeAcesso[,4]) and stored in a new variable DiasDaSemanaEmFormatoValue, this new variable is not in Data Frame format hence the command DDA <- DiasDaSemanaEmFormatoValue[order(order(y)),] gave right! Thank you very much!!!

  • @Fábiojunioalvessousa I’m glad it worked out. Note: when I read your code I found a little strange so many calls to table. Are you sure they are (all) necessary?

  • Yes because with this function I get the frequency of the data. I have a database with 60000 lines, one of the attributes is this of date, which I converted it to the format YYYY-MM-DD to then obtain the day of the week for each day, then for me to know the frequency of the days of the week in 60000 lines, I use the function table().

Show 2 more comments

Browser other questions tagged

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