如何解决创建分组的条形图以在R中进行模型比较
我在创建分组条形图以可视化两个模型的性能时遇到问题。我已经将来自k折交叉验证的数据存储到名为modelcomp的数据框中:
npm config set ignore-scripts false
我想要达到的结果尽可能类似于以下内容(使用excel制作)
。
感谢您的帮助!
解决方法
尝试一下:
library(tidyverse)
#Data
df <- data.frame(n_comp=c('20 components','5 components'),Accuracy=c(0.7754178,0.7716294),Sens=c(0.8006006,0.7783033),Spec=c(0.7548485,0.7663636),stringsAsFactors = F)
#Melt
df1 <- df %>% pivot_longer(cols = -n_comp)
#Plot
ggplot(df1,aes(x=name,y=value,fill=n_comp,label=paste0(100*round(value,3),'%')))+
geom_bar(stat='identity',position = 'dodge')+
geom_text(position = position_dodge(0.9),vjust=-0.25)+
theme(legend.position = 'bottom')
输出:
,使用@Duck的数据框。这将构建基本的图形条形图:
out <- barplot(as.matrix(df[,-1]),ylab="Value",ylim=c(0,.9),main="Model Comparison",beside=TRUE,col=c("blue","dark orange"))
legend(2.5,-.07,df$n_comp,ncol=2,xpd=NA,fill=c("blue","dark orange"),bty="n")
x <- as.vector(out)
y <- unlist(df[,-1])
pct <- paste(round(unlist(df[,-1])*100,1),"%")
text(x,y,pct,pos=3,xpd=NA)
,
够近了吗?
library(ggplot2)
library(cowplot)
library(dplyr)
library(reshape2)
df <- data.frame(n_comp=c('20 components',0.7663636))
reshape2::melt(df,id.vars = "n_comp") %>%
mutate(n_comp = relevel(factor(n_comp),"5 components")) %>%
ggplot(aes(x=variable,label = scales::percent(value,accuracy=.1),width=.6)) +
geom_bar(stat='identity',position=position_dodge2(width=.8,padding=.2)) +
geom_text(position = position_dodge(.6),vjust=-0.5,size=5) +
theme_minimal_hgrid()+
theme(legend.position = 'bottom',legend.title = element_blank(),text = element_text(size = 18),axis.text = element_text(size = 18),plot.title = element_text(hjust = 0.5)) +
labs(x=NULL,y=NULL,title = "Model Comparison")+
coord_cartesian(ylim = c(.6,.85)) +
scale_fill_manual(values = c("#4472c4","#eb7c31"))
由reprex package(v0.3.0)于2020-08-17创建
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。