R 多个分类变量的ggplot2条形图
我想创建一个条形图,显示具有相同因子的不同变量。数据集是这样的R 多个分类变量的ggplot2条形图,r,ggplot2,bar-chart,factors,R,Ggplot2,Bar Chart,Factors,我想创建一个条形图,显示具有相同因子的不同变量。数据集是这样的 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 2
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
我想创建一个这样的条形图
我创建了另一个条形图,显示了我想要的所有信息,但它只有一个变量:
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"))
结果是这样的
感谢您的帮助根据我的评论,以下是您应该如何获得所需的堆叠条形图 当前,您的数据如下所示:
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
看起来是这样的:
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(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))
library(重塑2)
图书馆(GG2)
#加载数据
数据%摘要(N=N())->Dat1
#密谋
ggplot(Dat1,aes(x=变量,y=N,fill=值,label=N))+
几何图形栏(position=“stack”,stat=“identity”)+
geom_文本(大小=3,位置=位置_堆栈(vjust=0.5))
您需要更改数据结构-每行一个观察值,每列一个变量。您的新变量将是ID、软件(级别:Word、Excel、PowerPoint、电子邮件)和熟练程度(级别:未使用、基本、…、高级)。您可以使用tidyr包整理数据。然后,您可以使用x=软件和fill=熟练度创建几何图形栏。您不需要position=“dodge”就可以获得链接中提供的条形图类型。感谢您的帮助非常感谢您的回答,它完成了工作。这正是我想要的