Remi.b - 3 months ago 21

R Question

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

Here is a data.table

`set.seed(12)`

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

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

```
library(data.table)
set.seed(12)
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)
DT[,colour:=cut(hello,c(-Inf,reclassification,Inf))]
library(ggplot2)
p <- ggplot(DT,aes(x=categories,fill=colour)) + geom_bar()
print(p)
```