Error in program something related to the lines

Asked

Viewed 52 times

0

energia <- data.frame("curso" = 1:15,"enem"=1:5)
energia$curso <- c("Eng Aeroespacial", "Eng Ambiental", "Eng de Controle e Automação", "Eng de Minas", "Eng de Produção", "Eng de Sistemas", "Eng   Mecânica", "Eng Metalurgica", "Geologia", "Licenciatura em Matemática","Matemática","Química","Sistema de Informação")
energia$enem <- c(1, 2, 3, 4, 5)
rotulo <- c("curso, quantidade que fez enem")
par(mgp=c(1,1,0))
barplot(energia$enem, main="enem por curso", xlab=rotulo[1],ylab=rotulo[2], names.arg = energia$curso, ylim=c(0, 100), cex.names = 0.8, xaxs = "i")


barplot(energia$curso, xlab=rotulo[1], ylab=rotulo[2], names.arg = energia$curso, ylim=c(0, 100), cex.names = 0.8,    xaxs = "i", add=TRUE)
#Error in `$<-.data.frame`(`*tmp*`, curso, value = c("Eng Aeroespacial",  : 
#replacement has 13 rows, data has 15
  • 2

    Your data frame energia and the lines you create after don’t have the same amount of lines. And it’s good to post the error messages along with the questions.

  • 1
    1. The error pointed out by @Jorgemendes is the main one. But 2) why set again enem with exactly the same values? 3) Why ylim=c(0, 145000) (one hundred and forty-five thousand!!!) when max(energia$enem) == 5 and max(energia$curso) == 15? 4) Why have the grid above the bars of enem and below those of curso?
  • I’m trying to compare how many times each course the Enem but other than the error of the lines I fixed with tips those of the line that not but graph was not as I expected how to do please help me

  • The problem, as pointed out by Jorgemendes, is in the second line and relates to the fact that in a data.frame all the columns must have the same size. As the first one has 15 column elements curso should also have, but has only 13 values.

1 answer

1


To correct the error of the question data, I used the function rep to create the vector enem. The variable curso has been defined as being categorical or "factor" to keep the chart bar order down.

curso <- c("Eng Aeroespacial", "Eng Ambiental",
           "Eng de Controle e Automação", 
           "Eng de Minas", "Eng de Produção", 
           "Eng de Sistemas", "Eng   Mecânica", 
           "Eng Metalurgica", "Geologia", 
           "Licenciatura em Matemática",
           "Matemática","Química",
           "Sistema de Informação")
curso <- factor(curso, levels = curso)
enem <- rep(1:5, length.out = length(curso))
energia <- data.frame(curso, enem)

As for the graph, as for me it is better to use the package ggplot2.

library(ggplot2)

ggplot(energia, aes(x = curso, y = enem)) +
  geom_col() +
  ggtitle(label = "enem por curso") +
  theme(axis.text.x = element_text(angle = 50, hjust = 1),
        plot.title = element_text(hjust = 0.5))

inserir a descrição da imagem aqui

Browser other questions tagged

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