0

I am thinking how to present partial values in whisker plot/... M2 has only the max. Both measurements do not hav Code which output in Fig. 1

library("reshape2")
library("ggplot2")

ds <- structure(list(Vars = c("M1", "M2", "M1", "M2", "M1", "M2"), 
variable = structure(c(1L, 1L, 2L, 2L, 3L, 3L), .Label = c("Max", 
"Ave", "Min"), class = "factor"), value = c("150", 
"61", " 60", NA, " 41", NA)), row.names = c(NA, -6L), .Names = c("Vars", 
"variable", "value"), class = "data.frame")

# http://stackoverflow.com/q/44100187/54964 eipi10
ds$value = as.numeric(ds$value)

# http://stackoverflow.com/a/44090815/54964
minmax <- ds[ds$variable %in% c("Min","Max"), ]
absol  <- ds[ds$variable %in% c("Ave"), ]
# absol  <- ds[ds$variable %in% c("Ave", "Absolute"), ]
minm   <- dcast(minmax, Vars ~ variable)
absol  <- merge(absol, minm, by = "Vars", all.x = T)

absol

ggplot(absol, aes(x = Vars, y = value, fill = variable)) +
        geom_bar(stat = "identity") +
        geom_errorbar(aes(ymin = Min, ymax = Max), width = .25)

Values at start

            Max      Ave       Min Vars
M1          150       60        41   M1
M2           61     <NA>      <NA>   M2

Fig. 1 Output where no visualisations when only max value exists

enter image description here

The presentation of M1 is also weird in the barplot becuase no absolute values in data, designed initially in absol.

Expected output: mark maximum value in M2 presentation

OS: Debian 8.7
R: 3.4 (backports)

Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697
  • 1
    `value` is a character variable in your data, so it's being treated as a factor in the plot. When you create `ds` don't put quotes around the numbers in `value`, so that `value` will be numeric. Or you can do `ds$value = as.numeric(ds$value)`. – eipi10 May 21 '17 at 18:56
  • @eipi10 I added the fix in the body. The y-axis looks now ok, but otherwise, the same situation persists. What do you think? – Léo Léopold Hertz 준영 May 21 '17 at 19:30

1 Answers1

1

Add a column to absol, call it yMin, that will set the min value to the max value if the min value is missing.

absol$yMin <- ifelse(is.na(absol$Min), absol$Max, absol$Min)

Then, when plotting, have the geom_errorbar use yMin in the aesthetics.

ggplot(absol, aes(x = Vars, y = value, fill = variable)) +
        geom_bar(stat = "identity") + 
        geom_errorbar(aes(ymin = yMin, ymax = Max), width = .25)

enter image description here

Peter
  • 7,460
  • 2
  • 47
  • 68
  • I think this is the right answer to the question. I think the next step is to improve the initial data structure like done here https://stackoverflow.com/q/44030346/54964 I will be thinking about it soon. – Léo Léopold Hertz 준영 May 24 '17 at 13:48