Search code examples
rggplot2plotlyggplotly

Trying to add dots is specific points of line chart removed the lines


Below I display part of my dataframe using dput() in order to be sure that there is no problem with this. And then I try to create a line chart with ggplotly(). What I want to achieve is add points or dots or bubbles in the line chart where there are data in the columns Marketing/Product and label_text. Also the tooltip at these points should also include Marketing/Product and label_text besides the other columns. The problem is that the lines are not displayed at all now and only the dots are visible. The code is below the dataset.

to<-structure(list(int_end = structure(c(18660, 18682, 18682, 18689, 
                                         18694, 18710, 18715, 18715, 18715, 18716, 18716, 18716, 18717, 
                                         18717, 18717, 18718, 18718, 18718, 18719, 18719, 18719, 18720, 
                                         18720, 18720, 18721, 18721, 18721, 18722, 18722, 18722, 18723, 
                                         18723, 18723, 18724, 18724, 18724, 18725, 18725, 18725, 18726, 
                                         18726, 18726, 18727, 18727, 18727, 18728, 18728, 18728, 18729, 
                                         18729), class = "Date"), interval = structure(c(NA, NA, NA, NA, 
                                                                                         NA, NA, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 
                                                                                         24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 
                                                                                         24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 
                                                                                         24L, 24L, 24L, 24L, 24L, 24L, 24L), .Label = c("30", "29", "28", 
                                                                                                                                        "27", "26", "25", "24", "23", "22", "21", "20", "19", "18", "17", 
                                                                                                                                        "16", "15", "14", "13", "12", "11", "10", "9", "8", "7", "6", 
                                                                                                                                        "5", "4", "3", "2", "1"), class = "factor"), user_type = structure(c(NA, 
                                                                                                                                                                                                             NA, NA, NA, NA, NA, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 
                                                                                                                                                                                                             1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 
                                                                                                                                                                                                             3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 
                                                                                                                                                                                                             2L), .Label = c("total", "inactive", "active"), class = "factor"), 
                   users = c(NA, NA, NA, NA, NA, NA, 406L, 4311L, 4717L, 414L, 
                             4320L, 4734L, 418L, 4333L, 4751L, 412L, 4356L, 4768L, 383L, 
                             4389L, 4772L, 385L, 4388L, 4773L, 361L, 4414L, 4775L, 344L, 
                             4435L, 4779L, 414L, 4385L, 4799L, 441L, 4383L, 4824L, 447L, 
                             4403L, 4850L, 475L, 4395L, 4870L, 475L, 4399L, 4874L, 479L, 
                             4398L, 4877L, 545L, 4358L), `Marketing / Product` = c("P", 
                                                                                   "P", "P", "P", "P", "M", NA, NA, NA, NA, NA, NA, NA, NA, 
                                                                                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
                                                                                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
                                                                                   NA, NA, NA, NA, NA, NA), Title = c("New product: GBB Directory", 
                                                                                                                      "New profile feature: Sprachen, Zahlungsmöglichkeiten, Barrierefreiheit", 
                                                                                                                      "New profile feature: Ausbildungen", "Notification: new duplicates", 
                                                                                                                      "Improvement: Activity Filter", "Activity Item: Penta cooperation", 
                                                                                                                      NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
                                                                                                                      NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
                                                                                                                      NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), 
                   `Short Description` = c("The order import was extended by the products GBB directory. As soon as a client is booking this directory product the client is getting a SDE account.", 
                                           "SDE users have the possibility to publish \"Sprachen, Zahlungsmöglichkeiten, Barrierefreiheit\" in their profile.", 
                                           "SDE users have the possibility to publish up to 3 \"Ausbildungsangebote\" and a badge f.ex. from the IHK in their profile.", 
                                           "Listing clients are informed about new duplicates in their Listing profile via Email and Activity Stream. The user has to check the duplicates in the area of \"Online-Einträge\".", 
                                           "The activity filter was improved and simplified based on user feedback.", 
                                           "All SDE users were getting the activity items for the cooperation with Penta. With this item the user has the possibility to get Penta to special conditions (financing software).", 
                                           NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
                                           NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
                                           NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), 
                   description = c("The order import was extended\nby the products GBB directory.\nAs soon as a client is booking\nthis directory product the\nclient is getting a SDE\naccount.", 
                                   "SDE users have the possibility\nto publish \"Sprachen,\nZahlungsmöglichkeiten,\nBarrierefreiheit\" in their\nprofile.", 
                                   "SDE users have the\npossibility to publish up to\n3 \"Ausbildungsangebote\" and\na badge f.ex. from the IHK in\ntheir profile.", 
                                   "Listing clients are informed\nabout new duplicates in their\nListing profile via Email and\nActivity Stream. The user has\nto check the duplicates in the\narea of \"Online-Einträge\".", 
                                   "The activity filter was\nimproved and simplified based\non user feedback.", 
                                   "All SDE users were getting\nthe activity items for the\ncooperation with Penta. With\nthis item the user has the\npossibility to get Penta to\nspecial conditions (financing\nsoftware).", 
                                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
                                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
                                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), 
                   label_text = structure(c("Details: The order import was extended\nby the products GBB directory.\nAs soon as a client is booking\nthis directory product the\nclient is getting a SDE\naccount.", 
                                            "Details: SDE users have the possibility\nto publish \"Sprachen,\nZahlungsmöglichkeiten,\nBarrierefreiheit\" in their\nprofile.", 
                                            "Details: SDE users have the\npossibility to publish up to\n3 \"Ausbildungsangebote\" and\na badge f.ex. from the IHK in\ntheir profile.", 
                                            "Details: Listing clients are informed\nabout new duplicates in their\nListing profile via Email and\nActivity Stream. The user has\nto check the duplicates in the\narea of \"Online-Einträge\".", 
                                            "Details: The activity filter was\nimproved and simplified based\non user feedback.", 
                                            "Details: All SDE users were getting\nthe activity items for the\ncooperation with Penta. With\nthis item the user has the\npossibility to get Penta to\nspecial conditions (financing\nsoftware).", 
                                            NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
                                            NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
                                            NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), class = c("glue", 
                                                                                                               "character")), text = c("int_end: 2021-02-02<br>users: NA<br>user_type: NA<br>Marketing / Product: P<br>label_text: Details: The order import was extended\nby the products GBB directory.\nAs soon as a client is booking\nthis directory product the\nclient is getting a SDE\naccount.", 
                                                                                                                                       "int_end: 2021-02-24<br>users: NA<br>user_type: NA<br>Marketing / Product: P<br>label_text: Details: SDE users have the possibility\nto publish \"Sprachen,\nZahlungsmöglichkeiten,\nBarrierefreiheit\" in their\nprofile.", 
                                                                                                                                       "int_end: 2021-02-24<br>users: NA<br>user_type: NA<br>Marketing / Product: P<br>label_text: Details: SDE users have the\npossibility to publish up to\n3 \"Ausbildungsangebote\" and\na badge f.ex. from the IHK in\ntheir profile.", 
                                                                                                                                       "int_end: 2021-03-03<br>users: NA<br>user_type: NA<br>Marketing / Product: P<br>label_text: Details: Listing clients are informed\nabout new duplicates in their\nListing profile via Email and\nActivity Stream. The user has\nto check the duplicates in the\narea of \"Online-Einträge\".", 
                                                                                                                                       "int_end: 2021-03-08<br>users: NA<br>user_type: NA<br>Marketing / Product: P<br>label_text: Details: The activity filter was\nimproved and simplified based\non user feedback.", 
                                                                                                                                       "int_end: 2021-03-24<br>users: NA<br>user_type: NA<br>Marketing / Product: M<br>label_text: Details: All SDE users were getting\nthe activity items for the\ncooperation with Penta. With\nthis item the user has the\npossibility to get Penta to\nspecial conditions (financing\nsoftware).", 
                                                                                                                                       "int_end: 2021-03-29<br>users: 406<br>user_type: active", 
                                                                                                                                       "int_end: 2021-03-29<br>users: 4311<br>user_type: inactive", 
                                                                                                                                       "int_end: 2021-03-29<br>users: 4717<br>user_type: total", 
                                                                                                                                       "int_end: 2021-03-30<br>users: 414<br>user_type: active", 
                                                                                                                                       "int_end: 2021-03-30<br>users: 4320<br>user_type: inactive", 
                                                                                                                                       "int_end: 2021-03-30<br>users: 4734<br>user_type: total", 
                                                                                                                                       "int_end: 2021-03-31<br>users: 418<br>user_type: active", 
                                                                                                                                       "int_end: 2021-03-31<br>users: 4333<br>user_type: inactive", 
                                                                                                                                       "int_end: 2021-03-31<br>users: 4751<br>user_type: total", 
                                                                                                                                       "int_end: 2021-04-01<br>users: 412<br>user_type: active", 
                                                                                                                                       "int_end: 2021-04-01<br>users: 4356<br>user_type: inactive", 
                                                                                                                                       "int_end: 2021-04-01<br>users: 4768<br>user_type: total", 
                                                                                                                                       "int_end: 2021-04-02<br>users: 383<br>user_type: active", 
                                                                                                                                       "int_end: 2021-04-02<br>users: 4389<br>user_type: inactive", 
                                                                                                                                       "int_end: 2021-04-02<br>users: 4772<br>user_type: total", 
                                                                                                                                       "int_end: 2021-04-03<br>users: 385<br>user_type: active", 
                                                                                                                                       "int_end: 2021-04-03<br>users: 4388<br>user_type: inactive", 
                                                                                                                                       "int_end: 2021-04-03<br>users: 4773<br>user_type: total", 
                                                                                                                                       "int_end: 2021-04-04<br>users: 361<br>user_type: active", 
                                                                                                                                       "int_end: 2021-04-04<br>users: 4414<br>user_type: inactive", 
                                                                                                                                       "int_end: 2021-04-04<br>users: 4775<br>user_type: total", 
                                                                                                                                       "int_end: 2021-04-05<br>users: 344<br>user_type: active", 
                                                                                                                                       "int_end: 2021-04-05<br>users: 4435<br>user_type: inactive", 
                                                                                                                                       "int_end: 2021-04-05<br>users: 4779<br>user_type: total", 
                                                                                                                                       "int_end: 2021-04-06<br>users: 414<br>user_type: active", 
                                                                                                                                       "int_end: 2021-04-06<br>users: 4385<br>user_type: inactive", 
                                                                                                                                       "int_end: 2021-04-06<br>users: 4799<br>user_type: total", 
                                                                                                                                       "int_end: 2021-04-07<br>users: 441<br>user_type: active", 
                                                                                                                                       "int_end: 2021-04-07<br>users: 4383<br>user_type: inactive", 
                                                                                                                                       "int_end: 2021-04-07<br>users: 4824<br>user_type: total", 
                                                                                                                                       "int_end: 2021-04-08<br>users: 447<br>user_type: active", 
                                                                                                                                       "int_end: 2021-04-08<br>users: 4403<br>user_type: inactive", 
                                                                                                                                       "int_end: 2021-04-08<br>users: 4850<br>user_type: total", 
                                                                                                                                       "int_end: 2021-04-09<br>users: 475<br>user_type: active", 
                                                                                                                                       "int_end: 2021-04-09<br>users: 4395<br>user_type: inactive", 
                                                                                                                                       "int_end: 2021-04-09<br>users: 4870<br>user_type: total", 
                                                                                                                                       "int_end: 2021-04-10<br>users: 475<br>user_type: active", 
                                                                                                                                       "int_end: 2021-04-10<br>users: 4399<br>user_type: inactive", 
                                                                                                                                       "int_end: 2021-04-10<br>users: 4874<br>user_type: total", 
                                                                                                                                       "int_end: 2021-04-11<br>users: 479<br>user_type: active", 
                                                                                                                                       "int_end: 2021-04-11<br>users: 4398<br>user_type: inactive", 
                                                                                                                                       "int_end: 2021-04-11<br>users: 4877<br>user_type: total", 
                                                                                                                                       "int_end: 2021-04-12<br>users: 545<br>user_type: active", 
                                                                                                                                       "int_end: 2021-04-12<br>users: 4358<br>user_type: inactive"
                                                                                                               )), row.names = c(NA, -50L), class = c("data.table", "data.frame"
                                                                                                               ), sorted = "int_end")
  
  
  
  
plot <- to %>%
  ggplot(aes(int_end, users, color = user_type,text = text))  +
  geom_point(data = filter(to, !is.na(`Marketing / Product`) | !is.na(label_text)))+
  geom_line()+
  theme_bw() +
  scale_y_continuous(labels = scales::label_number_si(accuracy = 0))+
  #theme(legend.position = "none")+
  theme(legend.position = 'top')+
  labs(title = glue::glue("Number of Users over time."),
       subtitle = glue::glue("Interval window of  days."),
       y = "", x = "")

ggplotly(plot,tooltip="text")%>%
  layout(legend = list(
    orientation = "h",y=1.06
  )
  )    

Solution

  • Besides adding the group aesthetic as mentioned in my comment, your issue with the NAs in the legend could be fixed by filtering the data which also fixes the issue with the empty space.

    library(plotly)
    
    to <- filter(to, !is.na(user_type))
    
    plot <- to %>%
      ggplot(aes(int_end, users, text = text, color = user_type, group = user_type)) +
      geom_point(data = filter(to, !is.na(`Marketing / Product`) | !is.na(label_text))) +
      geom_line() +
      theme_bw() +
      scale_y_continuous(labels = scales::label_number_si(accuracy = 0)) +
      theme(legend.position = "top") +
      labs(
        title = glue::glue("Number of Users over time."),
        subtitle = glue::glue("Interval window of  days."),
        y = "", x = ""
      )
    
    ggplotly(plot, tooltip = "text") %>%
      layout(legend = list(
        orientation = "h", y = 1.06
      ))
    

    enter image description here