Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - histogram within histogram

Tags:

r

I am trying to generate a histogram from below data

a   11
a   14
a   23
b   12
b   21
c   17
c   14
c   29
c   22
c   25

This is my target plot

enter image description here Looks like i can do something like this with ggplot but i dont have ggplot in my system. Is it possible to generate it without ggplot?

like image 476
SAN Avatar asked Sep 28 '22 03:09

SAN


1 Answers

Update

Here's a better version of the code which can be more easily adjusted to any number ranges to separate by:

dat <- data.frame(c1 = c("a", "a", "a", "b", "b", rep("c", 5)), c2=c(11, 14, 23, 12, 21, 17, 14, 29, 22, 25))

groups <- levels(dat$c1)
nranges <- 2
limits <- c(10, 20, 30) #Must have length equal to nranges + 1
intervals <- sapply(1:nranges, function(i) paste0(limits[i], "-", limits[i+1]))

frequencies <- sapply(1:nranges, function(i) sapply(groups, function(j) sum(dat[dat$c2>limits[i] & dat$c2<limits[i+1],1]==j)))
# Or using table(). One of them might be faster than the other for large data
#frequencies <- sapply(1:nranges, function(i) rowSums(table(dat[dat$c2>limits[i] & dat$c2<limits[i+1],])))

barplot(frequencies, beside = TRUE, col=1:length(groups), names.arg=intervals)

The result is the same as below with different colors and appropriate labels for the groups:

enter image description here

Original

This might not be ideal for your real data, but it works for your sample and will give you a start:

dat <- data.frame(c1 = c("a", "a", "a", "b", "b", rep("c", 5)), c2=c(11, 14, 23, 12, 21, 17, 14, 29, 22, 25))

groups <- levels(dat$c1)
dat1 <- sapply(groups, function(i) sum(dat[dat$c2>10 & dat$c2<20,1]==i))
dat2 <- sapply(groups, function(i) sum(dat[dat$c2>20 & dat$c2<30,1]==i))

barplot(matrix(c(dat1, dat2), ncol=2), beside = TRUE, col=c("Red", "Green", "Blue"))

Which generates:

enter image description here

The idea is to calculate the frequencies and then plot those using barplot with stacked data side by side, instead of trying to use hist().

like image 66
Molx Avatar answered Oct 12 '22 01:10

Molx