0

I am still fairly new to R. Please can someone help me with a query.

How do I complete individual box plots on my data? I need to compare the positive against the negative for each row. There are 195 columns.

The data look like this: (The positive is under the negative, so two row and up to 195 columns, and I need to boxplot each column)

                     1    2    3    4    5     6   7   etc 
Negative            1.1  2.1  2.2  3.1  5.66 8.99 5.11 etc
Positive            2.1  5.6  5.7  3.0  6.1  8.1  6.2  etc

Thanks in advance

  • boxplot for each column? There are only two numbers for each column. How can you plot a boxplot for just two numbers? I assume you want a boxplot for Positive as one group and Negative as the other group. Please see my answer. – www Jun 28 '18 at 13:45
  • Hello. Thanks for your reply. Yes I want to boxplot for positive and negative. – Alyce Belle Jun 28 '18 at 14:51

2 Answers2

1
require(tidyverse) # dplyr() and ggplot2()
# your data
data <- data.frame(negative = c( 1.1,  2.1,  2.2,  3.1,  5.66, 8.99, 5.11),
               positive = c(2.1,  5.6,  5.7,  3.0,  6.1,  8.1,  6.2))
# we gather to have one long column with values and one with the "class"
data <- data %>% gather(class) # you can name here the class column
# use ggplot to plot the data
ggplot(data, aes(y = value, x = class)) +
  geom_boxplot()

Please provide a reproducible example text time, see this: How to make a great R reproducible example?

RLave
  • 8,144
  • 3
  • 21
  • 37
1

Your data frame is in wide format, which is difficult to work with. We can first convert it to long format.

library(tidyverse)

dat2 <- dat %>%
  rownames_to_column() %>%
  gather(Column, Value, -rowname)

After that, you can use the ggplot2 package to plot the data as Riccardo Lavelli suggested. Here I showed another option, which is the bwplot function from the lattice package.

library(lattice)

bwplot(Value ~ rowname, dat2)

enter image description here

You can also consider to use the base R boxplot function.

boxplot(Value ~ rowname, dat2)

enter image description here

You can also consider the ggboxplot from the ggpubr package.

library(ggpubr)

ggboxplot(dat2, x = "rowname", y = "Value")

enter image description here

DATA

dat <- read.table(text = "                     1    2    3    4    5     6   7 
Negative            1.1  2.1  2.2  3.1  5.66 8.99 5.11
Positive            2.1  5.6  5.7  3.0  6.1  8.1  6.2",
                  header = TRUE)
www
  • 38,575
  • 12
  • 48
  • 84