【问题标题】:Basic Plot - How to use text labels on X axis?基本图 - 如何在 X 轴上使用文本标签?
【发布时间】:2021-12-08 06:01:00
【问题描述】:

您可以在绘图上使用文本作为 X 轴标签吗?我已经搜索过,但看不到任何示例。我是否正在尝试做 R 语言中不可能做的事情?即使我尝试绘制一个变量。 Countries 是文本/字符 - 但我不知道如何设置它

plot(Finally$Countries,Finally$RobberyPerCent, pch = 16, col = 2)

我得到了错误

Error in plot.window(...) : need finite 'xlim' values
In addition: There were 24 warnings (use warnings() to see them)

谢谢,我的目标是结合两个变量,看看有没有基本的模式。我已经能够计算出简单的线性回归(无相关性),但我在基本绘图方面失败了

#Subset for Percentages
Q5DataFinal <- subset(Q5Data, select = c(RobberyPerCent, UnlawfulPerCent))
View(Q5DataFinal)
library(data.table)
Nearlythere <- setDT(Q5DataFinal, keep.rownames = TRUE)[] # turn rownames into column data
names(Nearlythere)[names(Nearlythere) == 'rn'] <- 'Countries'     #renaming rn to countries
Nearlythere$Countries[] <- lapply(Nearlythere$Countries, as.character) #Changing Countries to Character
Finally <- Nearlythere
summary(Finally) #Countries saved as characters


# Attempt to create two Y axis Graph with Countries as X ticks
par(mar = c(5, 4, 4, 4) + 0.3)              # Additional space for second y-axis
plot(Finally$Countries,Finally$RobberyPerCent, pch = 16, col = 2)              # Create first plot
par(new = TRUE)                             # Add new plot
plot(Finally$Countries, Finally$UnlawfulPerCent, pch = 17, col = 3,              # Create second plot without axes
     axes = FALSE, xlab = "", ylab = "")
axis(side = 4, at = pretty(range(Finally$UnlawfulPerCent)))      # Add second axis
mtext("UnlawfulPerCent", side = 4, line = 3)             # Add second axis label

输入是

structure(list(Countries = list("Albania", "Austria", "Bulgaria", 
    "Croatia", "Cyprus", "Czechia", "Finland", "Germany (until 1990 former territory of the FRG)", 
    "Greece", "Ireland", "Italy", "Kosovo (under United Nations Security Council Resolution 1244/99)", 
    "Latvia", "Lithuania", "Luxembourg", "Malta", "Montenegro", 
    "Romania", "Serbia", "Slovenia", "Spain", "Switzerland"), 
    RobberyPerCent = c(5, 6, 18, 7, 5, 23, 5, 9, 24, 9, 40, 12, 
    17, 18, 10, 52, 24, 33, 10, 17, 80, 2), UnlawfulPerCent = c(95, 
    94, 82, 93, 95, 77, 95, 91, 76, 91, 60, 88, 83, 82, 90, 48, 
    76, 67, 90, 83, 20, 98)), row.names = c(NA, -22L), class = c("data.table", 
"data.frame"), .internal.selfref = <pointer: 0x0000020282d01ef0>)

【问题讨论】:

  • 试试plot(Basta$RobberyPerCent ~ factor(Basta$Countries))?
  • 谢谢@xilliam,它给出了错误Error in order(y) : unimplemented type 'list' in 'orderVector1' 我将国家列转换为字符
  • 最好通过将dput(Basta) 的控制台输出复制粘贴到您的问题中来发布您的数据对象。
  • @xilliam 抱歉 - 我发送的原始代码存在问题 - Basta 现在已替换为 finally

标签: r plot axis axis-labels


【解决方案1】:

你想要这样的东西吗?

 par(mar = c(5, 5, 4, 2))
 x <- seq(0, 5, length.out = 500)
 plot(x, sin(x^2), xaxt = "n", xlab = expression("Here is X"), ylab = expression(sin(x^2)),
      main = expression("My coolest plot" - sin(x^2)))
 axis(1, at=0:5, labels=c("Albania", "Kosovo", "Kongo", "Germany", "Bulgaria", "Spain"))

补充


#your dataset
countries <- list("Albania", "Austria", "Bulgaria", 
                  "Croatia", "Cyprus", "Czechia", "Finland", "Germany (until 1990 former territory of the FRG)", 
                  "Greece", "Ireland", "Italy", "Kosovo (under United Nations Security Council Resolution 1244/99)", 
                  "Latvia", "Lithuania", "Luxembourg", "Malta", "Montenegro", 
                  "Romania", "Serbia", "Slovenia", "Spain", "Switzerland")

#modify to
axis(1, at=0:21, labels=countries, cex.axis=0.5) #select cex.axis for better displaying 

【讨论】:

  • 在 X 轴上 - 我希望从 Countries 列中添加国家名称,而不是 0 到 5,因此 0 是阿尔巴尼亚,22 是西班牙。谢谢
  • @iKcon 看,我添加了您需要的内容;)您可以针对您的情况进行更正。
  • 谢谢@manro 我正在努力尝试调整您的解决方案 - 我想使用列 Countries 而不是输入 c("Albania,...etc) - 我在那里失败了。下一步是让你的公式适应我的数据框
  • @iKcon 看,我更正了。我认为你应该切换到GermanyKosovo。最好在剧情下发表评论。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-15
  • 1970-01-01
  • 1970-01-01
  • 2015-04-01
相关资源
最近更新 更多