基本图 - 如何在 X 轴上使用文本标签?

Basic Plot - How to use text labels on X axis?

你能在绘图上使用文本作为 X 轴标签吗?我已经搜索过,但看不到任何示例。我想做一些在 R 中做不到的事情吗?即使我试图绘制一个变量。 Countries 是 text/character - 但我不知道如何设置它

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

你想要这样的东西吗?

 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