GerritCalle GerritCalle - 3 months ago 9
R Question

plot performance MSE, RMSE

This dataframe as explained below consists of 4 methods and 3 performance measures for each method. I want to have a barplot for each method similar as this:

https://s31.postimg.org/6ldiguqm3/Schermafbeelding_2016_08_05_om_14_04_08.png

Method MSE RMSE MAE
1 Baseline 42674.68 206.58 149.96
2 Linear Regression 10738.56 103.63 55.85
3 Random forest 4492.47 67.03 37.29
4 Neural Network 7650.72 87.47 57.50


However, I am not able to obtain this with ggplot or something similar. Can someone help me?

Answer

First read in your data

dd = read.table(textConnection("Method MSE RMSE MAE
Baseline 42674.68 206.58 149.96
LinearRegression 10738.56 103.63  55.85
Randomforest  4492.47  67.03  37.29
NeuralNetwork  7650.72  87.47  57.50"), header=TRUE)

Next we need to reshape your data frame to be ggplot2 friendly using reshape2

dd_m = reshape2::melt(dd, c("Method"))

head(dd_m, 2)
#            Method variable value
#1         Baseline      MSE 42675
#2 LinearRegression      MSE 10739

Then we use geom_bar

library(ggplot2)
ggplot(dd_m) + 
  geom_bar(aes(x=variable, y=value, fill=Method), 
              stat="identity", # Don't transform the data
              position = "dodge") # Dodge the bars

enter image description here

Comments