如何在R条形图中显示所有x标签? [英] How to display all x labels in R barplot?

查看:53
本文介绍了如何在R条形图中显示所有x标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个基本问题,但我找不到答案.我在一个面板内生成了大约 9 个条形图,每个条形图有大约 12 个条形图.我在输入中提供了所有 12 个标签,但 R 仅命名备用条.这显然是由于 R 中的某些默认设置需要更改,但我找不到它.

This is a basic question but I am unable to find an answer. I am generating about 9 barplots within one panel and each barplot has about 12 bars. I am providing all the 12 labels in my input but R is naming only alternate bars. This is obviously due to to some default setting in R which needs to be changed but I am unable to find it.

推荐答案

如果在 plot() 调用.这个参数和下面提到的其他参数在 ?par 中描述,它设置绘图设备的图形参数.这会将文本旋转 90 度.否则,您将需要使用 xaxt=n"(以抑制刻度和标签),然后通过单独调用 axis(1, at= <some数值向量>,标签=<某些字符向量>).

You may be able get all of the labels to appear if you use las=2 inside the plot() call. This argument and the others mentioned below are described in ?par which sets the graphical parameters for plotting devices. That rotates the text 90 degrees. Otherwise, you will need to use xaxt="n" (to suppress ticks and labels) and then put the labels in with a separate call to axis(1, at= <some numerical vector>, labels=<some character vector>).

#  midpts <- barplot( ... ) # assign result to named object
axis(1, at = midpts, labels=names(DD), cex.axis=0.7) # shrinks axis labels

另一种方法是先收集中点,然后使用 text()xpd=TRUE 允许文本出现在绘图区域之外,srt 将文本旋转的某个角度作为命名参数来控制文本旋转的程度:

Another method is to first collect the midpoints and then use text() with xpd=TRUE to allow text to appear outside the plot area and srt be some angle for text rotation as named arguments to control the degree of text rotation:

text(x=midpts, y=-2, names(DD), cex=0.8, srt=45, xpd=TRUE)

需要使用绘制区域中的坐标选择 y 值.

The y-value needs to be chosen using the coordinates in the plotted area.

复制一个有用的评论:对于不知道这些参数的作用的未来读者:las=2 将标签逆时针旋转 90 度.此外,如果您需要缩小字体,您可以使用 cex.names=.5 缩小尺寸

Copying a useful comment: For future readers who don't know what these arguments do: las=2 rotates the labels counterclockwise by 90 degrees. furthermore, if you need to reduce the font you can use cex.names=.5 to shrink the size down

这篇关于如何在R条形图中显示所有x标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆