Reputation: 37
I would like to create a barplot that shows different variables with the same factors. The dataset is like this
ID;Word;Excel;Power Point;Correo electronico
11;Intermedio;Intermedio;Intermedio;Intermedio
25;Intermedio;Intermedio;Intermedio;Intermedio
26;Básico;No la he utilizado;Básico;Básico
27;Básico;Básico;Básico;Intermedio
29;Intermedio;Intermedio;Intermedio;Avanzado
33;Avanzado;Intermedio;Avanzado;Avanzado
37;Avanzado;Básico;Intermedio;Avanzado
39;Intermedio;Intermedio;No la he utilizado;Intermedio
43;Intermedio;Intermedio;Intermedio;Intermedio
51;Avanzado;Básico;Intermedio;Avanzado
53;Intermedio;Intermedio;Intermedio;Intermedio
54;Intermedio;Intermedio;Básico;Avanzado
60;Intermedio;Intermedio;Intermedio;Intermedio
I would like to create a barplot like this barplot
I have created another barplot that shows all the information as I wanted but it is just with one variable:
ggplot(aes(x = Tablet,
y = prop.table(stat(count)),
fill = factor(Tablet),
label = scales::percent(prop.table(stat(count)), accuracy = 0.01))) +
scale_fill_manual(values = CColors)+
geom_bar(position = "dodge") +
geom_text( aes(label = scales::percent((..count..)/sum(..count..), accuracy = 0.01),
y= ((..count..)/sum(..count..))), stat="count",
vjust = -0.5,
size = 3,
hjust=1)+
scale_y_continuous(labels = scales::percent) +
labs(y = 'Frecuencia relativa', fill = 'Uso de Tablet')+
facet_wrap( ~ Sexo, nrow = 1)+
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank())+ ggtitle(paste("Uso de Tablet para acceder a clases virtuales \nTotal: ", nrow(das), " Encuestados"))
The result is this Barplot with one variable and grouped by Sex Thanks for your help
Upvotes: 1
Views: 2086
Reputation: 39595
You can try this. Hoping this can help.
library(reshape2)
library(ggplot2)
#Load your data
Data <- structure(list(ID = c(11L, 25L, 26L, 27L, 29L, 33L, 37L, 39L,
43L, 51L, 53L, 54L, 60L), Word = structure(c(3L, 3L, 2L, 2L,
3L, 1L, 1L, 3L, 3L, 1L, 3L, 3L, 3L), .Label = c("Avanzado", "Básico",
"Intermedio"), class = "factor"), Excel = structure(c(2L, 2L,
3L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L), .Label = c("Básico",
"Intermedio", "No la he utilizado"), class = "factor"), Power.Point = structure(c(3L,
3L, 2L, 2L, 3L, 1L, 3L, 4L, 3L, 3L, 3L, 2L, 3L), .Label = c("Avanzado",
"Básico", "Intermedio", "No la he utilizado"), class = "factor"),
Correo.electronico = structure(c(3L, 3L, 2L, 3L, 1L, 1L,
1L, 3L, 3L, 1L, 3L, 1L, 3L), .Label = c("Avanzado", "Básico",
"Intermedio"), class = "factor")), class = "data.frame", row.names = c(NA,
-13L))
#Melt data
Data.Melt <- melt(Data,id.vars = 'ID')
Data.Melt %>% group_by(variable,value) %>% summarise(N=n()) -> Dat1
#Plot
ggplot(Dat1,aes(x=variable,y=N,fill=value,label=N))+
geom_bar(position="stack", stat="identity")+
geom_text(size = 3, position = position_stack(vjust = 0.5))
Upvotes: 2
Reputation: 825
As per my comment, here is what you should do to get the stacked bar plot you want.
Currently your data looks like this:
ID Word Excel PowerPoint Correo.electronico
2 11 Intermedio Intermedio Intermedio Intermedio
3 25 Intermedio Intermedio Intermedio Intermedio
4 26 Básico No la he utilizado Básico Básico
5 27 Básico Básico Básico Intermedio
6 29 Intermedio Intermedio Intermedio Avanzado
7 33 Avanzado Intermedio Avanzado Avanzado
8 37 Avanzado Básico Intermedio Avanzado
9 39 Intermedio Intermedio No la he utilizado Intermedio
10 43 Intermedio Intermedio Intermedio Intermedio
11 51 Avanzado Básico Intermedio Avanzado
12 53 Intermedio Intermedio Intermedio Intermedio
13 54 Intermedio Intermedio Básico Avanzado
14 60 Intermedio Intermedio Intermedio Intermedio
You can tidy it to have one observation per row and one variable per column:
library(tidyr)
df_tidy <- df %>% pivot_longer(-ID, names_to = "software")
df_tidy
# A tibble: 52 x 3
ID software value
<chr> <chr> <chr>
1 11 Word Intermedio
2 11 Excel Intermedio
3 11 PowerPoint Intermedio
4 11 Correo.electronico Intermedio
5 25 Word Intermedio
6 25 Excel Intermedio
7 25 PowerPoint Intermedio
8 25 Correo.electronico Intermedio
9 26 Word Básico
10 26 Excel No la he utilizado
# … with 42 more rows
You can then plot your bar plot:
library(ggplot2)
ggplot(df_tidy, aes(x = software, fill = value)) +
geom_bar()
It looks like this:
You can then play with the axis labels and facet as required.
Upvotes: 1