ggpubr not creating multiple bars in ggdotchart












3















Utilizing the example package code in ggpubr, the ggdotchart function does not create separate segments as is shown in the example, instead there is only a single segment, though the dots seem to be placed in the correct orientation. Does anyone have any tips on what the problem may be? I've thought it may be due to factors, tibbles vs. df, but I haven't been able to determine the problem.



Code:



df <- diamonds %>%
filter(color %in% c("J", "D")) %>%
group_by(cut, color) %>%
summarise(counts = n())

ggdotchart(df, x = "cut", y ="counts",
color = "color", palette = "jco", size = 3,
add = "segment",
add.params = list(color = "lightgray", size = 1.5),
position = position_dodge(0.3),
ggtheme = theme_pubclean()
)


With the expected output of:
enter image description here



But instead I am getting:
enter image description here










share|improve this question





























    3















    Utilizing the example package code in ggpubr, the ggdotchart function does not create separate segments as is shown in the example, instead there is only a single segment, though the dots seem to be placed in the correct orientation. Does anyone have any tips on what the problem may be? I've thought it may be due to factors, tibbles vs. df, but I haven't been able to determine the problem.



    Code:



    df <- diamonds %>%
    filter(color %in% c("J", "D")) %>%
    group_by(cut, color) %>%
    summarise(counts = n())

    ggdotchart(df, x = "cut", y ="counts",
    color = "color", palette = "jco", size = 3,
    add = "segment",
    add.params = list(color = "lightgray", size = 1.5),
    position = position_dodge(0.3),
    ggtheme = theme_pubclean()
    )


    With the expected output of:
    enter image description here



    But instead I am getting:
    enter image description here










    share|improve this question



























      3












      3








      3








      Utilizing the example package code in ggpubr, the ggdotchart function does not create separate segments as is shown in the example, instead there is only a single segment, though the dots seem to be placed in the correct orientation. Does anyone have any tips on what the problem may be? I've thought it may be due to factors, tibbles vs. df, but I haven't been able to determine the problem.



      Code:



      df <- diamonds %>%
      filter(color %in% c("J", "D")) %>%
      group_by(cut, color) %>%
      summarise(counts = n())

      ggdotchart(df, x = "cut", y ="counts",
      color = "color", palette = "jco", size = 3,
      add = "segment",
      add.params = list(color = "lightgray", size = 1.5),
      position = position_dodge(0.3),
      ggtheme = theme_pubclean()
      )


      With the expected output of:
      enter image description here



      But instead I am getting:
      enter image description here










      share|improve this question
















      Utilizing the example package code in ggpubr, the ggdotchart function does not create separate segments as is shown in the example, instead there is only a single segment, though the dots seem to be placed in the correct orientation. Does anyone have any tips on what the problem may be? I've thought it may be due to factors, tibbles vs. df, but I haven't been able to determine the problem.



      Code:



      df <- diamonds %>%
      filter(color %in% c("J", "D")) %>%
      group_by(cut, color) %>%
      summarise(counts = n())

      ggdotchart(df, x = "cut", y ="counts",
      color = "color", palette = "jco", size = 3,
      add = "segment",
      add.params = list(color = "lightgray", size = 1.5),
      position = position_dodge(0.3),
      ggtheme = theme_pubclean()
      )


      With the expected output of:
      enter image description here



      But instead I am getting:
      enter image description here







      r ggplot2 ggpubr






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 21:57









      markus

      11.1k1031




      11.1k1031










      asked Nov 14 '18 at 20:49









      JenksJenks

      3551521




      3551521
























          1 Answer
          1






          active

          oldest

          votes


















          2














          Here is a way to get your desired plot without ggpubr::ggdotchart. The issue seems to be that geom_segment does not allow dodging, as discussed here: R - ggplot dodging geom_lines and here: how to jitter/dodge geom_segments so they remain parallel?.



          # your data
          df <- diamonds %>%
          filter(color %in% c("J", "D")) %>%
          group_by(cut, color) %>%
          summarise(counts = n())


          The first step is to expand your data. We will need this when we call geom_line which allows for dodging. I took this idea from @Stibu's answer. We create a copy of df and change the counts column to be 0 in df2. Finally we use bind_rows to create a single data frame from df and df2.



          df2 <- df
          df2$counts <- 0

          df_out <- purrr::bind_rows(df, df2)
          df_out


          Then I use ggplot to create / replicate your desired output.



          ggplot(df_out, aes(x = cut, y = counts)) +
          geom_line(
          aes(col = color), # needed for dodging, we'll later change colors to "lightgrey"
          position = position_dodge(width = 0.3),
          show.legend = FALSE,
          size = 1.5
          ) +
          geom_point(
          aes(fill = color),
          data = subset(df_out, counts > 0),
          col = "transparent",
          shape = 21,
          size = 3,
          position = position_dodge(width = 0.3)
          ) +
          scale_color_manual(values = c("lightgray", "lightgray")) + #change line colors
          ggpubr::fill_palette(palette = "jco") +
          ggpubr::theme_pubclean()


          enter image description here






          share|improve this answer





















          • 1





            I didn't know that geom_segments didn't allow for dodging, now it all makes sense! Thanks @markus

            – Jenks
            Nov 15 '18 at 14:34











          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%2f53308501%2fggpubr-not-creating-multiple-bars-in-ggdotchart%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









          2














          Here is a way to get your desired plot without ggpubr::ggdotchart. The issue seems to be that geom_segment does not allow dodging, as discussed here: R - ggplot dodging geom_lines and here: how to jitter/dodge geom_segments so they remain parallel?.



          # your data
          df <- diamonds %>%
          filter(color %in% c("J", "D")) %>%
          group_by(cut, color) %>%
          summarise(counts = n())


          The first step is to expand your data. We will need this when we call geom_line which allows for dodging. I took this idea from @Stibu's answer. We create a copy of df and change the counts column to be 0 in df2. Finally we use bind_rows to create a single data frame from df and df2.



          df2 <- df
          df2$counts <- 0

          df_out <- purrr::bind_rows(df, df2)
          df_out


          Then I use ggplot to create / replicate your desired output.



          ggplot(df_out, aes(x = cut, y = counts)) +
          geom_line(
          aes(col = color), # needed for dodging, we'll later change colors to "lightgrey"
          position = position_dodge(width = 0.3),
          show.legend = FALSE,
          size = 1.5
          ) +
          geom_point(
          aes(fill = color),
          data = subset(df_out, counts > 0),
          col = "transparent",
          shape = 21,
          size = 3,
          position = position_dodge(width = 0.3)
          ) +
          scale_color_manual(values = c("lightgray", "lightgray")) + #change line colors
          ggpubr::fill_palette(palette = "jco") +
          ggpubr::theme_pubclean()


          enter image description here






          share|improve this answer





















          • 1





            I didn't know that geom_segments didn't allow for dodging, now it all makes sense! Thanks @markus

            – Jenks
            Nov 15 '18 at 14:34
















          2














          Here is a way to get your desired plot without ggpubr::ggdotchart. The issue seems to be that geom_segment does not allow dodging, as discussed here: R - ggplot dodging geom_lines and here: how to jitter/dodge geom_segments so they remain parallel?.



          # your data
          df <- diamonds %>%
          filter(color %in% c("J", "D")) %>%
          group_by(cut, color) %>%
          summarise(counts = n())


          The first step is to expand your data. We will need this when we call geom_line which allows for dodging. I took this idea from @Stibu's answer. We create a copy of df and change the counts column to be 0 in df2. Finally we use bind_rows to create a single data frame from df and df2.



          df2 <- df
          df2$counts <- 0

          df_out <- purrr::bind_rows(df, df2)
          df_out


          Then I use ggplot to create / replicate your desired output.



          ggplot(df_out, aes(x = cut, y = counts)) +
          geom_line(
          aes(col = color), # needed for dodging, we'll later change colors to "lightgrey"
          position = position_dodge(width = 0.3),
          show.legend = FALSE,
          size = 1.5
          ) +
          geom_point(
          aes(fill = color),
          data = subset(df_out, counts > 0),
          col = "transparent",
          shape = 21,
          size = 3,
          position = position_dodge(width = 0.3)
          ) +
          scale_color_manual(values = c("lightgray", "lightgray")) + #change line colors
          ggpubr::fill_palette(palette = "jco") +
          ggpubr::theme_pubclean()


          enter image description here






          share|improve this answer





















          • 1





            I didn't know that geom_segments didn't allow for dodging, now it all makes sense! Thanks @markus

            – Jenks
            Nov 15 '18 at 14:34














          2












          2








          2







          Here is a way to get your desired plot without ggpubr::ggdotchart. The issue seems to be that geom_segment does not allow dodging, as discussed here: R - ggplot dodging geom_lines and here: how to jitter/dodge geom_segments so they remain parallel?.



          # your data
          df <- diamonds %>%
          filter(color %in% c("J", "D")) %>%
          group_by(cut, color) %>%
          summarise(counts = n())


          The first step is to expand your data. We will need this when we call geom_line which allows for dodging. I took this idea from @Stibu's answer. We create a copy of df and change the counts column to be 0 in df2. Finally we use bind_rows to create a single data frame from df and df2.



          df2 <- df
          df2$counts <- 0

          df_out <- purrr::bind_rows(df, df2)
          df_out


          Then I use ggplot to create / replicate your desired output.



          ggplot(df_out, aes(x = cut, y = counts)) +
          geom_line(
          aes(col = color), # needed for dodging, we'll later change colors to "lightgrey"
          position = position_dodge(width = 0.3),
          show.legend = FALSE,
          size = 1.5
          ) +
          geom_point(
          aes(fill = color),
          data = subset(df_out, counts > 0),
          col = "transparent",
          shape = 21,
          size = 3,
          position = position_dodge(width = 0.3)
          ) +
          scale_color_manual(values = c("lightgray", "lightgray")) + #change line colors
          ggpubr::fill_palette(palette = "jco") +
          ggpubr::theme_pubclean()


          enter image description here






          share|improve this answer















          Here is a way to get your desired plot without ggpubr::ggdotchart. The issue seems to be that geom_segment does not allow dodging, as discussed here: R - ggplot dodging geom_lines and here: how to jitter/dodge geom_segments so they remain parallel?.



          # your data
          df <- diamonds %>%
          filter(color %in% c("J", "D")) %>%
          group_by(cut, color) %>%
          summarise(counts = n())


          The first step is to expand your data. We will need this when we call geom_line which allows for dodging. I took this idea from @Stibu's answer. We create a copy of df and change the counts column to be 0 in df2. Finally we use bind_rows to create a single data frame from df and df2.



          df2 <- df
          df2$counts <- 0

          df_out <- purrr::bind_rows(df, df2)
          df_out


          Then I use ggplot to create / replicate your desired output.



          ggplot(df_out, aes(x = cut, y = counts)) +
          geom_line(
          aes(col = color), # needed for dodging, we'll later change colors to "lightgrey"
          position = position_dodge(width = 0.3),
          show.legend = FALSE,
          size = 1.5
          ) +
          geom_point(
          aes(fill = color),
          data = subset(df_out, counts > 0),
          col = "transparent",
          shape = 21,
          size = 3,
          position = position_dodge(width = 0.3)
          ) +
          scale_color_manual(values = c("lightgray", "lightgray")) + #change line colors
          ggpubr::fill_palette(palette = "jco") +
          ggpubr::theme_pubclean()


          enter image description here







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 12:52

























          answered Nov 14 '18 at 21:59









          markusmarkus

          11.1k1031




          11.1k1031








          • 1





            I didn't know that geom_segments didn't allow for dodging, now it all makes sense! Thanks @markus

            – Jenks
            Nov 15 '18 at 14:34














          • 1





            I didn't know that geom_segments didn't allow for dodging, now it all makes sense! Thanks @markus

            – Jenks
            Nov 15 '18 at 14:34








          1




          1





          I didn't know that geom_segments didn't allow for dodging, now it all makes sense! Thanks @markus

          – Jenks
          Nov 15 '18 at 14:34





          I didn't know that geom_segments didn't allow for dodging, now it all makes sense! Thanks @markus

          – Jenks
          Nov 15 '18 at 14:34


















          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%2f53308501%2fggpubr-not-creating-multiple-bars-in-ggdotchart%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







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini