Why is histogram calling 2, value 1.5

here is my data structure

str(databody)
'data.frame':   259 obs. of  2 variables:
 $ Genes_bodies: Factor w/ 259 levels "FBgn0000045",..: 142 5 82 118 107 241 165 66 78 67 ...
 $ Frenq       : int  4 4 4 4 4 4 5 4 3 2 ...
 - attr(*, "na.action")=Class 'omit'  Named int [1:1109] 260 261 262 263 264 265 266 267 268 269 ...
  .. ..- attr(*, "names")= chr [1:1109] "260" "261" "262" "263" ...

h = hist(databody$Frenq)
h$density = h$counts/sum(h$counts)*100
plot(h,freq=F, col= "red")

dput(h)
structure(list(breaks = c(1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 
5.5, 6), counts = c(28L, 47L, 0L, 65L, 0L, 69L, 0L, 42L, 0L, 
8L), density = c(10.8108108108108, 18.1467181467181, 0, 25.0965250965251, 
0, 26.6409266409266, 0, 16.2162162162162, 0, 3.08880308880309
), mids = c(1.25, 1.75, 2.25, 2.75, 3.25, 3.75, 4.25, 4.75, 5.25, 
5.75), xname = "databody$Frenq", equidist = TRUE), .Names = c("breaks", 
"counts", "density", "mids", "xname", "equidist"), class = "histogram")

str(h)
List of 6
 $ breaks  : num [1:11] 1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 ...
 $ counts  : int [1:10] 28 47 0 65 0 69 0 42 0 8
 $ density : num [1:10] 10.8 18.1 0 25.1 0 ...
 $ mids    : num [1:10] 1.25 1.75 2.25 2.75 3.25 3.75 4.25 4.75 5.25 5.75
 $ xname   : chr "databody$Frenq"
 $ equidist: logi TRUE
 - attr(*, "class")= chr "histogram"

h$counts 47 shouldnt fall under 1.5 but under counts 2? What am I doing wrong and missing? Also is there a way to make a single stacked barplot with h$densities?

Answers


hist will break the data up into continuous intervals, rather than specific integer values. If your data are all integer values, then maybe one of these options would work better for you:

# Fake data
set.seed(1095)
x = sample(1:10, 100, replace=TRUE)

# Base graphics
plot(table(x), lwd=3)

# ggplot2
library(ggplot2)

ggplot(as.data.frame(x), aes(factor(x))) +
  geom_bar(width=0.5, fill="grey30")

I'm not sure a stacked bar plot works so well here, but here's how to create one in ggplot:

ggplot(as.data.frame(table(x)), aes(x="my_label", y=Freq, fill=Freq)) +
  geom_bar(stat='identity', position="stack") +
  geom_text(aes(label=paste0(x, ": Freq = ", Freq), y=cumsum(Freq) - 0.5*Freq),
            colour="white") +
  labs(x="")

Need Your Help

How get intent data in internal class

android android-intent

I have a activity where I have some data from previous activity: