如何使我的垂直标签适合我的绘图窗口?

时间:2022-01-25 14:56:54

I'm creating a histogram in R which displays the frequency of several events in a vector. Each event is represented by an integer in the range [1, 9]. I'm displaying the label for each count vertically below the chart. Here's the code:

我在R中创建了一个直方图,它显示了向量中几个事件的频率。每个事件由[1,9]范围内的整数表示。我正在图表的垂直下方显示每个计数的标签。这是代码:

hist(vector, axes = FALSE, breaks = chartBreaks)
axis(1, at = tickMarks, labels = eventTypes, las = 2, tick = FALSE) 

Unfortunately, the labels are too long, so they are cut off by the bottom of the window. How can I make them visible? Am I even using the right chart?

不幸的是,标签太长了,所以它们被窗户的底部切掉了。我怎样才能让它们可见?我甚至使用正确的图表?

4 个解决方案

#1


9  

This doesn't sound like a job for a histogram - the event is not a continuous variable. A barplot or dotplot may be more suitable.

这听起来不像是直方图的工作 - 事件不是连续变量。条形图或点图可能更合适。

Some dummy data

一些虚拟数据

set.seed(123)
vec <- sample(1:9, 100, replace = TRUE)
vec <- factor(vec, labels = paste("My long event name", 1:9))

A barplot is produced via the barplot() function - we provide it the counts of each event using the table() function for convenience. Here we need to rotate labels using las = 2 and create some extra space of the labels in the margin

通过barplot()函数生成条形图 - 为方便起见,我们使用table()函数为每个事件提供计数。在这里,我们需要使用las = 2旋转标签,并在边距中创建一些额外的标签空间

## lots of extra space in the margin for side 1
op <- par(mar = c(10,4,4,2) + 0.1)
barplot(table(vec), las = 2)
par(op) ## reset

A dotplot is produced via function dotchart() and has the added convenience of sorting out the plot margins for us

点图是通过函数dotchart()生成的,并且为我们排序绘图边距提供了额外的便利

dotchart(table(vec))

The dotplot has the advantage over the barplot of using much less ink to display the same information and focuses on the differences in counts across groups rather than the magnitudes of the counts.

点图比使用更少墨水显示相同信息的条形图具有优势,并且侧重于组间计数的差异而不是计数的大小。

Note how I've set the data up as a factor. This allows us to store the event labels as the labels for the factor - thus automating the labelling of the axes in the plots. It also is a natural way of storing data like I understand you to have.

请注意我如何将数据设置为一个因素。这允许我们将事件标签存储为因子的标签 - 从而自动标记图中的轴。它也是一种自然的方式来存储我理解你拥有的数据。

#2


10  

Look at help(par), in particular fields mar (for the margin) and oma (for outer margin). It may be as simple as

看看帮助(标准杆),特别是mar(保证金)和oma(保证金)。它可能很简单

par(mar=c(5,3,1,1))   # extra large bottom margin
hist(vector, axes = FALSE, breaks = chartBreaks)
axis(1, at = tickMarks, labels = eventTypes, las = 2, tick = FALSE) 

#3


0  

Perhaps adding \n into your labels so they will wrap onto 2 lines? It's not optimal, but it may work.

也许在你的标签中添加\ n,这样它们就会包裹在2行上?它不是最佳的,但可能有效。

#4


0  

You might want to look at this post from Cross Validated

您可能希望从Cross Validated查看此帖子

#1


9  

This doesn't sound like a job for a histogram - the event is not a continuous variable. A barplot or dotplot may be more suitable.

这听起来不像是直方图的工作 - 事件不是连续变量。条形图或点图可能更合适。

Some dummy data

一些虚拟数据

set.seed(123)
vec <- sample(1:9, 100, replace = TRUE)
vec <- factor(vec, labels = paste("My long event name", 1:9))

A barplot is produced via the barplot() function - we provide it the counts of each event using the table() function for convenience. Here we need to rotate labels using las = 2 and create some extra space of the labels in the margin

通过barplot()函数生成条形图 - 为方便起见,我们使用table()函数为每个事件提供计数。在这里,我们需要使用las = 2旋转标签,并在边距中创建一些额外的标签空间

## lots of extra space in the margin for side 1
op <- par(mar = c(10,4,4,2) + 0.1)
barplot(table(vec), las = 2)
par(op) ## reset

A dotplot is produced via function dotchart() and has the added convenience of sorting out the plot margins for us

点图是通过函数dotchart()生成的,并且为我们排序绘图边距提供了额外的便利

dotchart(table(vec))

The dotplot has the advantage over the barplot of using much less ink to display the same information and focuses on the differences in counts across groups rather than the magnitudes of the counts.

点图比使用更少墨水显示相同信息的条形图具有优势,并且侧重于组间计数的差异而不是计数的大小。

Note how I've set the data up as a factor. This allows us to store the event labels as the labels for the factor - thus automating the labelling of the axes in the plots. It also is a natural way of storing data like I understand you to have.

请注意我如何将数据设置为一个因素。这允许我们将事件标签存储为因子的标签 - 从而自动标记图中的轴。它也是一种自然的方式来存储我理解你拥有的数据。

#2


10  

Look at help(par), in particular fields mar (for the margin) and oma (for outer margin). It may be as simple as

看看帮助(标准杆),特别是mar(保证金)和oma(保证金)。它可能很简单

par(mar=c(5,3,1,1))   # extra large bottom margin
hist(vector, axes = FALSE, breaks = chartBreaks)
axis(1, at = tickMarks, labels = eventTypes, las = 2, tick = FALSE) 

#3


0  

Perhaps adding \n into your labels so they will wrap onto 2 lines? It's not optimal, but it may work.

也许在你的标签中添加\ n,这样它们就会包裹在2行上?它不是最佳的,但可能有效。

#4


0  

You might want to look at this post from Cross Validated

您可能希望从Cross Validated查看此帖子