Home > OS >  ggplot2 stacked bars seem to be separated in many different slices when combined with ggplotly()
ggplot2 stacked bars seem to be separated in many different slices when combined with ggplotly()

Time:09-17

I create the stacked bar chart below but while the code seems to work the bars seem to be separated in slices with a different hover for every slice which is unnecessary and confusing. How can I remove them?

Tar2<-structure(list(Site = c("ABC123", "ABC123", "ABC123", "ABC123", 
"ABC123", "ABC123", "ABC123", "ABC123", "ABC123", "ABC123", "ABC123", 
"ABC123", "ABC123", "ABC123", "ABC123", "ABC123", "ABC123", "ABC123", 
"ABC123", "ABC123", "ABC123", "ABC123", "ABC123", "ABC123", "ABC123", 
"ABC123", "ABC123", "ABC123", "ABC123", "ABC123", "ABC123", "ABC123", 
"ABC123", "ABC123", "ABC123", "ABC123", "ABC123", "ABC123", "ABC123", 
"ABC123"), Group = c("A", "A", "B", "B", "A", "A", "A", "A", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "A", "A", "B", "B", "B", "B", "A", "A", "B", "B", "B", "B", 
"B", "B", "B", "B", "A", "A"), Patients = c(46L, 46L, 46L, 46L, 
46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 
46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 
46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L)), row.names = c(NA, 
-40L), class = c("tbl_df", "tbl", "data.frame"))

library(ggplot2)
library(dplyr)
library(plotly)
c<-ggplot(Tar2, aes(fill=Group, y=Patients, x=Site))  
    geom_bar(position="stack", stat="identity")
c<-c  theme(axis.text.x = element_text(angle = 45, vjust = 0.5, hjust=1)) labs(title = "Figure 2:Weekly Enrollment by Cohort by Site"
)
ggplotly(c)

CodePudding user response:

You can use group_by and slice to get only one row per group:

Tar2 <- structure(list(Site = c(
  "ABC123", "ABC123", "ABC123", "ABC123",
  "ABC123", "ABC123", "ABC123", "ABC123", "ABC123", "ABC123", "ABC123",
  "ABC123", "ABC123", "ABC123", "ABC123", "ABC123", "ABC123", "ABC123",
  "ABC123", "ABC123", "ABC123", "ABC123", "ABC123", "ABC123", "ABC123",
  "ABC123", "ABC123", "ABC123", "ABC123", "ABC123", "ABC123", "ABC123",
  "ABC123", "ABC123", "ABC123", "ABC123", "ABC123", "ABC123", "ABC123",
  "ABC123"
), Group = c(
  "A", "A", "B", "B", "A", "A", "A", "A",
  "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B",
  "B", "A", "A", "B", "B", "B", "B", "A", "A", "B", "B", "B", "B",
  "B", "B", "B", "B", "A", "A"
), Patients = c(
  46L, 46L, 46L, 46L,
  46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L,
  46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L,
  46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L
)), row.names = c(
  NA,
  -40L
), class = c("tbl_df", "tbl", "data.frame"))

library(tidyverse)

c <-
  Tar2 %>%
  group_by(Group, Patients, Site) %>%
  slice(1) %>%
  ggplot(aes(fill = Group, y = Patients, x = Site))  
  geom_bar(position = "stack", stat = "identity")  
  theme(axis.text.x = element_text(angle = 45, vjust = 0.5, hjust = 1))  
  labs(title = "Figure 2:Weekly Enrollment by Cohort by Site")
c

Created on 2021-09-14 by the reprex package (v2.0.1)

  • Related