I have a faceted ggplot, and I'm really struggling to add a tooltip on hover.
A sample file is here: https://github.com/rollingstone09/help (edit: have now added a dput output to the bottom of this post).
a <- read_excel("a.xlsx")
a$Date <- as.Date(as.character(a$Date))
a$Week <- as.Date(as.character(a$Week))
a$Day <- factor(a$Day,
levels = c("Monday", "Tuesday",
"Wednesday", "Thursday",
"Friday", "Saturday", "Sunday"))
a$Session <- factor(a$Session,
levels = c("PM", "AM",
"All Day"))
a$Priority <- as.character(as.character(a$Priority))
aa2 <- a %>%
group_by(Date, Theatre) %>%
mutate(Order3 = row_number())
aa2 %>%
ggplot() +
geom_bar(aes("", Available), stat = "identity", position = "identity", colour = "red", fill = "white", alpha = 0, linetype = 2) +
geom_col(aes("", Booked, fill = Priority), colour = "black") +
geom_point(aes("", Available), size = 1, colour = "red", shape = 4, stroke = 2) +
coord_flip() +
facet_grid(Theatre~Day) +
theme_bw()
I would like to be able to hover over the stacked bars, and the following information (from "a") to pop up: "PID", "Booked", "Operation", "Surgeon", "Ward To". My hope is to add a copy of the plot to a rmarkdown report, and ideally the end product would be a clone of the above ggplot but with this information shown on hover.
If I try to use ggplotly: the plot looks awful... the ggplot theme_bw() is overwritten, geom_bar disappears, my geom_point is no longer a cross etc.. If I try to use ggiraph, the plot seems to quadruple in size, the facets become squashed. I have tried googling how I can go about rectifying, but I'm at a loss and just hoping someone can either show me or point me in the right direction of where I'd need to go to learn. Any help greatly appreciated.
(Added dput output of my sample table as requested) --
structure(list(Date = structure(c(1672790400, 1672790400, 1672790400,
1672790400, 1672790400, 1672876800, 1672876800, 1672876800, 1672876800,
1672876800, 1672963200, 1672963200, 1672963200, 1672963200, 1672704000,
1672704000, 1672704000, 1672704000, 1672704000, 1672704000, 1672704000,
1672704000, 1672790400, 1672790400, 1672790400, 1672790400, 1672876800,
1672876800, 1672876800, 1672876800, 1672876800, 1672704000, 1672790400,
1672963200, 1672704000, 1672876800, 1672876800, 1672876800, 1672876800,
1672704000, 1672704000, 1672704000, 1672704000, 1672876800, 1672876800,
1672876800, 1672876800, 1672704000, 1672704000, 1672704000, 1672704000,
1672704000, 1672790400, 1672790400, 1672790400, 1672790400, 1672790400,
1672963200, 1672963200, 1672963200, 1672790400, 1672790400, 1672790400,
1672790400, 1672876800, 1672876800, 1672963200, 1672963200, 1672790400,
1672790400, 1672790400, 1672704000, 1672704000, 1672704000, 1672704000,
1672704000, 1672790400, 1672790400, 1672876800, 1672876800, 1672963200,
1672963200, 1672876800, 1672876800, 1672790400, 1672790400, 1672790400,
1672790400, 1672790400, 1672876800, 1672876800, 1672876800, 1672876800,
1672876800, 1672704000, 1672704000, 1672704000, 1672704000, 1672704000,
1672704000, 1672704000, 1672790400, 1672876800, 1672876800, 1672790400,
1672790400, 1672790400, 1672876800, 1672876800, 1672963200, 1672963200,
1672963200, 1672963200, 1672963200, 1672963200, 1672963200, 1672963200,
1672876800, 1672876800, 1672876800, 1672963200, 1672963200, 1672963200,
1672963200, 1673049600, 1672790400, 1672876800, 1672876800, 1672876800,
1672876800, 1672704000), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
Week = structure(c(1672617600, 1672617600, 1672617600, 1672617600,
1672617600, 1672617600, 1672617600, 1672617600, 1672617600,
1672617600, 1672617600, 1672617600, 1672617600, 1672617600,
1672617600, 1672617600, 1672617600, 1672617600, 1672617600,
1672617600, 1672617600, 1672617600, 1672617600, 1672617600,
1672617600, 1672617600, 1672617600, 1672617600, 1672617600,
1672617600, 1672617600, 1672617600, 1672617600, 1672617600,
1672617600, 1672617600, 1672617600, 1672617600, 1672617600,
1672617600, 1672617600, 1672617600, 1672617600, 1672617600,
1672617600, 1672617600, 1672617600, 1672617600, 1672617600,
1672617600, 1672617600, 1672617600, 1672617600, 1672617600,
1672617600, 1672617600, 1672617600, 1672617600, 1672617600,
1672617600, 1672617600, 1672617600, 1672617600, 1672617600,
1672617600, 1672617600, 1672617600, 1672617600, 1672617600,
1672617600, 1672617600, 1672617600, 1672617600, 1672617600,
1672617600, 1672617600, 1672617600, 1672617600, 1672617600,
1672617600, 1672617600, 1672617600, 1672617600, 1672617600,
1672617600, 1672617600, 1672617600, 1672617600, 1672617600,
1672617600, 1672617600, 1672617600, 1672617600, 1672617600,
1672617600, 1672617600, 1672617600, 1672617600, 1672617600,
1672617600, 1672617600, 1672617600, 1672617600, 1672617600,
1672617600, 1672617600, 1672617600, 1672617600, 1672617600,
1672617600, 1672617600, 1672617600, 1672617600, 1672617600,
1672617600, 1672617600, 1672617600, 1672617600, 1672617600,
1672617600, 1672617600, 1672617600, 1672617600, 1672617600,
1672617600, 1672617600, 1672617600, 1672617600, 1672617600,
1672617600, 1672617600), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
SID = c(686724, 686724, 686724, 686724, 686724, 686726, 686726,
686726, 686726, 686726, 686728, 686728, 686728, 686728, 686732,
686732, 686732, 686732, 686732, 686732, 686732, 686732, 686734,
686734, 686734, 686734, 686736, 686736, 686736, 686736, 686736,
686744, 686746, 686752, 686756, 686760, 686760, 686760, 686760,
686768, 686768, 686770, 686770, 686774, 686774, 686774, 686776,
686782, 686782, 686782, 686782, 686782, 686784, 686784, 686784,
686786, 686786, 686790, 686792, 686792, 686800, 686800, 686800,
686800, 686802, 686802, 686804, 686804, 686808, 686810, 686810,
686816, 686816, 686816, 686826, 686826, 686828, 686828, 686830,
686830, 686832, 686832, 686948, 686948, 686954, 686954, 686956,
686956, 686956, 686958, 686958, 686960, 686960, 686960, 686962,
686962, 686962, 686964, 686964, 686964, 687114, 687116, 687118,
687118, 687166, 687166, 687166, 687168, 687168, 687170, 687380,
687380, 687382, 687382, 687382, 687382, 687382, 688596, 688596,
688596, 688804, 688804, 688806, 688806, 688814, 688828, 689432,
689432, 689434, 689434, 689522), PID = c("ANON1", "ANON2",
"ANON3", "ANON4", "ANON5", "ANON6", "ANON7", "ANON8", "ANON9",
"ANON10", "ANON11", "ANON12", "ANON13", "ANON14", "ANON15",
"ANON16", "ANON17", "ANON18", "ANON19", "ANON20", "ANON21",
"ANON22", "ANON23", "ANON24", "ANON25", "ANON26", "ANON27",
"ANON28", "ANON29", "ANON30", "ANON31", "ANON32", "ANON33",
"ANON34", "ANON35", "ANON36", "ANON37", "ANON38", "ANON39",
"ANON40", "ANON41", "ANON42", "ANON43", "ANON44", "ANON45",
"ANON46", "ANON47", "ANON48", "ANON49", "ANON50", "ANON51",
"ANON52", "ANON53", "ANON54", "ANON55", "ANON56", "ANON57",
"ANON58", "ANON59", "ANON60", "ANON61", "ANON62", "ANON63",
"ANON64", "ANON65", "ANON66", "ANON67", "ANON68", "ANON69",
"ANON70", "ANON71", "ANON72", "ANON73", "ANON74", "ANON75",
"ANON76", "ANON77", "ANON78", "ANON79", "ANON80", "ANON81",
"ANON82", "ANON83", "ANON84", "ANON85", "ANON86", "ANON87",
"ANON88", "ANON89", "ANON90", "ANON91", "ANON92", "ANON93",
"ANON94", "ANON95", "ANON96", "ANON97", "ANON98", "ANON99",
"ANON100", "ANON101", "ANON102", "ANON103", "ANON104", "ANON105",
"ANON106", "ANON107", "ANON108", "ANON109", "ANON110", "ANON111",
"ANON112", "ANON113", "ANON114", "ANON115", "ANON116", "ANON117",
"ANON118", "ANON119", "ANON120", "ANON121", "ANON122", "ANON123",
"ANON124", "ANON125", "ANON126", "ANON127", "ANON128", "ANON129",
"ANON130", "ANON131"), Day = c("Wednesday", "Wednesday",
"Wednesday", "Wednesday", "Wednesday", "Thursday", "Thursday",
"Thursday", "Thursday", "Thursday", "Friday", "Friday", "Friday",
"Friday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday",
"Tuesday", "Tuesday", "Tuesday", "Wednesday", "Wednesday",
"Wednesday", "Wednesday", "Thursday", "Thursday", "Thursday",
"Thursday", "Thursday", "Tuesday", "Wednesday", "Friday",
"Tuesday", "Thursday", "Thursday", "Thursday", "Thursday",
"Tuesday", "Tuesday", "Tuesday", "Tuesday", "Thursday", "Thursday",
"Thursday", "Thursday", "Tuesday", "Tuesday", "Tuesday",
"Tuesday", "Tuesday", "Wednesday", "Wednesday", "Wednesday",
"Wednesday", "Wednesday", "Friday", "Friday", "Friday", "Wednesday",
"Wednesday", "Wednesday", "Wednesday", "Thursday", "Thursday",
"Friday", "Friday", "Wednesday", "Wednesday", "Wednesday",
"Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Wednesday",
"Wednesday", "Thursday", "Thursday", "Friday", "Friday",
"Thursday", "Thursday", "Wednesday", "Wednesday", "Wednesday",
"Wednesday", "Wednesday", "Thursday", "Thursday", "Thursday",
"Thursday", "Thursday", "Tuesday", "Tuesday", "Tuesday",
"Tuesday", "Tuesday", "Tuesday", "Tuesday", "Wednesday",
"Thursday", "Thursday", "Wednesday", "Wednesday", "Wednesday",
"Thursday", "Thursday", "Friday", "Friday", "Friday", "Friday",
"Friday", "Friday", "Friday", "Friday", "Thursday", "Thursday",
"Thursday", "Friday", "Friday", "Friday", "Friday", "Saturday",
"Wednesday", "Thursday", "Thursday", "Thursday", "Thursday",
"Tuesday"), Operation = c("Text 1", "Text 2", "Text 3", "Text 4",
"Text 5", "Text 6", "Text 7", "Text 8", "Text 9", "Text 10",
"Text 11", "Text 12", "Text 13", "Text 14", "Text 15", "Text 16",
"Text 17", "Text 18", "Text 19", "Text 20", "Text 21", "Text 22",
"Text 23", "Text 24", "Text 25", "Text 26", "Text 27", "Text 28",
"Text 29", "Text 30", "Text 31", "Text 32", "Text 33", "Text 34",
"Text 35", "Text 36", "Text 37", "Text 38", "Text 39", "Text 40",
"Text 41", "Text 42", "Text 43", "Text 44", "Text 45", "Text 46",
"Text 47", "Text 48", "Text 49", "Text 50", "Text 51", "Text 52",
"Text 53", "Text 54", "Text 55", "Text 56", "Text 57", "Text 58",
"Text 59", "Text 60", "Text 61", "Text 62", "Text 63", "Text 64",
"Text 65", "Text 66", "Text 67", "Text 68", "Text 69", "Text 70",
"Text 71", "Text 72", "Text 73", "Text 74", "Text 75", "Text 76",
"Text 77", "Text 78", "Text 79", "Text 80", "Text 81", "Text 82",
"Text 83", "Text 84", "Text 85", "Text 86", "Text 87", "Text 88",
"Text 89", "Text 90", "Text 91", "Text 92", "Text 93", "Text 94",
"Text 95", "Text 96", "Text 97", "Text 98", "Text 99", "Text 100",
"Text 101", "Text 102", "Text 103", "Text 104", "Text 105",
"Text 106", "Text 107", "Text 108", "Text 109", "Text 110",
"Text 111", "Text 112", "Text 113", "Text 114", "Text 115",
"Text 116", "Text 117", "Text 118", "Text 119", "Text 120",
"Text 121", "Text 122", "Text 123", "Text 124", "Text 125",
"Text 126", "Text 127", "Text 128", "Text 129", "Text 130",
"Text 131"), Specialty = c("Text 1", "Text 2", "Text 3",
"Text 4", "Text 5", "Text 6", "Text 7", "Text 8", "Text 9",
"Text 10", "Text 11", "Text 12", "Text 13", "Text 14", "Text 15",
"Text 16", "Text 17", "Text 18", "Text 19", "Text 20", "Text 21",
"Text 22", "Text 23", "Text 24", "Text 25", "Text 26", "Text 27",
"Text 28", "Text 29", "Text 30", "Text 31", "Text 32", "Text 33",
"Text 34", "Text 35", "Text 36", "Text 37", "Text 38", "Text 39",
"Text 40", "Text 41", "Text 42", "Text 43", "Text 44", "Text 45",
"Text 46", "Text 47", "Text 48", "Text 49", "Text 50", "Text 51",
"Text 52", "Text 53", "Text 54", "Text 55", "Text 56", "Text 57",
"Text 58", "Text 59", "Text 60", "Text 61", "Text 62", "Text 63",
"Text 64", "Text 65", "Text 66", "Text 67", "Text 68", "Text 69",
"Text 70", "Text 71", "Text 72", "Text 73", "Text 74", "Text 75",
"Text 76", "Text 77", "Text 78", "Text 79", "Text 80", "Text 81",
"Text 82", "Text 83", "Text 84", "Text 85", "Text 86", "Text 87",
"Text 88", "Text 89", "Text 90", "Text 91", "Text 92", "Text 93",
"Text 94", "Text 95", "Text 96", "Text 97", "Text 98", "Text 99",
"Text 100", "Text 101", "Text 102", "Text 103", "Text 104",
"Text 105", "Text 106", "Text 107", "Text 108", "Text 109",
"Text 110", "Text 111", "Text 112", "Text 113", "Text 114",
"Text 115", "Text 116", "Text 117", "Text 118", "Text 119",
"Text 120", "Text 121", "Text 122", "Text 123", "Text 124",
"Text 125", "Text 126", "Text 127", "Text 128", "Text 129",
"Text 130", "Text 131"), Booked = c(150, 150, 90, 50, 90,
90, 75, 180, 120, 75, 120, 90, 60, 120, 90, 90, 45, 45, 45,
60, 70, 30, 60, 90, 240, 90, 180, 180, 60, 60, 60, 540, 810,
480, 810, 60, 120, 60, 60, 82, 90, 90, 119, 90, 75, 70, 240,
60, 150, 60, 90, 90, 105, 60, 110, 90, 75, 240, 120, 90,
90, 120, 90, 180, 360, 60, 420, 60, 240, 90, 90, 60, 90,
60, 240, 280, 150, 181, 280, 240, 240, 360, 240, 240, 120,
120, 75, 90, 90, 120, 60, 60, 75, 75, 90, 90, 60, 60, 60,
90, 500, 510, 152, 363, 87, 227, 150, 528, 123, 300, 150,
100, 60, 60, 50, 40, 50, 90, 90, 60, 60, 120, 240, 120, 427,
540, 240, 90, 240, 60, 240), Available = c(540, 540, 540,
540, 540, 540, 540, 540, 540, 540, 540, 540, 540, 540, 540,
540, 540, 540, 540, 540, 540, 540, 540, 540, 540, 540, 540,
540, 540, 540, 540, 540, 810, 540, 810, 540, 540, 540, 540,
240, 240, 240, 240, 240, 240, 240, 240, 540, 540, 540, 540,
540, 240, 240, 240, 240, 240, 240, 240, 240, 540, 540, 540,
540, 540, 540, 540, 540, 240, 240, 240, 240, 240, 240, 540,
540, 540, 540, 540, 540, 540, 540, 540, 540, 240, 240, 240,
240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240,
240, 540, 540, 540, 540, 540, 540, 540, 540, 540, 540, 240,
240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240, 240,
240, 240, 540, 240, 240, 240, 240, 240), Theatre = c("03",
"03", "03", "03", "03", "03", "03", "03", "03", "03", "03",
"03", "03", "03", "04", "04", "04", "04", "04", "04", "04",
"04", "04", "04", "04", "04", "04", "04", "04", "04", "04",
"05", "05", "05", "06", "06", "06", "06", "06", "07", "07",
"07", "07", "07", "07", "07", "07", "08", "08", "08", "08",
"08", "08", "08", "08", "08", "08", "08", "08", "08", "09",
"09", "09", "09", "09", "09", "09", "09", "10", "10", "10",
"11", "11", "11", "12", "12", "12", "12", "12", "12", "12",
"12", "05", "05", "07", "07", "07", "07", "07", "08", "08",
"08", "08", "08", "09", "09", "09", "09", "09", "09", "14",
"14", "14", "14", "15", "15", "15", "15", "15", "15", "04",
"04", "04", "04", "04", "04", "04", "11", "11", "11", "06",
"06", "06", "06", "15", "06", "10", "10", "10", "10", "03"
), Priority = c(3, 3, 2, 1, 1, 1, 2, 1, 4, 2, 4, 4, 2, 3,
2, 4, 2, 1, 2, 3, 2, 2, 3, 1, 4, 1, 1, 1, 2, 3, 2, 3, 1,
4, 4, 3, 4, 2, 2, 2, 2, 1, 1, 1, 2, 2, 1, 4, 1, 4, 2, 4,
2, 1, 1, 4, 3, 3, 2, 2, 4, 4, 2, 1, 2, 4, 3, 4, 4, 1, 4,
4, 3, 1, 3, 1, 4, 4, 2, 4, 3, 1, 1, 4, 1, 1, 2, 2, 3, 1,
3, 4, 4, 4, 4, 1, 4, 4, 2, 4, 2, 3, 3, 1, 3, 2, 1, 3, 4,
1, 4, 4, 1, 1, 3, 4, 2, 1, 4, 2, 3, 2, 3, 4, 2, 4, 1, 1,
1, 1, 3), Session = c("All Day", "All Day", "All Day", "All Day",
"All Day", "All Day", "All Day", "All Day", "All Day", "All Day",
"All Day", "All Day", "All Day", "All Day", "All Day", "All Day",
"All Day", "All Day", "All Day", "All Day", "All Day", "All Day",
"All Day", "All Day", "All Day", "All Day", "All Day", "All Day",
"All Day", "All Day", "All Day", "All Day", "All Day", "All Day",
"All Day", "All Day", "All Day", "All Day", "All Day", "AM",
"AM", "PM", "PM", "AM", "AM", "AM", "PM", "All Day", "All Day",
"All Day", "All Day", "All Day", "AM", "AM", "AM", "PM",
"PM", "AM", "PM", "PM", "All Day", "All Day", "All Day",
"All Day", "All Day", "All Day", "All Day", "All Day", "AM",
"PM", "PM", "PM", "PM", "PM", "All Day", "All Day", "All Day",
"All Day", "All Day", "All Day", "All Day", "All Day", "All Day",
"All Day", "AM", "AM", "PM", "PM", "PM", "AM", "AM", "PM",
"PM", "PM", "AM", "AM", "AM", "PM", "PM", "PM", "All Day",
"All Day", "All Day", "All Day", "All Day", "All Day", "All Day",
"All Day", "All Day", "All Day", "AM", "AM", "PM", "PM",
"PM", "PM", "PM", "PM", "PM", "PM", "AM", "AM", "PM", "PM",
"AM", "All Day", "AM", "AM", "PM", "PM", "AM"), WardTo = c(40,
15, 5, 33, 11, 18, 40, 19, 12, 34, 41, 24, 9, 23, 36, 26,
25, 31, 46, 30, 12, 4, 27, 32, 48, 46, 3, 23, 44, 1, 35,
45, 9, 34, 21, 28, 26, 23, 20, 37, 39, 2, 42, 36, 35, 6,
36, 6, 11, 39, 13, 12, 28, 50, 47, 16, 26, 2, 33, 38, 50,
47, 49, 10, 7, 49, 36, 44, 42, 2, 38, 19, 43, 21, 12, 10,
48, 10, 14, 42, 2, 39, 11, 41, 18, 41, 47, 15, 39, 8, 11,
17, 11, 12, 12, 20, 50, 12, 2, 46, 13, 6, 4, 22, 8, 3, 11,
27, 12, 12, 14, 27, 23, 48, 25, 49, 49, 30, 1, 28, 35, 9,
37, 21, 27, 25, 31, 18, 13, 3, 7), Order = c(1, 2, 3, 4,
5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 1, 2, 3, 4, 5, 6, 7, 8, 1,
2, 3, 4, 1, 2, 3, 4, 5, 1, 1, 1, 1, 1, 2, 3, 4, 1, 2, 1,
2, 1, 2, 3, 1, 1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 1, 1, 2, 1,
2, 3, 4, 1, 2, 1, 2, 1, 1, 2, 1, 2, 3, 1, 2, 1, 2, 1, 2,
1, 2, 1, 2, 1, 2, 1, 2, 3, 1, 2, 1, 2, 3, 1, 2, 3, 1, 2,
3, 1, 1, 1, 2, 1, 2, 3, 1, 2, 1, 1, 2, 1, 2, 3, 4, 5, 1,
2, 3, 1, 2, 1, 2, 1, 1, 1, 2, 1, 2, 1), Surgeon = c(2, 2,
5, 4, 9, 4, 8, 4, 5, 7, 9, 7, 8, 8, 10, 6, 6, 1, 2, 1, 4,
5, 7, 4, 9, 4, 6, 10, 1, 3, 10, 4, 8, 9, 4, 3, 2, 7, 6, 5,
1, 5, 10, 8, 5, 8, 10, 4, 10, 4, 3, 4, 7, 8, 5, 9, 8, 1,
5, 10, 3, 1, 3, 6, 10, 7, 8, 3, 5, 1, 2, 6, 5, 9, 10, 4,
9, 2, 4, 10, 5, 7, 2, 6, 8, 3, 9, 2, 4, 1, 9, 9, 2, 3, 9,
8, 9, 6, 10, 5, 7, 9, 2, 5, 7, 2, 10, 2, 2, 1, 1, 2, 6, 5,
3, 10, 2, 4, 3, 3, 3, 9, 9, 7, 1, 6, 3, 5, 5, 10, 2)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -131L))