如何使用ggplot2在R中绘制包含不同组的条形图?

How to draw bar plot including different groups in R with ggplot2?

我想画一个组合条形图,方便不同分数类型的比较

compare_data = data.frame(model=c(lr,rf,gbm,xgboost),
precision=c(0.6593,0.7588,0.6510,0.7344),
recall=c(0.5808,0.6306,0.4897,0.6416),f1=c(0.6176,0.6888,0.5589,0.6848),
acuracy=c(0.6766,0.7393,0.6453,0.7328))

compare1 <- ggplot(evaluation_4model, aes(x=Model, y=Precision)) + 
  geom_bar(aes(fill = Model), stat="identity")
compare1 <- compare+labs(title = "Precision")

这是我绘制的条形图之一,这是“精度”类型,但是,我想制作一个宽条形图,4种分数类型下的所有模型共享相同的Y轴,如果可能的话还有字幕。

您的代码抛出错误,因为 evaluation_4model 未定义。

但是,您的问题的答案可能会生成多面图,从而将数据融合为长格式。为此,我通常使用重塑库。调整您的代码如下所示

library(ggplot2)
library(reshape2)

compare_data = data.frame(model=c("lr","rf","gbm","xgboost"),
                          precision=c(0.6593,0.7588,0.6510,0.7344),
                          recall=c(0.5808,0.6306,0.4897,0.6416),
                          f1=c(0.6176,0.6888,0.5589,0.6848),
                          acuracy=c(0.6766,0.7393,0.6453,0.7328))

plotdata <- melt(compare_data,id.vars = "model")

compare2 <- ggplot(plotdata, aes(x=model, y=value)) + 
  geom_bar(aes(fill = model), stat="identity")+
  facet_grid(~variable)

compare2

有帮助吗?