How to fix missing labels in base R barplot

只谈情不闲聊 提交于 2021-02-17 03:39:35

问题


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

回答1:


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




回答2:


Here are four other ways to deal with the labels not showing:

  1. Make the label text smaller:

    barplot(counts, main="Placements", xlab="number of staffs", cex.names=.7)
    
  2. Change the size of the plot window:

    dev.new(width=10, height=6)
    barplot(counts, main="Placements", xlab="number of staffs")
    
  3. Use a horizontal bar plot by increasing the left margin:

    oldp <- par(mar=c(5.1, 8.1, 2.1, 1.1))
    barplot(counts, main="Placements", xlab="number of staffs", horiz=TRUE, las=1)
    par(oldp)
    
  4. Increase the bottom margin and plot the text at an angle:

    out <- barplot(counts, main="Placements", names.arg="")
    title(xlab="number of staffs", line=4)
    text(out, rep(-.1, 6), names(counts), srt=45, pos=2, xpd=NA, cex=.75)
    


来源:https://stackoverflow.com/questions/58759916/how-to-fix-missing-labels-in-base-r-barplot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!