Reputation: 427
One category in my stacked bar chart doesn't have any color, although it has the right color in the legend. Does anyone have an idea how this can be solved? It concerns "date" = 2020-11-7 and "Altersklassen_num" = 1.
In ggplot (the first step) the graph looks perfectly fine. Also when I remove dynamicTicks (but then the x-scale gets very messy).
My data
structure(list(date = structure(c(18567, 18568, 18569,
18569, 18570, 18571, 18571, 18571, 18572, 18572, 18572, 18573,
18573, 18574, 18574, 18575, 18575, 18575, 18576, 18576, 18577,
18578, 18578, 18579, 18580, 18580, 18581, 18581, 18582, 18582,
18583, 18584, 18584, 18585, 18585, 18586, 18586, 18587, 18587,
18588, 18588, 18589, 18589, 18589, 18590, 18591, 18592, 18592,
18593, 18593, 18594, 18594, 18595, 18595, 18596, 18596, 18596,
18597, 18598, 18598, 18599, 18599, 18600, 18600, 18601, 18601,
18601, 18602, 18602, 18603, 18603, 18603), class = "Date"), Altersklassen_num = structure(c(2L,
1L, 1L, 2L, 1L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 1L, 2L, 1L, 2L,
3L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 3L, 1L, 1L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 3L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 3L, 1L, 2L, 1L, 2L, 4L), .Label = c("4", "3", "2", "1"), class = "factor"),
Altersklasse1 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1, 1), Altersklasse2 = c(0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 0,
0, 0, 0, 0), Altersklasse3 = c(2, 0, 4, 4, 0, 3, 3, 3, 3,
3, 3, 1, 1, 4, 4, 1, 1, 1, 3, 3, 0, 2, 2, 0, 3, 3, 2, 2,
3, 3, 0, 2, 2, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 0, 0, 2,
2, 4, 4, 2, 2, 4, 4, 3, 3, 3, 0, 2, 2, 3, 3, 1, 1, 1, 1,
1, 1, 1, 3, 3, 3), Altersklasse4 = c(0, 3, 7, 7, 6, 5, 5,
5, 7, 7, 7, 3, 3, 11, 11, 9, 9, 9, 5, 5, 6, 2, 2, 5, 3, 3,
2, 2, 4, 4, 2, 3, 3, 4, 4, 2, 2, 3, 3, 5, 5, 3, 3, 3, 3,
3, 2, 2, 7, 7, 3, 3, 7, 7, 4, 4, 4, 7, 8, 8, 5, 5, 4, 4,
6, 6, 6, 1, 1, 3, 3, 3), Anz = c(2, 3, 7, 4, 6, 5, 3, 1,
7, 3, 1, 3, 1, 11, 4, 9, 1, 2, 5, 3, 6, 2, 2, 5, 3, 3, 2,
2, 4, 3, 2, 3, 2, 4, 1, 2, 2, 3, 2, 5, 3, 3, 3, 1, 3, 3,
2, 2, 7, 4, 3, 2, 7, 4, 4, 3, 1, 7, 8, 2, 5, 3, 4, 1, 6,
1, 2, 1, 1, 3, 3, 1)), row.names = c(NA, -72L), class = c("tbl_df",
"tbl", "data.frame"))
My code for the plot:
g.barchart<- ggplot(d.data_agg, aes(x= as.Date(date, "%Y-%m-%d"), y=Anz, fill=Altersklassen_num, text=paste("Datum: ", format(as.Date(date),"%d.%m.%Y"), "<br><br>0- bis 49-Jährige: ", Altersklasse1,"<br>50- bis 64-Jährige: ", Altersklasse2,"<br>65- bis 79-Jährige: ", Altersklasse3,"<br>über 80-Jährige: ", Altersklasse4))) +
geom_bar(position="stack",stat="identity",width = 1.0)
g.barchart
fig <- ggplotly(g.barchart,tooltip = c("text"), dynamicTicks = TRUE) %>%
style(hoverinfo = c("text")) %>%
config(displayModeBar = T, locale = "de") %>%
layout(hoverlabel = list(align = "left", bgcolor = "#E5E8E8", font = list(size=14)),
legend = list(orientation = "h", x = 0.15, y = -0.15),
xaxis = list(showgrid = F, ticklen = 10,
tickval = NULL,
ticktext = NULL,
ticks = "outside",
tickmode = "array",
tickformat = '%e.%b',
ticktext = labels,
tickangle = 0,
title = "",
type = "date",
autorange = FALSE,
range = c(as.numeric(as.POSIXct("2020-10-30", format="%Y-%m-%d"))*1000,
as.numeric(as.POSIXct(k.DownDat, format="%Y-%m-%d"))*1000)),
yaxis = list(autorange = FALSE))
fig
Upvotes: 0
Views: 100
Reputation: 21297
Try to complete
the dataframe and then run ggplot
. After that if you run ggplotly
, you will get the missing stacked bar.
d.data_agg <- d.data_agg %>% complete(date, nesting(Altersklassen_num), fill = list(Anz=0))
Upvotes: 1