Plot Chart Multiple Bars Variables in R

2

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

    
asked by anonymous 19.04.2016 / 20:59

2 answers

1

I'm not sure I understand the format of your data. It looks like you have some columns populated with people counts in different observations, and you want the sum of each column in the chart. 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))

    
20.04.2016 / 01:55
1

An alternative to the @Molx 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")

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

    
10.05.2016 / 15:42