Plot Graph Multiple Variable Bars in R

Asked

Viewed 1,477 times

2

I need to assemble a graph in R from a CSV table in bars as follows: The Y axis refers to the total count of each variable (column) on the X axis. Where Y is the number of people and X is the number of columns in the table. The examples I found only use the bar graph to express frequency, which is not my need. Thank you

2 answers

2

An alternative to @Molx’s response, but following the concept tidy data.

library(dplyr)
library(tidyr)
library(ggplot2)

dados <- replicate(5, sample(1:10, 5, TRUE))
colnames(dados) <- paste0("Col", 1:5)

dados_tidy <- dados %>%
  as.data.frame() %>%
  gather("Coluna", "Valor", 1:5)

head(dados_tidy)

##   Coluna Valor
## 1   Col1     4
## 2   Col1     6
## 3   Col1    10
## 4   Col1     7
## 5   Col1     5
## 6   Col2     5

dados_tidy %>% 
  ggplot(aes(Coluna, Valor)) +
  geom_bar(stat = "identity")

inserir a descrição da imagem aqui

dados_tidy %>% 
  filter(Coluna %in% c("Col1","Col3", "Col5")) %>%
  ggplot(aes(Coluna, Valor)) +
  geom_bar(stat = "identity")

inserir a descrição da imagem aqui

1


I’m not sure I understand the format of your data. It seems that you have some columns filled with counts of people in different observations, and want the sum of each column in the graph. If so, it’s quite simple:

First, I created some random data:

dados <- replicate(5, sample(1:10, 5, TRUE))
colnames(dados) <- paste0("Col", 1:5)
dados
#     Col1 Col2 Col3 Col4 Col5
#[1,]    9    3    1    8    8
#[2,]    3    9    3    5   10
#[3,]    4   10    2    8    3
#[4,]    6    7    7   10    7
#[5,]   10    7    4    4    2

To make the graph, simply plot the sum of the columns:

barplot(colSums(dados))

inserir a descrição da imagem aqui

  • That’s exactly it. I just forgot to mention one thing. This table has several columns and I want to select just a few of them. Where I specify it?

  • @Gilbertokreisler You can specify the columns by name, for example: barplot(colSums(dados[,c("Col1", "Col3")])) or in order: barplot(colSums(dados[,c(1, 3)])). You can also calculate the sum of all columns and then take only the values of interest. The result does not change, but the logic does: barplot(colSums(dados)[c("Col1", "Col3")])or barplot(colSums(dados)[c(1,3)]).

  • When I used the second logic gave a mistake: Error in colSums(date) : 'x' must be numeric' . In the first logic, which works, when plotting the graph a bar appears without the name of the referring column and in others it is the opposite. I don’t know if the bars that don’t appear have to do with the disparity of values, if so, is there anything you can do to make them appear? Thank you so much for your help

  • @Gilbertokreisler The error must have occurred because you have text columns too, so the ideal is to use the first alternative. The names not appearing are probably just for lack of space. Try to see the chart in a larger window and the problem should be solved.

Browser other questions tagged

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