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0, 1, 0, 1, 0,
0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0,
0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1,
0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
0), label = NA_character_, class = c("labelled", "numeric"))
I want to create a histogram of thevalues
like so:
df <- data.frame(value = c(A),
variable = rep(c("thevalues"), each = length(A)))
ggplot(df, aes(value, fill = variable)) +
geom_density(aes(y = ..count..), size = 0.7, alpha = 0.1) +
geom_bar(position = "dodge") +
scale_fill_brewer(palette = "Set1") +
scale_x_continuous(breaks = c(1:9), labels = c(1:9)) +
theme(legend.title = element_blank(), legend.position = c(0.1, 0.85))
However, I would like to see the mistakes
as part of these bars:
table(thevalues, mistakes)
mistakes
thevalues 0 1
1 1 1
2 1 0
3 1 1
4 9 2
5 10 4
6 17 8 # The total height of the bar is 25, 8 have a different colour.
7 24 16 # The total height of the bar is 40, 16 have a different colour.
8 33 16 # The total height of the bar is 49, 16 have a different colour.
9 49 14 # The total height of the bar is 63, 14 have a different colour.
Something like this:
EDIT:
The solution works perfectly, but I would really like to do this when there are two variables in the histogram:
thevalues_II <- structure(c(9, 9, 9, 8, 8, 9, 6, 9, 8, 8, 6, 9, 9, 9, 6, 7, 9,
7, 8, 9, 7, 9, 9, 8, 7, 9, 8, 7, 8, 9, 8, 9, 9, 9, 9, 7, 9, 7,
8, 9, 7, 7, 8, 4, 6, 9, 7, 7, 9, 9, 9, 8, 9, 8, 9, 9, 4, 8, 9,
8, 7, 9, 9, 8, 7, 8, 9, 8, 2, 7, 8, 8, 8, 8, 8, 6, 4, 9, 9, 8,
3, 7, 3, 8, 8, 9, 7, 9, 5, 6, 7, 8, 9, 8, 9, 9, 9, 9, 9, 9, 9,
7, 3, 7, 9, 7, 7, 7, 8, 8, 9, 9, 8, 8, 9, 6, 9, 9, 6, 7, 8, 7,
8, 9, 9, 7, 6, 8, 7, 9, 6, 5, 8, 8, 7, 9, 8, 9, 9, 7, 9, 7, 9,
8, 7, 9, 4, 8, 7, 7, 9, 9, 9, 9, 9, 4, 9, 9, 6, 7, 6, 7, 8, 9,
8, 9, 5, 9, 8, 8, 8, 9, 9, 6, 8, 8, 8, 8, 8, 8, 7, 8, 9, 9, 9,
7, 4, 8, 7, 7, 9, 8, 8, 7, 5, 8, 9, 8, 8, 9, 8, 5, 8, 9, 8, 9,
7), label = NA_character_, class = c("labelled", "numeric"))
df <- data.frame(value = c(thevalues, thevalues_II),
variable = rep(c("tax", "truth"), each = length(A)))
ggplot(df, aes(value, fill = variable)) +
geom_density(aes(y = ..count..), size = 0.7, alpha = 0.3) +
geom_bar(position = "dodge") +
scale_fill_brewer(palette = "Set1") +
theme(legend.title = element_blank(), legend.position = c(0.1, 0.85))
I tried:
library(tidyverse)
mydf <- data.frame(thevalues, mistakes)
mycount <- count(mydf, thevalues, thevalues_II, mistakes)
ggplot() +
geom_col(data = mycount, aes(thevalues, thevalues_II, n, fill = as.character(mistakes))) +
geom_density(data = mydf, aes(thevalues, thevalues_II, y = ..count..), size = 0.7, alpha = 0.1) +
scale_fill_brewer(palette = "Set1") +
theme(legend.title = element_blank(), legend.position = c(0.1, 0.85))
But that does not work.