0

I have created a stacked barplot

ggplot(data %>% count(x, y),
        aes(x, n, fill = factor(y))) + 
geom_bar(stat="identity")+
theme_light()+
theme(plot.title = element_text(hjust=0.5))

enter image description here

there are (possible) outliers at 50,54 and 60. How can I add their ID into the graph?

user1607
  • 531
  • 7
  • 28
  • How do you want to decide a value is an outlier? Anything above a set threshold, more than x number of standard deviations from mean, etc. – camille Apr 23 '18 at 16:55
  • 1
    I basically want to add ID to the values at x-axis at 50,54 and 60. I managed to do it manually, but it is inefficient. The distribution has mean 31.86 and sd 6.07 - so the threshold would be more than 3 standard deviations – user1607 Apr 23 '18 at 19:50

2 Answers2

1

If you post your data, I'll amend this answer using it. But basically you want

df %>%
    count(x, y) %>%
    ggplot(aes(x = x, y = n, fill = y)) +
    geom_col() +
    geom_text(aes(label = x), data = . %>% filter(x >= thresh), vjust = 0, nudge_y = 0.1)

where thresh is some threshold you've set--maybe an arbitrary cutoff point that makes sense, or maybe 3 standard deviations from the mean of x, or whatever. You can store it in an outside variable, you can make a boolean column in your dataframe, or you can calculate it inline inside your geom_text--really up to you. vjust = 0, nudge_y = 0.1 puts the labels just above the bars corresponding to your outliers.

camille
  • 16,432
  • 18
  • 38
  • 60
0

Maybe geom_text(data=mydata%>%filter(just.the.outliers) ? See also this: RE: Alignment of numbers on the individual bars with ggplot2

steveLangsford
  • 646
  • 5
  • 9