为什么R中的plot()在按列的值对数据帧进行排序后不反映数据?

为什么R中的plot()在按列的值对数据帧进行排序后不反映数据?,r,plot,R,Plot,这是完整的代码: require(RCurl) require(foreign) require(tidyverse) x = getURL("https://raw.githubusercontent.com/RInterested/PLOTS/master/drinks_csv.csv") data <- read.csv(textConnection(x)) data <- data[,c(1:5,8)] interest <- c(1,2,3,7,9,10,11

这是完整的代码:

require(RCurl)
require(foreign)
require(tidyverse) 
x = getURL("https://raw.githubusercontent.com/RInterested/PLOTS/master/drinks_csv.csv")

data <- read.csv(textConnection(x))
data <- data[,c(1:5,8)]


interest <- c(1,2,3,7,9,10,11,14,16,17,21,24,26,29,30,31,34,35,36,38,
              39,40,42,44,47,48,49,50,55,57,61,62,63,65,69,70,71,72,
              73,74,75,76,77,78,79,80,81,82,83,84,85,89,91,92,93,94,
              100,102,103,104,105,106,108,110,111,112,113,114,115,116,
              118,119,121,122,125,126,127,134,141,142,143,144,152,154,
              155,158,159,160,162,169)

data <- data[interest,]
data <- droplevels(data)
data <- data[with(data, order(data$wine_servings)),]
row.names(data) <- 1:nrow(data)


plot(data$country,data$wine_servings,las=2, xlab="", ylab="", xaxt = 'n', yaxt = 'n')
axis(1, at = 1:length(data$country), labels = data$country, cex.axis = 0.6,las = 2)
require(RCurl)
要求(外国)
要求(整洁的人)
x=getURL(“https://raw.githubusercontent.com/RInterested/PLOTS/master/drinks_csv.csv")

以上代码中的数据:

head(data$country)
[1] afghanistan bangladesh  india       indonesia   iran        iraq       
90 Levels: afghanistan albania algeria argentina australia ... zimbabwe
这是一个因子,打印时,它将转换为数字,并沿其他标高拖动,例如:

plot(head(data$country),rep(1,6))

您的数据帧正常,我们只需执行以下操作:

plot(1:nrow(data),data$wine_servings,las=2, xlab="", ylab="", xaxt = 'n', yaxt = 'n')
axis(1, at = 1:length(data$country), labels = as.character(data$country), cex.axis = 0.6,las = 2)


长和短,当列是一个因子时要小心,因为基R plot函数很容易转换它们。

在上面的代码中:

head(data$country)
[1] afghanistan bangladesh  india       indonesia   iran        iraq       
90 Levels: afghanistan albania algeria argentina australia ... zimbabwe
这是一个因子,打印时,它将转换为数字,并沿其他标高拖动,例如:

plot(head(data$country),rep(1,6))

您的数据帧正常,我们只需执行以下操作:

plot(1:nrow(data),data$wine_servings,las=2, xlab="", ylab="", xaxt = 'n', yaxt = 'n')
axis(1, at = 1:length(data$country), labels = as.character(data$country), cex.axis = 0.6,las = 2)


长和短,当列是因子时要小心,因为基本R绘图函数很容易转换它们。

您必须对因子级别进行排序,而不是对数据进行排序。试着快速搜索一下如何做到这一点,我认为还有很多。你必须对因子水平进行排序,而不是数据。尝试快速搜索如何做到这一点,我认为有很多。谢谢你。。。我明天会接受-给你的答案一个曝光的机会。。。您描述的这个“工件”只影响plot(),对吗?例如,没有必要调整cor(data$this,data$that)之类的东西,对吗?不客气。因此,对于关联或建模,快速跟进不是问题。如果您忘记了向量是一个因子,而使用它来子集矩阵,或者基本上是可以轻松转换它们的函数,那么这可能是一个问题。它们通过因子中的级别转换为数值。默认情况下,当data.frame中有字符时,这些字符将转换为因子,并且它们的级别按字母顺序排序。例如,as.numeric(因子(因子(因子)(c)(“a”,“c”,“b”))给出u 1,3,2,但如果指定级别,as.numeric(因子(因子)(因子(c)(“a”,“c”,“b”),级别=c(“c”,“b”,“a”))则会给出其他一些绘图内容?绘图(如.numeric(head(data$country)),rep(1,6)),但您不会得到标签谢谢。。。我明天会接受-给你的答案一个曝光的机会。。。您描述的这个“工件”只影响plot(),对吗?例如,没有必要调整cor(data$this,data$that)之类的东西,对吗?不客气。因此,对于关联或建模,快速跟进不是问题。如果您忘记了向量是一个因子,而使用它来子集矩阵,或者基本上是可以轻松转换它们的函数,那么这可能是一个问题。它们通过因子中的级别转换为数值。默认情况下,当data.frame中有字符时,这些字符将转换为因子,并且它们的级别按字母顺序排序。例如,as.numeric(因子(因子(因子)(c)(“a”,“c”,“b”))给出u 1,3,2,但如果指定级别,as.numeric(因子(因子)(因子(c)(“a”,“c”,“b”),级别=c(“c”,“b”,“a”))则会给出其他一些绘图内容?绘图(如.numeric(head(data$country)),rep(1,6)),但您将无法获得标签