Search code examples
rhistogram

hist: Set class boundaries on the histogram


When the histogram classes are of different amplitude, I want these values to appear in the graph. In the attached example, 15, 25, 45 and 60 should be displayed.[![enter image description here][1]][1] I would like the blue axis to disappear and the class limits to appear on the abscissa axis.

LI <- sample(10:20,1)
lim<-c(LI)
for(k in 1:3){
a=c(10,20,15)[k]
lim<-c(lim,lim[k]+a)
}


distancia <-c(lim)

densidades <- function(m){
v <- NULL
for (i in 1:m){v <-c(v, c(lim [i+1]-lim [i]))}
v
}


densi<-densidades(3)
intervalos <- limites(3) 
frecuencias <- frecuencias <- sample(150:300, 3)
d <-round(frecuencias/densidades(3),2)

si <-c(rep(distancia[1]),rep(distancia[2],d[1]),rep(distancia[3],d[2]),rep(distancia[4],d[3]))

si1 <-c(distancia[1],distancia[2],distancia[3],distancia[4])

distancia1 <-c(lim[1],lim[2],lim[3],lim[4])

si2 <-c(d[1],d[2],d[3])



hist(si, main="", breaks = c(lim[1],lim[2],lim[3],lim[4]), freq = T,  
 xlab = "Edad", ylab = "Densidades de frecuencias") 
rug(si)
axis(1,
     col = "blue",        # Color línea ejes
     col.ticks = "blue", # Color marcas ejes
     col.axis = "blue")    # Color de las etiquetas
axis(2,
     col = "blue",        # Color línea ejes
     col.ticks = "blue", # Color marcas ejes
     col.axis = "blue")    # Color de las etiquetas
grid(nx = NA, ny = NULL)
text(si1, si2,labels=si2, adj=c(-2, 2))

Solution

  • if I understand you correctly you this is a possible solution for removing the axis and building them up in separate calls:

    # use axes = FALSE
    hist(si, main="", breaks = c(lim[1],lim[2],lim[3],lim[4]), freq = T, axes = FALSE) 
    rug(si)
    # build new axis using vector the informs position of ticks
    axis(1,
           at = lim,
           col = "blue",        # Color línea ejes
           col.ticks = "blue", # Color marcas ejes
           col.axis = "blue")    # Color de las etiquetas
    axis(2,
           col = "blue",        # Color línea ejes
           col.ticks = "blue", # Color marcas ejes
           col.axis = "blue")    # Color de las etiquetas
    grid(nx = NA, ny = NULL)
    text(si1, si2,labels=si2, adj=c(-2, 2))
    

    enter image description here