I’m trying to visualise ordinal data with the following structure:
library(ggdist)
library(ggplot2)
library(dplyr)
data(long_data)
srss_question1 <- long_data %>%
filter(type == "1") %>%
dplyr::select(id, day, response)
head(srss_question1)
# A tibble: 6 × 3
id day response
<dbl> <fct> <dbl>
1 1 day7 5
2 1 day2 4
3 1 day0 3
4 2 day7 5
5 2 day2 5
6 2 day0 6
Code to plot figure without colour that works as intended:
srss_question1 %>%
ggplot(aes(y = response, x = day)) +
stat_slab(
data = filter(srss_question1, day == "day7"),
density = "histogram",
breaks = breaks_fixed(width = 0.5),
align = "center",
position = position_nudge(x = 0),
side = "right"
) +
stat_slab(
data = filter(srss_question1, day == "day2"),
density = "histogram",
breaks = breaks_fixed(width = 0.5),
align = "center",
position = position_nudge(x = 0),
side = "right"
) +
stat_slab(
data = filter(srss_question1, day == "day0"),
density = "histogram",
breaks = breaks_fixed(width = 0.5),
align = "center",
position = position_nudge(x = 0),
side = "right",
) +
geom_line(
aes(group = id),
size = 0.25,
position = position_jitter(h = 0.05, w = 0.05),
alpha = 0.25
) +
scale_y_continuous(limits = c(-0.25, 7.5), breaks = 0:10) +
labs(x = "Day", y = "Question 1 Responses") +
theme_bw()
Which produces this figure:
Figure Gray
I’d like to see if mapping the Viridis colour palette to this figure helps with the visualisation and have coded it as such here:
srss_question1 %>%
ggplot(aes(x = day, y = response, fill = factor(response))) +
stat_slab(
data = filter(srss_question1, day == "day7"),
density = "histogram",
breaks = breaks_fixed(width = 0.5),
align = "center",
side = "right",
position = position_nudge(x = 0)
) +
stat_slab(
data = filter(srss_question1, day == "day2"),
density = "histogram",
breaks = breaks_fixed(width = 0.5),
align = "center",
side = "right",
position = position_nudge(x = 0)
) +
stat_slab(
data = filter(srss_question1, day == "day0"),
density = "histogram",
breaks = breaks_fixed(width = 0.5),
align = "center",
side = "right",
position = position_nudge(x = 0)
) +
geom_line(
aes(group = id),
linewidth = 0.25,
position = position_jitter(h = 0.05, w = 0.05),
alpha = 0.25
) +
scale_fill_viridis_d(option = "D", direction = 1) +
scale_y_continuous(limits = c(-0.25, 7.5), breaks = 0:10) +
labs(x = "Day", y = "Question 1 Responses") +
theme_bw()
But this removes the bars from the figure like here:
Figure colour
Why do my stat_slab bars disappear when mapping the colour palette to the figure? Any insights on how to fix the code much appreciated 🙂
Please let me know if more information is needed.
The problem is that your y axis is continuous, and when you are trying to fill by discrete values (factors) your bars became of width 0. You can see this if you replace fill
aesthetics by colour
(also the corresponding scale).
On the other hand, for this type of geom you cannot use discrete y axis. If you want wide bars, you probably need to think about other way to plot this, with geom_bar
or geom_rect
.