【问题标题】:Formatting x-axis with histogram in R在R中用直方图格式化x轴
【发布时间】:2014-03-12 07:41:14
【问题描述】:

我想要 x 轴从 1 到 20,y 轴从 1 到 6。 我的数据:

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")

我使用以下命令来绘制直方图:

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)
 }

但使用上述命令后,我在 x 轴和 y 轴上得到不同的数字。

【问题讨论】:

标签: r plot drawing histogram


【解决方案1】:

我不明白你的情节会是什么样子。从您提供的问题和数据中不清楚。

我已经尝试过绘制它。如果您认为这是要走的路,请发表评论。

考虑到dt 是你的data.frame

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

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

【讨论】:

    【解决方案2】:

    您可以在hist 函数中使用xlimylim 参数并使用以下方法控制轴 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)  
    }
    

    您真的希望您的 y 轴从 1 变为 6 吗?这将切断部分钢筋。 此外,您迭代所有 20 行以获得具有 12 个图的网格。上面的代码给出了以下情节:

    【讨论】:

      猜你喜欢
      • 2012-08-23
      • 2019-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-02
      • 1970-01-01
      • 1970-01-01
      • 2012-01-18
      相关资源
      最近更新 更多