用于多个分类变量的 ggplot2 barplot
ggplot2 barplot for several categorical variables
我想创建一个显示具有相同因子的不同变量的条形图。数据集是这样的
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
我想创建一个这样的条形图
barplot
我创建了另一个显示我想要的所有信息的条形图,但它只有一个变量:
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"))
结果是这样的
Barplot with one variable and grouped by Sex
感谢您的帮助
根据我的评论,您应该按照以下步骤获得所需的堆积条形图。
目前您的数据如下所示:
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
您可以将其整理为每行一个观察值,每列一个变量:
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
然后您可以绘制条形图:
library(ggplot2)
ggplot(df_tidy, aes(x = software, fill = value)) +
geom_bar()
看起来像这样:
然后您可以根据需要使用轴标签和小平面。
你可以试试这个。希望这能有所帮助。
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))
我想创建一个显示具有相同因子的不同变量的条形图。数据集是这样的
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
我想创建一个这样的条形图 barplot
我创建了另一个显示我想要的所有信息的条形图,但它只有一个变量:
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"))
结果是这样的 Barplot with one variable and grouped by Sex 感谢您的帮助
根据我的评论,您应该按照以下步骤获得所需的堆积条形图。
目前您的数据如下所示:
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
您可以将其整理为每行一个观察值,每列一个变量:
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
然后您可以绘制条形图:
library(ggplot2)
ggplot(df_tidy, aes(x = software, fill = value)) +
geom_bar()
看起来像这样:
然后您可以根据需要使用轴标签和小平面。
你可以试试这个。希望这能有所帮助。
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))