0

I have the following data.frame with which i want to make a ggplot:

> topmicegrn
Topic Antigen           n    tc
    0     BCP 0.350533878 25193
    0     HEL 0.344341682 25193
    0     OVA 0.194974795 25193
    0     RSV 0.110149645 25193
    1     BCP 0.453020134   298
    1     HEL 0.228187919   298
    1     OVA 0.318791946   298
   10     BCP 0.979310345   145
   10     OVA 0.013793103   145
   10     HEL 0.006896552   145
...

The plot looks like this atm:

ggplot(topmicegrn, aes(Topic, n, label=tc)) +
  geom_bar(stat = "identity", aes(fill = Antigen)) +
  geom_text(stat='identity', position = 'stack') + 
  scale_fill_brewer(palette="YlGnBu") +
  coord_flip() 

Link to plot

Now, i would like to keep only the 'tc'-label at the end of each bar (so one label per 'Topic'), and get rid of the ones on every stack. I tried with geom_text(aes(group=Topic)), but it results in the same plot as shown.

Also, not every 'Topic' contains every 'Antigen', and the ordering is quite messy(the 'Topic' column is a factor which i ordered in a specific order), so using these solutions does not work for me. Any ideas?

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
ElArk
  • 23
  • 5

1 Answers1

1

Dataset is incomplete but this may work. You seem to have tc duplicated across row, so need to filter out the duplicates. You could do it by creating a second simpler dataframe with just that data

Here is one way that does it:

library(tidyverse)
topmicegrn <-
  read.table(text=
             "Topic Antigen           n    tc
0     BCP 0.350533878 25193
0     HEL 0.344341682 25193
0     OVA 0.194974795 25193
0     RSV 0.110149645 25193
1     BCP 0.453020134   298
1     HEL 0.228187919   298
1     OVA 0.318791946   298
10     BCP 0.979310345   145
10     OVA 0.013793103   145
10     HEL 0.006896552   145",
           header = T, stringsAsFactors = F)

topmicegrn_tc <-
  topmicegrn %>% 
  group_by(Topic) %>% 
  summarise(tc = max(tc))

ggplot(topmicegrn, aes(Topic, n)) +
  geom_bar(stat = "identity", aes(fill = Antigen)) +
  scale_fill_brewer(palette="YlGnBu") +
  coord_flip() +
  geom_text(data = topmicegrn_tc,
            aes(x= Topic, y = 1, label= tc),
            stat='identity', hjust = 1) 

Created on 2018-11-13 by the reprex package (v0.2.1)

Matt L.
  • 2,753
  • 13
  • 22
  • Sorry for the incomplete data. Your solution sadly won't work since not all 'Topics' contain the Antigen you filtered for. The tc's are duplicated since it's the only way to append them to the df. In other words: There is one tc per Topic, and i need that tc displayed at the top of the bar. – ElArk Nov 13 '18 at 18:33
  • instead of duplicating the 'tc' in the dataframe, create a separate dataframe with `Topic` and `tc`; then use that for the `data =` argument in geom_text. you may also need to add `inherit.aes=FALSE` – Matt L. Nov 13 '18 at 19:36
  • edit above should work- you may need to create the separate dataframe differently, depending on how you calculated tc. – Matt L. Nov 13 '18 at 19:41