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, 0, 1, 0, 0, 0, 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))

![enter image description here

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:

![enter image description here

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))

enter image description here

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.

Tom
  • 2,173
  • 1
  • 17
  • 44

1 Answers1

3

Try a summarising count first. Apologies again for lack of image - using online console with reduced facilities.

library(tidyverse)
mydf <- data.frame(thevalues, mistakes)

mycount <- count(mydf, thevalues, mistakes)

ggplot() +
  geom_col(data = mycount, aes(thevalues, n, fill = as.character(mistakes))) +
  geom_density(data = mydf, aes(thevalues, 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))
tjebo
  • 21,977
  • 7
  • 58
  • 94
  • Thank you very much! I will delete the other question. – Tom Jan 29 '21 at 12:12
  • Okay, now I cannot figure out how to add another variable. I am so bad at this. As you might remember from the very poorly written earlier question I wanted to do this when apart from `thevalues` there are more values let's say, `thevalues_II`. Is there any easy way to add this (I will make an edit)? – Tom Jan 29 '21 at 12:27
  • 1
    @Tom you're looking for "dodge and stack at the same time". this is currently difficult - and I find in terms of visualisation also maybe confusing. Check https://stackoverflow.com/questions/12715635/ggplot2-bar-plot-with-both-stack-and-dodge for workarounds – tjebo Jan 29 '21 at 12:33
  • I have another idea in that case.. I think I can just subtract the penalty count from the other counts, and then sum them instead in the plot, or would that still be "dodge and stack at the same time"? – Tom Jan 29 '21 at 12:37
  • I keep on circling the solution.. Now I have both (https://stackoverflow.com/questions/65955368/making-a-bar-plot-with-stack-and-dodge), but I cannot figure out how to get the other options back.. – Tom Jan 29 '21 at 13:27