在R中打印二维表格对象时控制轴标签的格式/位置

在R中打印二维表格对象时控制轴标签的格式/位置,r,formatting,plot,ggplot2,R,Formatting,Plot,Ggplot2,在R中打印二维表时,我希望能够更好地控制轴标签的大小和位置,但打印规则对于表对象的工作方式似乎与数据帧不同。为了使我的问题具体化,我使用dput()在消息末尾粘贴了假数据。运行该代码将创建下面的绘图代码中使用的dept.table对象 在R中的2D表格上运行plot时,会创建马赛克图,如下所示: plot(dept.table, col=c("yellow","blue","red"), las=1, cex=.7, dir=c("h","v"),

在R中打印二维表时,我希望能够更好地控制轴标签的大小和位置,但打印规则对于表对象的工作方式似乎与数据帧不同。为了使我的问题具体化,我使用
dput()
在消息末尾粘贴了假数据。运行该代码将创建下面的绘图代码中使用的
dept.table
对象

在R中的2D表格上运行
plot
时,会创建马赛克图,如下所示:

plot(dept.table, 
     col=c("yellow","blue","red"), 
     las=1, 
     cex=.7, 
     dir=c("h","v"), 
     off=c(8,4))
我想对此调用创建的绘图中的轴标签进行三个更改

首先,将“大一”、“大二”等标签的高度在顶部交替排列,这样它们就不会重叠(我知道我可以缩短它们,但我想找出这个问题,以备将来参考,因为缩短标签并不总是很方便)。我想关闭轴标签,并使用
text()
函数手动添加它们。但是,在打印表对象时,将
xaxt='n'
添加到
plot
调用显然不起作用(R给出了一条“额外参数”'xaxt'将被忽略”消息)

另一个选项是通过设置
las=2
旋转x轴标签。然而,为了便于阅读,我更喜欢将标签水平放置。不过,我应该注意,当我设置las=2时,“研究生”标签重叠在绘图区域上

因此,这里有几个问题:

  • 如何关闭x轴标签的打印,以便手动添加标签
  • 更好的是,有没有一种更优雅的方法让标签上下交替,这样它们就不会重叠,而不必手动设置每个标签的位置
  • 如果标签与打印区域重叠,是否有消除重叠的方法(缩短标签除外)
  • 有没有办法使x轴和y轴标签具有不同的文本大小?我想要绘制的实际绘图有更多的部门(垂直轴),因此我希望y轴的文本大小小于x轴的文本大小。轴文本大小的唯一选项似乎是
    cex
    ,它可以调整两个轴。有没有办法分别调整每个轴

  • y轴标签左对齐。有没有办法使它们正确对齐,这样它们就会靠近绘图区域

  • 顺便说一句,虽然我用基本图形绘制了这个图,但我想知道是否有更好的方法用lattice、ggplot2或其他软件包来绘制

    dept.table = structure(c(10, 15, 20, 200, 5, 15, 35, 200, 49, 15, 25, 20, 
    250, 5, 12, 34, 150, 30, 50, 108, 75, 800, 32, 39, 135, 400, 
    195, 80, 99, 64, 700, 47, 41, 134, 350, 160, 5, 10, 5, 110, 5, 
    4, 10, 30, 13), .Dim = c(9L, 5L), .Dimnames = structure(list(
        c("Anthropology", "Economics", "Mathematics", "Business Administration", 
        "Geography", "Special Education, & Deaf Studies", "History", 
        "Kinesiology & Health Science", "Family & Consumer Sciences"
        ), c("Freshman", "Sophomore", "Junior", "Senior", "Graduate Student"
        )), .Names = c("", "")), class = "table")
    

    一种使用ggplot的方法

    # convert to a data frame
    dept_data <- as.data.frame(dept.table)
    

    我喜欢它看起来多么光滑(这是ggplot的一大优势!)。有没有办法使酒吧的宽度与某个系的学生人数成比例?与条形图相比,马赛克图的一个优点是它们增加了第二维度的信息。我没有注意到这一点。我现在编辑了答案,并给出了一个可能有用的答案。感谢您为此付出的努力。看起来ggplot在每个水平条之间保持一个恒定的垂直空间。有没有办法减少空间,让它看起来更像一个“标准”的马赛克图?这可能是ggplot2中添加的“geom_马赛克”的核心!我又调整了一下。问题是使用
    facet\u grid(…,space='free'
    coord\u flip()
    结合使用。我重做了第二种方法,以避免使用
    coord\u flip()
    。我从未使用过它,但您可能会在
    vcd
    包中发现一些有用的东西——“vcd”用于“可视化分类数据”
    # add proportions
    library(plyr)
    dept_data_prop <- ddply(dept_data, .(Var1), mutate, prop = Freq /sum(Freq))
    
    
    library(ggplot2)
    ggplot(dept_data_prop, aes(x= Var1, y = prop, colour = Var2, fill = Var2)) + 
      geom_bar() + 
      coord_flip() + 
      facet_wrap(~Var1, scales = 'free', ncol = 1) + 
      opts(strip.background =theme_blank(), strip.text.x = theme_blank(), 
           strip.text.y = theme_text(), axis.ticks = theme_blank(), 
           axis.title.x = theme_blank(), axis.text.x = theme_blank()) + xlab('') + 
      scale_fill_brewer('Student',  type = 'div', palette = 5) + 
      scale_colour_brewer('Student', type = 'div', palette = 5) + 
      scale_x_discrete(expand = c(0,0)) + 
      scale_y_continuous(expand=c(0,0))
    
    dept_data <- as.data.frame(dept.table)
    names(dept_data) <- c('department', 'student', 'count')
    
    
    
    dept_prop <- ddply(dept_data, .(department), mutate, 
                   prop_department = count / sum(count),
                   max_department = cumsum(prop_department),
                   min_department = max_department - prop_department,
                   total_department = sum(count))
    
    
    dept_prop <- mutate(dept_prop, prop_total = total_department / sum(count))
    
    dept_prop <- ddply(dept_prop, .(student), mutate, 
       max_total = cumsum(prop_total), 
       min_total = max_total - prop_total)
    
    
    
    ggplot(dept_prop, aes(xmin = min_department, xmax = max_department, 
                          ymin = min_total, ymax = max_total)) +
     geom_rect(aes(colour = student, fill =student)) + 
     facet_grid(department~., scales = 'free', space = 'free') +
     opts(strip.background =theme_blank(), 
       strip.text.y = theme_text(hjust = 0.05), axis.ticks = theme_blank(), 
       axis.title.x = theme_blank(), axis.text.x = theme_blank(), 
       axis.title.y = theme_blank(), axis.text.y = theme_blank(), 
       legend.position = 'bottom', legend.justification = 'left',
       panel.border = theme_blank(), panel.background = theme_blank(), 
       panel.grid.major = theme_blank(), panel.grid.minor = theme_blank()) +
       xlab('') + 
     scale_fill_brewer('Student',  palette = 'Set1') + 
     scale_colour_brewer('Student', palette = 'Set1') + 
     scale_x_continuous(expand = c(0, 0)) + 
     scale_y_continuous(expand = c(0, 0))