Two boxplot from a dataset, using different rows

Dear all,
I am a student who is using R for university, and I'm a beginner, even in this community, so I hope I have posted the question correctly.

I have a dataset, and I would like to create a graph with two boxplots that show and compare the differences in the data. But I would like that the first boxplot is only with the rows 1 to 20 of my dataset, belonging to column 5, while the second boxplot, is only with the rows 21 to 50, always belonging of column 5.

I can't draw this graph. Is it possible? What commands should I use?

Thanks so much for your time

Here is an example where the data frame has 50 rows. The boxplots use the data from the third column. The first column, called Name, has the value A in the first 20 rows and the value B in rows 21 - 50. ggplot makes separate box plots for each Name.

#invent some data
DF <- data.frame(Name = c(rep("A", 20), rep("B", 30)), B = rnorm(50), C = runif(50))
ggplot(DF, aes(x = Name, y = C)) + geom_boxplot()

Created on 2020-08-20 by the reprex package (v0.3.0)

Dear FJCC,
thank you very much for your response and availability. In these days I have tried to follow your advice and do something with ggplot, but I did not succeed, I don't undestrand sorry.

I tried to make a drawing of a dataset for example.

If for example I wanted to build in this case, the two boxplots, the first for the variable "experiment 1" with the data of column 8, and rows 3 and 4, while the second comparison boxplot, with the data of rows 5 and 6 (for the "experiment 2"), how should I use the command?

Thanks a lot, sorry

Given that you are a self declared beginner. I suggest we focus on making sure you have data in your R environment from which a ggplot2 graph might be made...

Have you imported your data, such that it is a data.frame in R, with a name we can refer to ?
or do you need help with that ? if so , in what form is your data currently ?

Thank you very much, very kind! My data was in excel format, so I imported it in R in csv format. The name I gave to the dataframe is "d". I used this commands:

setwd ('andress of my dataset')
d <- read.csv('namefile.csv', header = T, sep = ';')
d <- d[,1:21]

Then I ran some tests, and I did some boxplots (not using ggplot), but now, having to compare data on different rows, I got stuck.

I installed ggplot2, using these commands:


Specifically of my dataset, now I should make a boxplot with rows 1 to 26 of column 20 ("experiment 1"), with a second boxplot with the rows 27 to 54 ("experiment 2"), always of column 20, to compare the two treatments.

If the labeling of the row as belonging to experiment 1 or 2 is in Column3, try

ggplot(d[1:54, ], aes(x = Column3, y = Column20, color = Column3)) + geom_boxplot()

I assumed the column names are Column1, Column2, etc. as in your original post.

Dear all,
sorry for the late of my reply, I had some inconvenience. Thanks so much for your help, I really appreciat, very kind. FJCC your last command works perfectly. Thanks so much! :slight_smile:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.