How to ggplot partial values ​​with max only on the mustache plot?

I am thinking how to represent partial values ​​in the whisker blank / ... M2

only has max. Both measurements do not have a code that is displayed in Figure 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)

      

Startup values

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

      

fig. 1 Conclusion where there is no visualization with only the maximum value

enter image description here

The M1 presentation is also odd in the bar chart because there are no absolute values ​​in the data originally designed in absol

.

Expected Result: Mark the maximum value in the presentation M2

OS: Debian 8.7
R: 3.4 (backports)

0


source to share


1 answer


Add a column in absol

, name it yMin

that will set the min value to the max value if min value is missing.

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

      

Then, when plotting, use geom_errorbar

in yMin

for aesthetic use.



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

+1


source







All Articles