在R中创建用于模型比较的分组条形图
我在创建分组条形图以可视化两个模型的性能时遇到问题。我已将来自k折叠交叉验证的数据存储到名为modelcomp的数据框中:在R中创建用于模型比较的分组条形图,r,plot,graph,bar-chart,R,Plot,Graph,Bar Chart,我在创建分组条形图以可视化两个模型的性能时遇到问题。我已将来自k折叠交叉验证的数据存储到名为modelcomp的数据框中: modelcomp n_comp Accuracy Sens Spec 1 20 components 0.7754178 0.8006006 0.7548485 2 5 components 0.7716294 0.7783033 0.7663636 我希望得到的结果与以下结果尽可能相似(用excel制作) 谢谢你的帮助 试试
modelcomp
n_comp Accuracy Sens Spec
1 20 components 0.7754178 0.8006006 0.7548485
2 5 components 0.7716294 0.7783033 0.7663636
我希望得到的结果与以下结果尽可能相似(用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')
库(tidyverse)
#资料
df试试这个:
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')
库(tidyverse)
#资料
df使用@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)
out使用@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)
out距离够近吗
库(ggplot2)
图书馆(cowplot)
图书馆(dplyr)
图书馆(E2)
df%
变异(n_comp=relevel(因子(n_comp),“5个成分”))%>%
ggplot(aes(x=变量,y=值,填充=n_comp,
标签=刻度::百分比(值,精度=.1),宽度=.6))+
几何图形栏(stat='identity',position=position_-dodge2(宽度=0.8,填充=0.2))+
几何图形文字(位置=位置减淡(.6),矢量=-0.5,大小=5)+
theme_minimal_hgrid()+
主题(legend.position='bottom',
legend.title=元素_blank(),
文本=元素\文本(大小=18),
axis.text=元素\文本(大小=18),
plot.title=元素\文本(hjust=0.5))+
实验室(x=NULL,y=NULL,title=“模型比较”)+
坐标笛卡尔(ylim=c(.6,.85))+
刻度-填充-手动(值=c(“#4472c4”),“#eb7c31”))
由(v0.3.0)于2020年8月17日创建,距离足够近吗
库(ggplot2)
图书馆(cowplot)
图书馆(dplyr)
图书馆(E2)
df%
变异(n_comp=relevel(因子(n_comp),“5个成分”))%>%
ggplot(aes(x=变量,y=值,填充=n_comp,
标签=刻度::百分比(值,精度=.1),宽度=.6))+
几何图形栏(stat='identity',position=position_-dodge2(宽度=0.8,填充=0.2))+
几何图形文字(位置=位置减淡(.6),矢量=-0.5,大小=5)+
theme_minimal_hgrid()+
主题(legend.position='bottom',
legend.title=元素_blank(),
文本=元素\文本(大小=18),
axis.text=元素\文本(大小=18),
plot.title=元素\文本(hjust=0.5))+
实验室(x=NULL,y=NULL,title=“模型比较”)+
坐标笛卡尔(ylim=c(.6,.85))+
刻度-填充-手动(值=c(“#4472c4”),“#eb7c31”))
创建于2020-08-17,由(v0.3.0)尝试条形图(as.matrix(d[-(1:2)]),旁边=真)
尝试条形图(as.matrix(d[-(1:2)]),旁边=真)
。谢谢,太棒了!谢谢,太好了!