I want to plot a plot in r as sketched manually like below:
The data to plot the expected plot above is as this:
# Logistic Regression 3-fold cross validation scores for Methods 1,2 and 3
Method1_LR <- c(40,50,60)
Method2_LR <- c(70,50,60)
Method3_LR <- c(30,50,40)
# Random Forest 3-fold cross validation scores for Methods 1,2 and 3
Method1_RF <- c(70,50,60)
Method2_RF <- c(40,50,60)
Method3_RF <- c(30,50,40)
For each method (there are 3) 3-fold cross validation scores for Logistic Regression (denoted with LR on plot) and Random Forest (denoted with RF on plot) are plotted. The aim is to plot cross validation scores for each method by plotting the min, max and mean value of cross validation scores for each. In the figure we can see for Method_1 in Logistic Regression setting, the minimum value is 40 and the maximum value is 60 and all 3 values's mean happens to be 50. The mean value is shown with a square for Method 1 and the min and max values are the tips of the lines extending from the square. For methods 1,2 and 3 the markings on the plot should be apart on the x-axis but not too apart that the markings will look on the Random Forest results side of the graph. Same goes for the RF side of the plot.
We can get the values of all the 'Method' objects from the ls
with mget
as a list
, convert the list
to two column dataset (enframe
), separate
the column 'name' into two, use geom_boxplot
from ggplot2
library(dplyr)
library(tidyr)
library(ggplot2)
library(tibble)
mget(ls(pattern='^Method\\d+_[A-Z]+$')) %>%
enframe %>%
unnest(value) %>%
separate(name, into = c("method", "cat"), sep="_") %>%
ggplot(aes(x = cat, y = value, fill = method)) +
geom_boxplot() +
theme_bw()
-output