将ggplot2与名称中带有空格的列一起使用 [英] Using ggplot2 with columns that have spaces in their names

查看:243
本文介绍了将ggplot2与名称中带有空格的列一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下数据框结构

df <- as.data.frame(A)
colnames(df)<- c("Sum of MAE", "Company")
df <- na.omit(df)
df2 <- df[order(df[,1]),]
df2 <- head(df2, n=10)
ggplot(df2, aes_string("Sum of MAE", "Company", group=1) + geom_line())
print(df2)

这是数据的结构

 Sum of MAE Company
606   0.030156758080105    COCO
182  0.0600065426668421    APWC
836  0.0602272459239397     EDS
1043 0.0704327240953608    FREE
2722               0.09   VLYWW
1334 0.0900000000000001    IKAN
2420  0.104746328560384     SPU
860   0.106063964745531    ELON
2838  0.108373386847075    WTSL
1721  0.110086738825851    MTSL

ggplot似乎不起作用.经过一连串的错误后,我现在遇到的错误是

The ggplot doesnt seem to be working. After a litany of errors the current one I'm getting is

Error in parse(text = x) : <text>:1:5: unexpected symbol
1: Sum of

有人可以帮助我使ggplot 2正常工作吗?

Can someone help me getting the ggplot 2 working.

推荐答案

哎呀,这就是为什么您应该始终确保拥有有效的列名的原因.首先,这是您的数据集易于复制的版本

Ugh, this is why you should always make sure you have valid column names. First, here's an easier-to-reproduce version of your dataset

df2 <- data.frame(`Sum of MAE` = c(0.030156758080105, 0.0600065426668421, 
   0.0602272459239397, 0.0704327240953608, 0.09, 0.0900000000000001, 
   0.104746328560384, 0.106063964745531, 0.108373386847075, 0.110086738825851
   ), Company = c("COCO", "APWC", "EDS", "FREE", "VLYWW", "IKAN", "SPU", "ELON", 
   "WTSL", "MTSL"), check.names=F)

ggplot(df2, aes_string("Sum of MAE", "Company", group=1) + geom_line())
# Error in parse(text = x) : <text>:1:5: unexpected symbol
# 1: Sum of
#         ^

问题是aes_string()使用parse()将您的文本表达式转换为可以在data.frame中解析的适当的R符号.当您解析"MAE之和"时,这不是有效的R语法-也就是说,它不会解析为单个漂亮的符号名称.如果使用这样的坏"名称,则可以使用反引号将它们转义,以将表达式(空格和全部)视为符号.所以你可以做

The problem is that aes_string() uses parse() to turn your text expression into a proper R symbol that can be resolved within the data.frame. When you parse "Sum of MAE" that's not valid R syntax -- that is, it doesn't resolve to a single nice symbol name. If you use "bad" names like that, you can escape them with the back-tick to treat the expression (spaces and all) as a symbol. So you can do

ggplot(df2, aes_string("`Sum of MAE`", "Company", group=1)) + geom_line()
# or
ggplot(df2, aes(`Sum of MAE`, Company, group=1)) + geom_line()

但实际上,最好坚持为data.frame使用有效的列名,而不是使用colnames()绕过检查.

but really it would be better to stick to using valid column names for your data.frame rather than bypassing the checks with colnames().

如果要更改列名称以获取更细的"轴标签,则可能应该使用xlab()代替.例如

If you were changing the column names to get "nicer" axis labels, you should probably do what with xlab() instead. For example

df3 <- data.frame(df2)
names(df3)
# [1] "Sum.of.MAE" "Company" 
ggplot(df3, aes(Sum.of.MAE, Company, group=1)) + 
    geom_line() + 
    xlab("Sum of MAE values")

这篇关于将ggplot2与名称中带有空格的列一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆