R 如何制作混淆矩阵的堆叠条形图
我有两个分类变量,我想通过交叉列表进行比较。我制作了一个扩展方形列联表的虚拟示例,其中X的类别在表的行中,而Y的相同类别序列在表的列中。该表总结了X和Y之间的关联 该表的对角线条目给出了X类别与Y类别匹配的观察数,在这种情况下,观察数为该类别的点击数。对于X类别,每个非对角条目都是假警报,而对于Y类别,则是未命中 我想制作一个类似于graph1的堆叠条形图,用来自列(Y变量)的填充颜色显示表格的每一行。最后两个条形图显示每个类别的未命中和错误报警 我设法画了两张独立的图表。下面的代码生成图1的前四行R 如何制作混淆矩阵的堆叠条形图,r,ggplot2,bar-chart,data-visualization,factor,R,Ggplot2,Bar Chart,Data Visualization,Factor,我有两个分类变量,我想通过交叉列表进行比较。我制作了一个扩展方形列联表的虚拟示例,其中X的类别在表的行中,而Y的相同类别序列在表的列中。该表总结了X和Y之间的关联 该表的对角线条目给出了X类别与Y类别匹配的观察数,在这种情况下,观察数为该类别的点击数。对于X类别,每个非对角条目都是假警报,而对于Y类别,则是未命中 我想制作一个类似于graph1的堆叠条形图,用来自列(Y变量)的填充颜色显示表格的每一行。最后两个条形图显示每个类别的未命中和错误报警 我设法画了两张独立的图表。下面的代码生成图1
# Create Dummy Input
sample.mtxx <- matrix(c(1,0,2,0,0,3,0,3,2,3,3,0,0,0,0,3), nrow = 4)
categories <- c("A","B","C","D")
colnames(sample.mtx) <- paste(categories)
rownames(sample.mtx) <- paste(categories)
# Change from wide to long format
g1.df <- melt(sample.mtx)
# Zero sizes were causing problem so I removed them.
g1.df <- g1.df[g1.df$value!=0,]
# Add a label column to show "Hit".
g1.df$label <- ifelse(g1.df$Var1==g1.df$Var2, "Hit", as.character(""))
# Plotting
plot1 <- ggplot(data=g1.df, mapping=aes(fill=Var2, y=value, x=Var1, label = label))+
geom_bar(width = 0.6, position="stack", stat="identity")+
labs(x="Table Feature", y="Entry size as the number of observations", title="Entry Size") +
geom_text(size = 4, position = position_stack(vjust = 0.5))+
coord_flip()+
theme_bw()+
scale_x_discrete(limit = c("D", "C", "B", "A"))+
scale_y_discrete(limits=seq(0,10,1))+
theme(plot.title = element_text(family = "Times", color = "#353535",
face = "bold", size = 12, hjust = 0.5))+
theme(legend.position = "bottom", legend.title = element_blank())+
theme(
panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank()
)
我对这个情节很满意。但我想要的是将plot1和Plot2放在一个图中,如图1所示。任何人都可以提供如何从不同数据帧绘制堆叠条形图的指导。或者是否有更好的方法制作图形1。“如何排列条形图的组成部分?”
诀窍是使用类型列的levels
属性(例如,这里的` categories')。您需要按顺序将其反转
我想要的是将plot1和Plot2放在一个图中,如图1所示。
如果您只想用您的代码重现图形1
,这是可行的:
#-------------------
#Data wrangling
colnames(g1.df)[1] <-categories; colnames(g1.df)[2] <- variable; #change the names similar to 2nd df
g1.df1.m[,3] <- as.numeric(g1.df1.m[,3]);# changing column type from character to numeric as the correpsonding column in `g1.df` is numeric.
g1.dfCombined <- g1.df %>% bind_rows(g1.df1.m); #merging two dfs.
#this is the part that reverses the order:
g1.dfCombined$categories <- factor(g1.dfCombined$categories, rev(levels(g1.dfCombined$categories)))
#-------------------
#Plotting: (all same except dropped `scale_x_discrete(limit = c("D", "C", "B", "A"))`)
ggplot(data=g1.dfCombined, mapping=aes(fill=categories, y=value, x=variable, label = label))+
geom_bar(width = 0.6, position="stack", stat="identity")+
labs(x="Table Feature", y="Entry size as the number of observations", title="Entry Size") +
geom_text(size = 4, position = position_stack(vjust = 0.5)) +
coord_flip()+theme_bw() + scale_y_discrete(limits=seq(0,10,1)) +
theme(plot.title = element_text(family = "Times", color = "#353535", face = "bold",
size = 12, hjust = 0.5)) +
theme(legend.position = "bottom", legend.title = element_blank()) +
theme(panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())
#-------------------
#数据争用
colnames(g1.df)[1]“如何排列条的组件?”
诀窍是使用类型列的levels
属性(例如,这里的` categories')。您需要按顺序将其反转
我想要的是将plot1和Plot2放在一个图中,如图1所示。
如果您只想用您的代码重现图形1
,这是可行的:
#-------------------
#Data wrangling
colnames(g1.df)[1] <-categories; colnames(g1.df)[2] <- variable; #change the names similar to 2nd df
g1.df1.m[,3] <- as.numeric(g1.df1.m[,3]);# changing column type from character to numeric as the correpsonding column in `g1.df` is numeric.
g1.dfCombined <- g1.df %>% bind_rows(g1.df1.m); #merging two dfs.
#this is the part that reverses the order:
g1.dfCombined$categories <- factor(g1.dfCombined$categories, rev(levels(g1.dfCombined$categories)))
#-------------------
#Plotting: (all same except dropped `scale_x_discrete(limit = c("D", "C", "B", "A"))`)
ggplot(data=g1.dfCombined, mapping=aes(fill=categories, y=value, x=variable, label = label))+
geom_bar(width = 0.6, position="stack", stat="identity")+
labs(x="Table Feature", y="Entry size as the number of observations", title="Entry Size") +
geom_text(size = 4, position = position_stack(vjust = 0.5)) +
coord_flip()+theme_bw() + scale_y_discrete(limits=seq(0,10,1)) +
theme(plot.title = element_text(family = "Times", color = "#353535", face = "bold",
size = 12, hjust = 0.5)) +
theme(legend.position = "bottom", legend.title = element_blank()) +
theme(panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank())
#-------------------
#数据争用
colnames(g1.df)[1]我不想让你泄气,但Graph1确实是你想要的吗?我对分类文献不是非常熟悉,但我发现图表很难阅读。也许更好的选择是气球图,例如:?我不想让你气馁,但图1确实是你想要的吗?我对分类文献不是非常熟悉,但我发现图表很难阅读。也许更好的选择是气球图,例如:?谢谢,我同意你的观点,使用因子是组合这些图的正确方法。但是,组合图的代码不会产生与图1相同的结果。例如,D行仅显示命中和未命中的虚警组件。行C有三个组件,而不是两个。填充颜色对于类别B、C和D都不正确。我还找不到修复方法。有什么想法吗?请检查一下您为plot1
提供的代码,数据框中有一些错误。。。与您实际尝试提供的内容相比。它与绘图1
不匹配,而是生成我粘贴在这里的图形的底部部分(没有的所有内容都未命中,假报警列。另外,您所说的“填充颜色对于类别B也不正确,…”是什么意思?Hello@user29271,你能检查并找到解决方案吗?谢谢,我同意你的观点,使用因子是组合这些图的正确方法。但是,组合图的代码不会产生与图1相同的结果。例如,D行只显示命中和未命中的假警报组件。C行有三个组件,而不是两个。The填充颜色对于类别B、C和D都不正确。我还找不到解决方案。有什么想法吗?请检查您为plot1
提供的代码,数据框中有一些错误…与您实际尝试提供的内容相比。它与plot1
不匹配,而是生成了g的底部我粘贴在这里的raph(没有未命中
和假报警
列的所有内容。另外,您所说的“填充颜色对于类别B也不正确,…”是什么意思?您好@user29271,您能检查并找到解决方案吗?