We have this data frame
t = c(0, 1, 2, 3, 4, 5, 6)
x = c("o", "o", "b", "b", "o", "o", "o")
df = data.frame(t, x)
and would like to have a flipped stacked bar plot using ggplo2
in R
.
I am using this code
colors <- c("o" = "black", "b" = "white")
df_long <- pivot_longer(df, cols = c("x"), names_to = "variable", values_to = "value")
df_long$value <- factor(df_long$value, levels = c("o", "b"))
to define the colors, and this code for plotting
ggplot(df_long, aes(x = variable, fill = value)) +
geom_bar(position = position_stack(reverse = FALSE)) +
scale_fill_manual(values = colors) +
coord_flip()
Now the problem is that the x
bar should be black-white-black
but what I get is white-black
Could someone help please?
New contributor
Vultra Uiolet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.