Histogram R. Changing the values of the axes!

3

I have a variable with the following value:

> pontos
        c         d         b         a 
0.6666667 1.0000000 0.3333333 0.6666667

hist (points, main="Points", xlab="p", ylab="f")

The result is:

Myx-axisvalueswillalwaysbebetween0and1.Alreadythey-axisvaluescanchangefrom0to100000000.Iwouldliketoreverse!Thatis,thevaluesbetween0and1remainontheYaxisandthefrequencyvalues,whichistheamountthateachvalueappears,stayontheXaxis.

Isawaparameteroftype,horizontal=TRUE.ThisisnotwhatIwant,itissimplytochangethevaluesoftheaxes,valuesthatareintheXaxisappearinYandviceversa.Iknowthatwiththisthehistogramstructurewillchange,butitdoesnotmatter,becausetheY-axiswillbecomeimmenseasthevaluesincrease,andIfindthosevaluesmoreinterestingtostayontheX-axis.

It'salmostabarplot!TheonlythingIdonotwantisthatwhenthereisavaluerepeat,asintheexamplebelow,nottwobarsappear,butonlyone,andontheXaxisindicatingthatthereare2barswithvalueinformedontheYaxis.

    
asked by anonymous 21.12.2016 / 02:24

1 answer

2

The trick here is to realize that it is possible to make a traditional histogram and save the information about its construction in an object. For example,

pontos <- c(0.6666667, 1.0000000, 0.3333333, 0.6666667)
histograma <- hist(pontos)
str(histograma)
List of 6
 $ breaks  : num [1:5] 0.2 0.4 0.6 0.8 1
 $ counts  : int [1:4] 1 0 2 1
 $ density : num [1:4] 1.25 0 2.5 1.25
 $ mids    : num [1:4] 0.3 0.5 0.7 0.9
 $ xname   : chr "pontos"
 $ equidist: logi TRUE
 - attr(*, "class")= chr "histogram"

So, just use some of the information in the histograma object to create a new frequency graph rotated by 90 degrees:

plot(NULL, type = "n", xlim = c(0, max(histograma$counts)),
ylim = c(range(histograma$breaks)), xlab="Frequência", ylab="Pontos")

rect(0, histograma$breaks[1:(length(histograma$breaks) - 1)], 
histograma$counts, histograma$breaks[2:length(histograma$breaks)])

You can create a function based on this code above to make your job easier every time a similar histogram needs to be created.

    
21.12.2016 / 13:25