I can reproduce a working ggplot2
boxplot with the test data but not with CSV data in R.
Data visually with single point about the events (sleep and awake)
"Vars" , "Sleep", "Awake"
"Average" , 7 , 12
"Min" , 4 , 5
"Max" , 10 , 15
Data in real life about sleep
"Vars" , "Sleep1", "Sleep2", ...
"Average" , 7 , 5
"Min" , 4 , 3
"Max" , 10 , 8
Data in real life about Awake
"Vars" , "Awake1", "Awake2", ...
"Average" , 12 , 14
"Min" , 10 , 7
"Max" , 15 , 17
Code where data integrated
# only single point!
dat.m <- structure(list(Vars = structure(c(1L, 3L, 2L), .Label = c("Average ",
"Max ", "Min "), class = "factor"), Sleep = c(7, 4, 10
), Awake = c(12L, 5L, 15L)), .Names = c("Vars", "Sleep", "Awake"
), class = "data.frame", row.names = c(NA, -3L))
library('ggplot2')
# works:
str(mpg)
#mpg$class
#mpg$hwy
ggplot(mpg, aes(x = class, y = hwy)) +
geom_boxplot()
# http://stackoverflow.com/a/44031194/54964
m <- t(dat.m)
dat.m <- data.frame(m[2:nrow(m),])
names(dat.m) <- m[1,]
dat.m$Vars <- rownames(m)[2:nrow(m)]
dat.m <- melt(dat.m, id.vars = "Vars")
# TODO complicates here although should not
ggplot(dat.m, aes(x = Vars, y = value, fill=variable)) + #
geom_boxplot()
Test data output in Fig. 1 and Output in Fig. 2.
Fig. 1 Test data output, Fig. 2 Output of the code
Assumption made below for the quartiles:
Code
# http://stackoverflow.com/a/44043313/54964
quartiles <- data.frame(Vars = c("Q1","Q3"), Sleep = c(6,8),
Awake = c(9,13))
I want to set Q1 <- 0.25 * average
and Q3 <- 0.75 * average
.
Assume you have any amount of the main fields (here Sleep
and Awake
).
How can you request the data (here dat.m
) to get min
and max
of each main field?
R: 3.3.3
OS: Debian 8.7
There is
base R
function to make boxplots using the quartiles:bxp()
, but you need 25th, 50th and 75th percentiles known as well as the lower quartile (Q1), the median (Q2) and upper quartile (Q3).For example:
Now using your data: (Edited)
Let us use the first dataset that you introduced:
In you data, the first and third quartiles are missing. The second is also needed, which is the median, but let us assume that it is equal to the mean. I will assume that you have all of them e.g.:
Then sorting your variables:
With ggplot2
We first convert the dataset from 'wide' to 'long' format with
reshape2::melt()
.Then:
You might find interesting these articles: