Grid on R does not work

Asked

Viewed 41 times

3

Hello, I’m trying to create a chart with the following code:

boxplot(covid_DF$obitosNovos ~ month(covid_DF$data),
        main="Média de obitos novos por mês no Distrito Federal",
        xlab="Mês",
        ylab="Quantidade de casos novos",
        col="#db525a",
        border="#050627"
)

The chart appears right, but when I try to put the function to add a grid to make the user view easier:

boxplot(covid_DF$obitosNovos ~ month(covid_DF$data),
        main="Média de obitos novos por mês no Distrito Federal",
        xlab="Mês",
        ylab="Quantidade de casos novos",
        col="#db525a",
        border="#050627",
        panel.first = grid()
)

Adding the grid function at the end does not work. Could you help me?

  • 3

    Welcome to Stackoverflow! Unfortunately, this question cannot be reproduced by anyone trying to answer it. Please take a look at this link (mainly in the use of function dput) and see how to ask a reproducible question in R. So, people who wish to help you will be able to do this in the best possible way.

1 answer

3


The argument panel.first is evaluated lazily ("Lazy"), which brings some limitations. One is that it does not work when using formula to specify the graph. The simplest solution to have the grid in the background in these cases is to generate the chart, add the grid and plot the graph again on top.

As your question does not depend on your data and for easy playback, I used generic data:

# Dados de exemplo
set.seed(732)
df <- data.frame(
  x = rep(LETTERS[1:4], each = 20),
  y = rnorm(80, 20))

boxplot(y ~ x, df)
grid()
boxplot(y ~ x, df, col = "white", add = TRUE)

inserir a descrição da imagem aqui

Browser other questions tagged

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