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

标签 r formatting plot ggplot2

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

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

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 时,标签“研究生”重叠在绘图区域上。

这里有几个问题:

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

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

假设您正在尝试绘制比例(而不是原始数字)

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

关于r - 在 R 中绘制 2D 表格对象时控制轴标签的格式/位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10727373/

相关文章:

r - R 中的逻辑回归图给出一条直线而不是 S 形曲线

r - 动态公式不适用于startsWith和colnames

r - 如何在没有可用分隔符的情况下从 mmddyyyy 字符串中分离年份?

python - rpy2 下的立体回归 : "subscript out of bounds" error

html - 如何将两张图片拼接在一起

PHPMailer 忽略\r\n

r - 更改列表中的每个向量

javascript - 打印山脉算法

python - python 上的多个图

matlab - 内部矩阵维度必须一致?