Overlay mustache or error strings on ggplot

I am creating graphs similar to the first example image below, and I need graphs similar to the second example below.

library(ggplot2)
library(scales)

# some data
data.2015 = data.frame(score = c(-50,20,15,-40,-10,60),
                       area = c("first","second","third","first","second","third"),
                       group = c("Findings","Findings","Findings","Benchmark","Benchmark","Benchmark"))

data.2014 = data.frame(score = c(-30,40,-15),
                       area = c("first","second","third"),
                       group = c("Findings","Findings","Findings"))

# breaks and limits
breaks.major = c(-60,-40,-22.5,-10, 0,10, 22.5, 40, 60)
breaks.minor = c(-50,-30,-15,-5,0, 5, 15,30,50) 
limits =c(-70,70)

# plot 2015 data
ggplot(data.2015, aes(x = area, y = score, fill = group)) +
  geom_bar(stat = "identity", position = position_dodge(width = 0.9)) +
  coord_flip() +
  scale_y_continuous(limit = limits, oob = squish, minor_breaks = breaks.minor, breaks = breaks.major)

sample chart

.2014 data is only relevant for the Results group. I would like to show these 2014 output values ​​on a chart in the corresponding / corresponding data area .2015 $, where 2014 data is available.

To display data for the last year only according to the "Search" data (red bars), I would like to use a one-sided error / mustache, which proceeds from the value of the corresponding data bar .2015, and ends with data.2014, for example:

perfect plot

, , 2015 , , 2014 abs() 2015 , , .

:

  • , errorbar/whisker , , , .
  • ,
  • , , . 2014 ( ) , ( , ).

, , , geom_linerange, , geom_errorbar, ymin ymax , ggplot geom_bar! .

+4
2

, , , . .

alldat = merge(data.2015, data.2014, all = TRUE, by = c("area", "group"), 
            suffixes = c(".2015", ".2014"))

, , ymin , y NA . , , plotscore, .

alldat$plotscore = with(alldat, ifelse(is.na(score.2014), NA, score.2015))

, , direction, 2015 2014 . Benchmark , .

alldat$direction = with(alldat, ifelse(score.2015 < score.2014, "dec", "inc"))
alldat$direction[is.na(alldat$score.2014)] = "absent"

, , :

    area     group score.2015 score.2014 plotscore direction
1  first Benchmark        -40         NA        NA    absent
2  first  Findings        -50        -30       -50       dec
3 second Benchmark        -10         NA        NA    absent
4 second  Findings         20         40        20       dec
5  third Benchmark         60         NA        NA    absent
6  third  Findings         15        -15        15       inc

, , :

ggplot(alldat, aes(x = area, y = score.2015, fill = group)) +
    geom_bar(stat = "identity", position = position_dodge(width = 0.9)) +
    geom_errorbar(aes(ymin = plotscore, ymax = score.2014, color = direction), 
                position = position_dodge(width = .9), lwd = 1.5, show.legend = FALSE) +
    coord_flip() +
    scale_y_continuous(limit = limits, oob = squish, minor_breaks = breaks.minor, breaks = breaks.major) +
    scale_color_manual(values = c(NA, "red", "green"))

enter image description here

ggplot2, ggplot2_1.0.1.9002 show_guide show.legend, geom_errorbar.

, , , , , .

+1

, :

    library(ggplot2)
    library(scales)

    # some data
    data.2015 = data.frame(score = c(-50,20,15,-40,-10,60),
                           area = c("first","second","third","first","second","third"),
                           group = c("Findings","Findings","Findings","Benchmark","Benchmark","Benchmark"))

    data.2014 = data.frame(score = c(-30,40,-15),
                           area = c("first","second","third"),
                           group = c("Findings","Findings","Findings"))

    # breaks and limits
    breaks.major = c(-60,-40,-22.5,-10, 0,10, 22.5, 40, 60)
    breaks.minor = c(-50,-30,-15,-5,0, 5, 15,30,50) 
    limits =c(-70,70)

    # reconfigure data to create values for the additional errorbar/linerange
    alldat = merge(data.2015, data.2014, all = TRUE, by = c("area", "group"), 
                suffixes = c(".2015", ".2014"))
    alldat$plotscore = with(alldat, ifelse(is.na(score.2014), NA, score.2015))
    alldat$direction = with(alldat, ifelse(score.2015 < score.2014, "dec", "inc"))
    alldat$direction[is.na(alldat$score.2014)] = "absent"

    ggplot(alldat, aes(x = area, y = score.2015, fill = group)) +
    geom_bar(stat = "identity", position = position_dodge(width = 0.9)) +

    # set the data min and max as the same to have a single 'cap' with no line
    geom_errorbar(aes(ymin = score.2014, ymax = score.2014, color = direction), 
                    position = position_dodge(width = .9), lwd = 1.5, show.legend = FALSE) +
    #then add the line
    geom_linerange(aes(ymin = score.2015, ymax = score.2014, color = direction), 
                    position = position_dodge(width = .9), lwd = 1.5, show.legend = FALSE) +
    coord_flip() +
    scale_y_continuous(limit = limits, oob = squish, minor_breaks = breaks.minor, breaks = breaks.major) +
    scale_color_manual(values = c(NA, "red", "green"))
0

All Articles