Why this CSV data complicates with ggplot2 whisker plot?

Viewed 238

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

enter image description here enter image description here

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

1 Answers
Related