Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot变量/列名包含空格时出错_R_Ggplot2 - Fatal编程技术网

R ggplot变量/列名包含空格时出错

R ggplot变量/列名包含空格时出错,r,ggplot2,R,Ggplot2,我看了一张桌子。把列名放进去。然后我在GGplot的帮助下绘制图形。当ggplot中的aes参数中我使用名称时出错,如代码中所示,名称之间带有空格,如“shucked weight”,但如果我使用单字母参数如“wweight”,则代码运行并绘制图形。当我将列名“shucked weight”替换为“sweight”时,我得到的图形没有错误 我注意到这是一个非常不寻常的错误,我不知道为什么 library(data.table) a<-fread("https://archive.ics.u

我看了一张桌子。把列名放进去。然后我在
GGplot
的帮助下绘制图形。当ggplot中的
aes
参数中我使用名称时出错,如代码中所示,名称之间带有空格,如“shucked weight”,但如果我使用单字母参数如“wweight”,则代码运行并绘制图形。当我将列名“shucked weight”替换为“sweight”时,我得到的图形没有错误

我注意到这是一个非常不寻常的错误,我不知道为什么

library(data.table)
a<-fread("https://archive.ics.uci.edu/ml/machine-learning-databases/abalone/abalone.data",sep=",")
dim(a)
names(a)<-c("sex","length","diameter","height","wweight","shucked weight","viscera Weight","shell weight","rings")
g<-ggplot(data = a,aes(wweight,rings))
g+geom_point()+geom_smooth(method="lm")
g<-ggplot(data = a,aes(shucked weight,rings))
g+geom_point()+geom_smooth(method="lm")
库(data.table)
a在评论中回答:

如果变量名包含空格或其他特殊字符,则需要在其周围加上反勾:

g <-ggplot(data = a, aes(`shucked weight`, rings)) + 
    geom_point() + 
    geom_smooth(method = "lm")

g如果使用带有空格的名称,则需要用倒钩(`)将其环绕,以便R知道这是一个符号。最好不要使用带有空格的变量名。请参见
?引号
g <-ggplot(data = a, aes(`shucked weight`, rings)) + 
    geom_point() + 
    geom_smooth(method = "lm")