Search code examples
rggplot2latticedata-analysisrgraph

Reproducing Excel Graphics with some annotations in R?


I'm performing Descriptive analysis in Excel these days. But being a R learner i want to reproduce (approx.) the Excel graphics in R by using R's own coding, packages, etc. Here is an example of data: dummy data

and Excel Graph is

Excel output of above data

Being a beginner at learning of R my question is pretty simple: "How to produce this excel graph in R by using base plotting, lattice or ggplot2 whichever is appealing?" Any help would be highly appreciated!!!


Solution

  • Your question is a multi-part question depending upon which elements of the plot are important. Here is a method to reproduce this figure using ggplot2.

    First, I create a reproducible dataset:

    df <- data.frame(
    Group1 = factor(rep(c("A", "Fially", "AC"), each = 3),  
        levels = c("A", "Fially", "AC")),
    Group2 = factor(c("B", "GGF", "Kp"), 
        levels = c(c("B", "GGF", "Kp"))),
    Value = c(100, 5, 6, 200, 42, 21, 300, 80, 15)
    )
    

    Note that you will need to reorder your factors (see Reorder levels of a factor without changing order of values for more help with this if you need it).

    Second, I plot the data using ggplot2 using a bar-plot (see the documentation here).

    library(ggplot2)
    ggOut <- ggplot(data = df, aes(x = Group1, 
                    y = Value, fill = Group2)) +
             geom_bar(stat="identity", position="dodge") +
             theme_bw() + 
             ylab("") +
             xlab("") + 
             scale_fill_manual(name = "", 
                               values = c("red", "blue", "black")) 
    
    print(ggOut)
    ggsave(ggOut)
    

    This code gives you this figure: enter image description here

    To change the legend, I followed this guide.