首页 文章

在R中绘制2D表格对象时控制轴标签的格式/位置

提问于
浏览
2

我使用 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))

我想对此调用创建的绘图中的轴标签进行三处更改 .

首先,在顶部交替标记"Freshman","Sophomore"等标签的高度,这样它们就不会总是方便缩短标签 . 我想过关闭轴标签并使用 text() 功能手动添加它们 . 但是,在绘制表对象(R给出"extra argument(s) 'xaxt' will be disregarded"消息)时,将 xaxt = 'n' 添加到 plot 调用显然不起作用 .

另一个选项是通过设置 las=2 来旋转x轴标签 . 但是,我更喜欢将标签放在水平位置以便于阅读 . 不过,我应该注意,当我设置 las=2 时,标签"Graduate Student"重叠到绘图区域 .

所以这里有几个问题:

  • 如何关闭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")

1 回答

  • 3

    一种使用ggplot的方法

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

    鉴于您试图绘制比例(而不是原始数字)

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

    要了解有关可用选项的更多信息,请查看 ?opts

    以下内容改编自learning r

    使条宽与总计数成比例

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

相关问题