R is the histogram in the histogram

I am trying to create 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 It looks like I can do something similar with ggplot, but I don't have ggplot on my system. Is it possible to generate it without ggplot?

+5
source share
1 answer

Update

Here's the best version of the code that can be more easily configured for any numeric number to separate:

 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 will be the same as shown below, with different colors and corresponding labels for the groups:

enter image description here

Original

This may not be ideal for your real data, but it works for your sample and gives 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")) 

What generates:

enter image description here

The idea is to calculate the frequencies and then draw them using a barplot with complex data, rather than trying to use hist() .

+4
source

All Articles