R does not create graph correctly

Asked

Viewed 757 times

4

I am using R to generate table charts, with two fields (company name and sales quantity), when using barplot, it is not putting the right values in the fields.

For example, if sales were 500,000 it will make on the chart the highest value 500, ie it is leaving aside the thousands.

How could I solve this problem? And barplot is the best alternative to generate graphs of records similar to this:

  • Name/Sales
    • 1 AMAZON/25.268
    • 2 APPLE/743.928
    • 3 CISCO/792.597
    • 4 GOOGLE/780.093
    • 5 HP COMPANY/108.600
    • 6 IBM/144.234
    • 7 INTEL/66.400
    • 8 MICROSOFT/589.300
    • 9 NETFLIX/1.614

Edit: Output dput command (table)

structure(list(Nome = structure(1:9, .Label = c("AMAZON", "APPLE","CISCO", "GOOGLE", "HP COMPANY", "IBM", "INTEL", "MICROSOFT","NETFLIX"), class = "factor"), Vendas = c(25.268, 743.928, 792.597,780.093, 108.6, 144.234, 66.4, 589.3, 1.614)), .Names = c("Nome", "Vendas"), class = "data.frame", row.names = c(NA, -9L))

I switched points to "," and he made the following mistake:

Error in barplot.default(empresas$Vendas, names.arg = empresas$Nome, main = "Venda por empresa de TI",  :   'height' deve ser um vetor ou uma matriz
  • probably the problem is in reading your data... edit your question by putting the result of dput(tabela), to make it easier to answer!

  • The problem is in your numbers, the point separator is decimal in R.

  • When I swapped the dot for the comma, I gave this error: Error in barplot.default(companies$Sales, Names.Arg = companies$Name, main = "Sale per IT company", : 'height' must be an array or matrix

  • @Rafael you can not put comma. Either truncate the number or will turn the number into text.

  • @Carloscinelli how I can make the numbers appear correct on the chart, without them looking like the ones you generated?

  • I don’t understand your question, rafael.

  • @Carloscinelli For example, I would like it to appear 600,000 instead of 6e+05.

Show 2 more comments

1 answer

5

Your problem is reading the data. Do not save your csv with dot as thousands separator, as the dot is decimal separator in R. For example, by taking the dots at hand in your data the barplot works normally.

empresas <- structure(list(Nome = structure(1:9, 
                                            .Label = c("AMAZON", "APPLE","CISCO", "GOOGLE", "HP COMPANY", "IBM", "INTEL", "MICROSOFT","NETFLIX"), 
                                            class = "factor"), 
                           Vendas = c(25268, 743928, 792597,780093, 1086, 144234, 664, 5893, 1614)), 
                      .Names = c("Nome", "Vendas"), 
                      class = "data.frame", row.names = c(NA, -9L))

barplot(empresas$Vendas, names.arg = empresas$Nome, main = "Venda por empresa de TI")

inserir a descrição da imagem aqui

You can format the labels y axis also if you want, for example:

barplot(empresas$Vendas, names.arg = empresas$Nome, main = "Venda por empresa de TI",
        axes = FALSE)
axis(2, at = c(0, 3e5, 6e5), 
 labels = c("0", "300.000","600.000"))

inserir a descrição da imagem aqui

Or, another example:

barplot(empresas$Vendas, names.arg = empresas$Nome, main = "Venda por empresa de TI",
        axes = FALSE)
axis(2, at = c(0, 3e5, 6e5), 
     labels = c("0", "300 mil","600 mil"))

inserir a descrição da imagem aqui

  • But I can’t fix the format of the numbers?

  • 1

    spin options(scipen=5) before making the chart, so you won’t get the scientific notation

  • 1

    @Rafael, you can format the y axis yes, including putting text, I will put an example of how to do this

Browser other questions tagged

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