Safiya Kkmha
Safiya Kkmha

Reputation: 55

Formatting x-axis with histogram in R

I want x- axis from 1 to 20 and y-axis from 1 to 6. My data:

structure(list(HEI.ID = structure(c(12L, 9L, 14L, 19L, 20L, 1L, 
7L, 5L, 11L, 3L, 10L, 18L, 2L, 8L, 6L, 15L, 13L, 17L, 4L, 16L
), .Label = c("BF", "CC", "DC", "ER", "IM", "MC", "ME      ", 
"MM", "MO", "OC", "OM", "OP", "SB", "SD", "SH", "SL", "SN", "TH", 
"UN", "WS"), class = "factor"), X2007 = c(18L, 14L, 15L, 20L, 
12L, 6L, 17L, 2L, 4L, 11L, 16L, 1L, 9L, 8L, 13L, 4L, 10L, 6L, 
3L, 19L), X2008 = c(20L, 9L, 16L, 18L, 8L, 17L, 15L, 6L, 3L, 
14L, 19L, 1L, 2L, 12L, 5L, 13L, 11L, 7L, 4L, 10L), X2009 = c(20L, 
13L, 17L, 8L, 4L, 9L, 19L, 12L, 2L, 11L, 16L, 1L, 2L, 7L, 6L, 
18L, 5L, 15L, 9L, 14L), X2010 = c(20L, 13L, 16L, 13L, 7L, 15L, 
19L, 8L, 3L, 9L, 18L, 1L, 5L, 11L, 12L, 6L, 10L, 4L, 2L, 17L), 
    X2011 = c(20L, 2L, 16L, 14L, 6L, 10L, 17L, 8L, 3L, 15L, 19L, 
    1L, 4L, 18L, 13L, 11L, 8L, 12L, 4L, 7L), X2012 = c(20L, 12L, 
    19L, 13L, 8L, 14L, 15L, 10L, 11L, 9L, 17L, 2L, 7L, 18L, 5L, 
    16L, 3L, 4L, 6L, 1L)), .Names = c("HEI.ID", "X2007", "X2008", 
"X2009", "X2010", "X2011", "X2012"), row.names = c(NA, -20L), class = "data.frame")

I use the following commands to draw histograms:

par(mfrow = c(3,4))
for(i in  1:20){
  print(i)
  hist(as.numeric(HEIrank11[i,-1]),nclass=12,,main='students/faculty',
       xlab = STOF[i,1],cex.lab=1, cex.axis=1, cex.main=1, cex.sub=1)
 }

But after using above commands, I get different number in x- axis and y-axis.

Upvotes: 0

Views: 847

Answers (2)

user1981275
user1981275

Reputation: 13382

You could use xlim and ylim parameters in the hist function and control the axes using axis:

par(mfrow = c(3,4))
for(i in  1:12){
  print(i)  
  hist(as.numeric(HEIrank11[i,-1]),nclass=12,,main='students/faculty',
       xlim=c(0, 21), ylim=c(0,6), xaxt='n', yaxt='n')
  axis(1, at=c(0, 10, 20))
  axis(2, at=0:6)  
}

Do you really want your y-axis to go from 1 to 6? This will cut off parts of the bars. Also, you iterate over all 20 rows for a grid with 12 plots. The code above gives the following plot:

enter image description here

Upvotes: 0

Paulo E. Cardoso
Paulo E. Cardoso

Reputation: 5856

I don't understand what your plot would looks like. It's not clear from your question and data provided.

I've tried to plot it. Please comment if you think it's the way to go.

Considering dt is your data.frame

library(reshape)
dt <- melt(dt)
library(ggplot2)
ggplot(aes(x=HEI.ID, y = value, fill = variable), data = dt) +
  geom_bar(stat = 'identity')

histogram

or

ggplot(aes(x=HEI.ID, y = value, fill = variable), data = dt1) +
  geom_bar(stat = 'identity') +
  facet_grid(variable ~.)

enter image description here

Upvotes: 1

Related Questions