ggplot2 aes_string()无法处理以数字开头或包含空格的名称

ggplot2 aes_string()无法处理以数字开头或包含空格的名称,r,ggplot2,R,Ggplot2,如果data.frame的列名以数字开头或有空格,aes\u string()无法处理它们: foo=data.frame("1st Col"=1:5, "2nd Col"=5:1, check.names=F) bar=colnames(foo) ggplot(foo, aes_string(x=bar[1],y=bar[2])) + geom_point() # Error in parse(text = x) : <text>:1:2: unexpected symbol #

如果
data.frame
的列名以数字开头或有空格,
aes\u string()
无法处理它们:

foo=data.frame("1st Col"=1:5, "2nd Col"=5:1, check.names=F)
bar=colnames(foo)
ggplot(foo, aes_string(x=bar[1],y=bar[2])) + geom_point()
# Error in parse(text = x) : <text>:1:2: unexpected symbol
# 1: 1st
#     ^

foo=data.frame("First Col"=1:5, "Second Col"=5:1, check.names=F)
bar=colnames(foo)
ggplot(foo, aes_string(x=bar[1],y=bar[2])) + geom_point()
# Error in parse(text = x) : <text>:1:7: unexpected symbol
# 1: First Col
#          ^

foo=data.frame("First_Col"=1:5, "Second_Col"=5:1, check.names=F)
bar=colnames(foo)
ggplot(foo, aes_string(x=bar[1],y=bar[2]))+geom_point()
# Now it works

据我所知,此方法应以编程方式工作:

foo=data.frame("1st Col"=1:5, "2nd Col"=5:1, check.names=F)

#Save the colnames
bar=colnames(foo)

#change the names to something usable
names(foo) <- c("col1", "col2")

#Plot with arbitrary labs
ggplot(foo, aes(x=col1, y=col2)) + geom_point()+
  labs(x=bar[1], y=bar[2])
foo=data.frame(“第一列”=1:5,“第二列”=5:1,check.names=F)
#保存colnames
bar=colnames(foo)
#将名称更改为可用的名称

名称(foo)最初的问题是如何修改变量的值,以便在事先不知道该值时ggplot()可以接受。 编写一个函数,将记号添加回变量值的开头和结尾:

# ggName -> changes a string so it is enclosed in back-ticks.
#   This can be used to make column names that have spaces (blanks)
#   or non-letter characters acceptable to ggplot2.
#   This version of the function is vectorized with sapply.
ggname <- function(x) {
    if (class(x) != "character") {
        return(x)
    }
    y <- sapply(x, function(s) {
        if (!grepl("^`", s)) {
            s <- paste("`", s, sep="", collapse="")
        }
        if (!grepl("`$", s)) {
            s <- paste(s, "`", sep="", collapse="")
        }
    }
    )
    y 
}
[1] 十二,

ggname("awk ward")
“`awk ward`”


l我也有类似的情况,我用``来让它明白:

mydf$ROIs <- row.names(mydf)
df.long <- melt(mydf)
colnames(df.long)[3] <- "Methods"
colnames(df.long)[4] <- "Mean L2 Norm Error"
variable.name="Variable", na.rm=TRUE)
ggplot(df.long, aes(ROIs, `Mean L2 Norm Error`, fill=Methods))+  
geom_bar(stat="identity",position="dodge")+
theme(axis.text.x=element_text(angle=45,hjust=1,vjust=1))

mydf$ROIs您可以使用下面的函数
aes\u string 2
代替
aes\u string

aes_string2 <- function(...){
  args <- lapply(list(...), function(x) sprintf("`%s`", x))
  do.call(aes_string, args)
}

aes\u string2尝试使用反勾号来括起字符串(我不确定它们是否会出现在注释中:“``”(实际上,仔细看,我不确定这是否有效)。感觉要跳过这个特殊的环将非常困难,我会使用“合法”列名并使用
labs()
或其他更改轴标签的方法等。@BenBolker谢谢,但正如我在问题中提到的,我并不认为我们已经知道列名是常量。你知道它们何时是变量吗?这是我问题的第二部分:我认为这很难,更惯用的方法是使用legal列名称并相应地调整指南标签。如果
ggplot
(通常更多的R)与
rms
包的
label()
框架一起工作,那就太好了……总是有两列吗?您必须使用
aes_字符串(x=“
1st Col
”,y=“
2nd Col
”)
-否则,
aes\u string
将无法复制(例如,
aes(x+1)
谢谢!出于我的目的,我使用了一个简化版本:
粘贴(“`”,x,`,`,”),sep=“”)
。值得一提的是,OP明确表示,他们对使用反勾保护编码时已知名称的解决方案不感兴趣:“请考虑我们可能不知道列名,所以请避免提供具有固定列名的示例……如下图……“我使用<代码>”“< /代码>”,代码没有错误,但没有绘制值,因此我必须在没有空间的情况下更改列名,然后绘制这些值。
l <- c("awk ward", "no way!")
ggname(l)
mydf$ROIs <- row.names(mydf)
df.long <- melt(mydf)
colnames(df.long)[3] <- "Methods"
colnames(df.long)[4] <- "Mean L2 Norm Error"
variable.name="Variable", na.rm=TRUE)
ggplot(df.long, aes(ROIs, `Mean L2 Norm Error`, fill=Methods))+  
geom_bar(stat="identity",position="dodge")+
theme(axis.text.x=element_text(angle=45,hjust=1,vjust=1))
aes_string2 <- function(...){
  args <- lapply(list(...), function(x) sprintf("`%s`", x))
  do.call(aes_string, args)
}