0
votes

My goal is to create a simple boxplot using the ggplot function. I've imported the data from an excel file using read_excel. The data consists of 4 columns (corresponding to treatment) and for each row/treatment, there are around 600 values (see screenshot of head of data.frame in R). For ggplot however, i don't have a clue what are the x/y names of my data frame to put in the aes() argument and I don't know how to create x/y names for the aes function.

I just want to know what the x and y values to put in aes()...or how to define them if I haven't yet. So far, code is:

Library(readxl)
Library(ggplot2)

CM<-read_excel(file.choose(new=FALSE))
CM<-data.frame(CM)

So for the graph: ggplot(CM, aes(??,??)

Didn't get very far...

enter image description here

1

1 Answers

1
votes

Your data is not the format supported by ggplot2, which follows the grammar of graphics. However, you can transform your data frame using tidyr, another part of tidyverse (of which ggplot2 is a part). You'd have to use gather(), like so:

library(tidyr)
library(ggplot2)

CM2 <- CM %>%
  gather(key = "Names", value = "Values") # You get to choose these names; choose wisely. You'll use them later

ggplot(CM2, aes(x = Names, y = Values))+
  geom_boxplot()

I wrote the least possible unnecessary code. Also, I created a second data.frame called CM2 because I don't know if you use the original later, but you can just write CM <- CM %>% [...] if you prefer.

You can find some reference on tidyr here.