Remi.b Remi.b - 1 year ago 71
R Question

ggplot, count of the number of occurences of a categorical variable and splitting this count according to a countinuous variable

I'm trying hard to achieve something with ggplot but I keep failing !...

Here is a data.table

reclassification = c(0,4,7,15)

I'm trying to do the following plot:

y-axis : The 3 categories (a,b,c)

x-axis : A count of the number of times each category is found

colour/shape : The column "hello" reclassified according the vector "reclassification". There should have 3 colours in my example. One for the count of "categories" for 0 < hello < 4, one for 4 < hello < 7, one for 7 < hello < 15

Note: This plot can be made of bars, lines, volumes, several different plots, etc... (I would actually appreciate trying some different solutions)

Thanks a lot for your help !

Answer Source

DT <- data.table(categories=c('c','a','b','a','a','c','b','b','a','c','c','b'),hello=runif(12,0,15))
reclassification <- c(0,4,7,15)

p <- ggplot(DT,aes(x=categories,fill=colour)) + geom_bar()

enter image description here