Arrange the bars of a bar chart in the R

4

I am plotting the following data in R to generate a bar graph:

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 wanted to know how to organize the graph columns in the following order: Sun, Mon, Tue, Wed, Thu, Fri, Sat. Thank you in advance!

    
asked by anonymous 06.11.2017 / 22:05

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 sort 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 edit.
In the above code I use the Sys.Date function to get a Monday. This changes with each passing day and the rest stops working. You should not force the user to change the system date just to work with the date the question was asked and the date the code was developed. So the best solution is to find a fixed day, which, for example, is a Sunday to serve as a basis for the vector of dates from Sunday to Saturday, as requested in the question. To do this simply change one line of the above code.

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, for the year, etc.

    
06.11.2017 / 22:45