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

Time:04-08

I want to draw a combined bar plot, so that I can make comparision among different score types.

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")

Here is one of the barplot I draw, and this is the type of "precision", however, I want to make a wide bar plot, with all the models under 4 score types sharing the same Y-axis, also with subtitle if possible.

CodePudding user response:

Your code throws an error, because evaluation_4model is not defined.

However, the answer to your problem is likely to make a faceted plot and hence melt the data to a long format. To do this, I usually make use of the reshape library. Tweaking your code looks like this

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

enter image description here

does that help?

  • Related