0
votes

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

2
That seems unlikely to be the actual code. There's an obvious syntax error with a missing closing single quote and comma at the beginning of the arguments to barplot: (data$'per hundred main ="Title",... perhaps should be barplot( data$'per hundred', main ="Title",.... I'm not voting to close as typo, since you have apparently got a plot occurring somewhere, but since there is no minimal reproducible example it might get closed eventually. Why no make a small example (perhaps with dput(head(data)) and remove the junk about reading data from your disk.IRTFM
It's the code, the only things changed is the actual information which I changed to make it more of a sample code. I corrected the problem with <(data$'per hundred'>. I'll try to make an example, this is my first time posting here. Thank you for letting me know of the issues.user3189
I added a picture for now since I'm still unfamiliar with using this website, but I'll try to adjust it so it's formatted correctly and shows the code as it should be for the minimal reproducible example. I'm sorry, I don't quite understand about the removing junk about reading data from my disk part.user3189

2 Answers

0
votes

Here's the barplot generated with all the labels.

Food Labeled Barplot

0
votes

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.