Remove geom_text labels on every layer of stack












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?










share|improve this question





























    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?










    share|improve this question



























      0












      0








      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?










      share|improve this question
















      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?







      r ggplot2






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 13:25









      hrbrmstr

      60.6k687148




      60.6k687148










      asked Nov 13 '18 at 13:24









      ElArkElArk

      52




      52
























          1 Answer
          1






          active

          oldest

          votes


















          0














          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)






          share|improve this answer


























          • 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











          • will try that tomorrow, thank you!

            – ElArk
            Nov 13 '18 at 19:54











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282014%2fremove-geom-text-labels-on-every-layer-of-stack%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          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)






          share|improve this answer


























          • 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











          • will try that tomorrow, thank you!

            – ElArk
            Nov 13 '18 at 19:54
















          0














          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)






          share|improve this answer


























          • 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











          • will try that tomorrow, thank you!

            – ElArk
            Nov 13 '18 at 19:54














          0












          0








          0







          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)






          share|improve this answer















          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)







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 19:40

























          answered Nov 13 '18 at 16:16









          Matt L.Matt L.

          887513




          887513













          • 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











          • will try that tomorrow, thank you!

            – ElArk
            Nov 13 '18 at 19:54



















          • 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











          • will try that tomorrow, thank you!

            – ElArk
            Nov 13 '18 at 19:54

















          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





          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





          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





          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













          will try that tomorrow, thank you!

          – ElArk
          Nov 13 '18 at 19:54





          will try that tomorrow, thank you!

          – ElArk
          Nov 13 '18 at 19:54


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282014%2fremove-geom-text-labels-on-every-layer-of-stack%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Danny Elfman

          Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues