如何从条形图的 R 上显示 x-axis 获取我的所有标签?
How do I get all my labels from x-axis shown on R for a barplot?
这是我第一次使用 RStudio,我试图找到我的解决方案的答案以及修改代码,但我一直无法弄清楚。我什至在这里看过,发现其他用户也有同样的问题 How to display all x labels in R barplot? and Rotating x axis labels in R for barplot 但我无法获得适用于我的代码的解决方案。我也问过两个更熟悉 R 的人,但在查看我的代码并自己尝试后,他们也无法弄清楚。
一切都会导致错误消息(我的控制台中没有显示错误消息,因为当有人试图弄清楚他们清除了全局环境时)。
我已经为我的条形图生成了 15 个条形图,条形图本身是用 ylab 的标签、我的 xlab 的标题和主图生成的,我什至还给它上色了,但我做不到为每个单独的栏命名。
所有标签的名称都列在源代码中,但它只显示大约每三个栏。我需要标记所有条。
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
这个有效,添加了工作示例:
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)
要修改 x 轴名称和标签的大小,请添加选项:
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
到 barplot() 函数。试一试尺码,找到最适合您的尺码。要避免 x 轴标签和 x 轴名称的重叠,请使用 sub = "Words"
而不是 xlab = "Words"
。简单高效。
这是使用所有标签生成的条形图。
Food Labeled Barplot
这是我第一次使用 RStudio,我试图找到我的解决方案的答案以及修改代码,但我一直无法弄清楚。我什至在这里看过,发现其他用户也有同样的问题 How to display all x labels in R barplot? and Rotating x axis labels in R for barplot 但我无法获得适用于我的代码的解决方案。我也问过两个更熟悉 R 的人,但在查看我的代码并自己尝试后,他们也无法弄清楚。
一切都会导致错误消息(我的控制台中没有显示错误消息,因为当有人试图弄清楚他们清除了全局环境时)。
我已经为我的条形图生成了 15 个条形图,条形图本身是用 ylab 的标签、我的 xlab 的标题和主图生成的,我什至还给它上色了,但我做不到为每个单独的栏命名。
所有标签的名称都列在源代码中,但它只显示大约每三个栏。我需要标记所有条。
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
这个有效,添加了工作示例:
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)
要修改 x 轴名称和标签的大小,请添加选项:
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
到 barplot() 函数。试一试尺码,找到最适合您的尺码。要避免 x 轴标签和 x 轴名称的重叠,请使用 sub = "Words"
而不是 xlab = "Words"
。简单高效。
这是使用所有标签生成的条形图。
Food Labeled Barplot