简体   繁体   中英

How do I get all my labels from x-axis shown on R for a barplot?

This is my first time using RStudio, and I've tried to find the the answer to my solution along with tinkering around with the code but I haven't been able to figure it out. I've even looked here and found other users with the same issue How to display all x labels in R barplot? and Rotating x axis labels in R for barplot but I wasn't able to get the answered solutions to work for my code. I've also asked two people more familiar with R, but after looking over my code and trying for themselves they were also unable to figure it out as well.

Everything would just result in error messages (I don't have the error messages showing in my console since when someone was trying to figure it out they cleared the global environment).

I have already generated 15 bars for my barplot, and the barplot itself is generated with the labels for the ylab, the title for my xlab, and the main, and I even have it colored, but I can't name each individual column bar.

The names for all the labels are listed in the source, but it's only showing every third bar or so. I need all bars labeled.

setwd("C:/Users/Person/Desktop/Rfolder")
library(readxl)
data <- read_excel("filename.xlsx")
View(topic)


barplot(data$'per hundred',
 main ="Title",
 xlab = "Words",
 ylab = "variable stats",
 col = c("gray"))

axis(1, at =c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15), 
     srt = 45,
   labels=c("Apple","Butter","Banana","Bacon","Candy","Carrot","Spam","Ube","Ice cream","Italian ice","Jackfruit","Kale","Tofu","Udon","All types"))

Coded Food Barplot

This one works, with working example added:

df <- cbind.data.frame("names" = c("Apple","Butter","Banana","Bacon","Candy","Carrot","Spam","Ube","Ice cream","Italian ice","Jackfruit","Kale","Tofu","Udon","All types"),  "numbers" = sample(20:100, 15))

barplot(height = df$numbers,
    names.arg = df$names,
    main = "Title",
    xlab = "Words",
    ylim = c(0, 100),
    ylab = "variable stats",
    col = c("gray"),
    las = 2)

To modify sizing of x axis names and labels, add options:

cex.names = 1 # controls magnification of x axis names. value starts at 1
cex.lab = 1 # control magnification of x & y axis labels. value starts at 1

to the barplot() function. Play around with sizing to find what works for you best. To escape the overlap of x axis label and x axis names, instead of xlab = "Words" use sub = "Words" . Simple & efficient.

Here's the barplot generated with all the labels.

Food Labeled Barplot

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM