如何修复基本R barplot中丢失的标签 [英] How to fix missing labels in base R barplot

查看:79
本文介绍了如何修复基本R barplot中丢失的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图绘制一个工作人员数量的小图,但是x轴上的某些标签丢失了.如何解决这个问题

> dput(dat)
structure(list(Name = c("John", "Jacky", "Jill", "Sam", "Arthur", 
"Aaron", "Jacob", "Joseph", "Martin", "Alan", "Albert", "Clare", 
"Frederick", "Florence", "David", "George", "Michael", "Doughlas", 
"Andrew", "Brian"), Clinc = c("DMMTC", "DMMTC", "DMMTC", "DMMTC", 
"CKDMTC", "CKDMTC", "CKDMTC", "CKDMTC", "Warfarin MTC", "Warfarin MTC", 
"Warfarin MTC", "Warfarin MTC", "Respiratory MTC", "Respiratory MTC", 
"QSC", "QSC", "QSC", "Pain MTC", "Pain MTC", "Pain MTC")), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))

summary(dat)

head(dat)


counts <- table(dat$Clinc)
barplot(counts, main="Placements",
        xlab="number of staffs",)

解决方案

Anandapadmanathan,这对绘图窗口的实际大小很敏感.如果将其扩展为更大的尺寸,则应该看到所有标签.由于重叠的文本框,有些可能被隐藏.例如,这是您的代码,但绘图窗口更大.

不幸的是,虽然我不知道为什么一个或多个名称会从您的图中丢失",但您仍然可以随意添加标签.

首先,您是否知道barplot返回每个小节中心的X值?奇怪的是,它们不是整数:

 bp <- barplot(counts, main="Placements", xlab="number of staffs")
bp
#      [,1]
# [1,]  0.7
# [2,]  1.9
# [3,]  3.1
# [4,]  4.3
# [5,]  5.5
# [6,]  6.7
 

我们可以使用它们来手动放置一些文本.

从过于狭窄的情节开始:

 # left, unchanged
bp <- barplot(counts, main="Placements", xlab="number of staffs")
 

我们可以手动添加一些文本.为此,知道xpd=NA允许我们将文本(和点)放置在常规绘图窗口之外.另外,adj=是"x对齐"和"y对齐"的向量,其中0.5居中,在这种情况下,-4是突降大约4个高度".您需要玩这个游戏才能达到您想要的效果. (有关详情,请参见 ?par 帮助xpd ?text 获取有关adj的帮助.)

 # middle, just for testing
bp <- barplot(counts, main="Placements", xlab="number of staffs")
text(bp, 0, names(counts), xpd = NA, adj = c(0.5, 4), col = "red")
 

最后,由于我们现在可以看到省略了第2和第5个标签,因此我们可以定位这些标签.

 # right, fixed
bp <- barplot(counts, main="Placements", xlab="number of staffs")
text(bp[c(2,5)], 0, names(counts)[c(2,5)], xpd = NA, adj = c(0.5, 4), col = "red")
 

这是完美的吗?不,美丽吗?也许不是.但是,这使您可以重新获得丢失的东西. (我个人认为,交替使用x标签的高度可能是一件好事,尤其是当其中一些标签的宽度超出所需宽度时.)

Hi i have trying to plot a barplot of number of staffs, however some of the labels on x axis is missing. How to fix this

> dput(dat)
structure(list(Name = c("John", "Jacky", "Jill", "Sam", "Arthur", 
"Aaron", "Jacob", "Joseph", "Martin", "Alan", "Albert", "Clare", 
"Frederick", "Florence", "David", "George", "Michael", "Doughlas", 
"Andrew", "Brian"), Clinc = c("DMMTC", "DMMTC", "DMMTC", "DMMTC", 
"CKDMTC", "CKDMTC", "CKDMTC", "CKDMTC", "Warfarin MTC", "Warfarin MTC", 
"Warfarin MTC", "Warfarin MTC", "Respiratory MTC", "Respiratory MTC", 
"QSC", "QSC", "QSC", "Pain MTC", "Pain MTC", "Pain MTC")), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))

summary(dat)

head(dat)


counts <- table(dat$Clinc)
barplot(counts, main="Placements",
        xlab="number of staffs",)

Anandapadmanathan, this is sensitive to the actual size of the plot window. If you expand it to a larger size, you should see all labels. Some may be hidden due to over-lapping text boxes. For instance, this is your code but with a much wider plot window.

Unfortunately, while I don't have an easy fix for why one or more names are "missing" from your plot, you have the ability to add labels arbitrarily.

First, did you know that barplot returns the X values for the center of each bar? Oddly enough, they are not integers:

bp <- barplot(counts, main="Placements", xlab="number of staffs")
bp
#      [,1]
# [1,]  0.7
# [2,]  1.9
# [3,]  3.1
# [4,]  4.3
# [5,]  5.5
# [6,]  6.7

We can use these to place some text manually.

Starting with a too-narrow plot:

# left, unchanged
bp <- barplot(counts, main="Placements", xlab="number of staffs")

We can add some text manually. For this, know that xpd=NA allows us to put text (and points) outside of the normal plotting window. Also, adj= is a vector of "x alignment" and "y alignment", where 0.5 is centered, and in this case, -4 is "bump down approximately 4 heights". You'll need to play with this to get it to what you want. (See ?par for help on xpd, and ?text for help on adj.)

# middle, just for testing
bp <- barplot(counts, main="Placements", xlab="number of staffs")
text(bp, 0, names(counts), xpd = NA, adj = c(0.5, 4), col = "red")

Lastly, since we can now see that it's the 2nd and 5th label being omitted, we can just target those.

# right, fixed
bp <- barplot(counts, main="Placements", xlab="number of staffs")
text(bp[c(2,5)], 0, names(counts)[c(2,5)], xpd = NA, adj = c(0.5, 4), col = "red")

Is it perfect? No. Is it beautiful? Perhaps not. But this allows you to regain something of what you've lost. (I personally feel that alternating the height of x-labels can be a good thing, especially when some of them are wider than you want.)

这篇关于如何修复基本R barplot中丢失的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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