【发布时间】: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