R:来自multiline ggplot2命令的"一元运算符错误"

soo*_*sus 35 r multiline ggplot2 reshape2

我正在使用ggplot2对两种不同物种进行箱线图比较,如下面的第三列所示:

> library(reshape2)
> library(ggplot2)
> melt.data = melt(actb.raw.data)

> head(actb.raw.data)
  region  expression species
1     CG -0.17686667   human
2     CG -0.06506667   human
3     DG  1.04590000   human
4    CA1  1.94093333   human
5    CA2  1.55023333   human
6    CA3  1.75800000   human

> head(melt.data)
  region species   variable       value
1     CG   human expression -0.17686667
2     CG   human expression -0.06506667
3     DG   human expression  1.04590000
4    CA1   human expression  1.94093333
5    CA2   human expression  1.55023333
6    CA3   human expression  1.75800000
Run Code Online (Sandbox Code Playgroud)

但是,当我运行以下代码时:

ggplot(combined.data, aes(x = region, y = expression, fill = species)) +
+     geom_boxplot() +
+     scale_fill_manual(values = c("yellow", "orange"))
+     ggtitle("Expression comparisons for ACTB")
+     theme(axis.text.x = element_text(angle=90, face="bold", colour="black"))
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

> ggplot(actb.raw.data, aes(x = region, y = expression, fill = species)) +
+     + geom_boxplot() +
+     + scale_fill_manual(values = c("yellow", "orange"))
Error in +geom_boxplot() : invalid argument to unary operator
> + ggtitle("ACTB expression in human vs. macaque")
Error in +ggtitle("ACTB expression in human vs. macaque") : 
 invalid argument to unary operator
> + theme(axis.text.x = element_text(angle=90, face="bold", colour="black"))
Error in inherits(x, "theme") : argument "e2" is missing, with no default
Run Code Online (Sandbox Code Playgroud)

当我使用变量melt.data运行时,也会发生这种情况.有人可以帮我解决这个问题吗?我之前使用格式相同的不同数据集成功运行此代码,我无法弄清楚这里出了什么问题.

Dav*_*idt 70

看起来你可能已经+在每一行的开头插入了一个额外的,R正在解释为一元运算符(如-解释为否定,而不是减法).我认为有用的是

ggplot(combined.data, aes(x = region, y = expression, fill = species)) +
    geom_boxplot() +
    scale_fill_manual(values = c("yellow", "orange")) + 
    ggtitle("Expression comparisons for ACTB") + 
    theme(axis.text.x = element_text(angle=90, face="bold", colour="black"))
Run Code Online (Sandbox Code Playgroud)

也许您从R控制台的输出中复制并粘贴?+当输入不完整时,控制台在行的开头使用.


smc*_*mci 20

在R中发布多行命令时,这是一个众所周知的麻烦.(当您source()使用脚本复制并粘贴行时,您可以获得不同的行为,包括多行和注释)

规则:总是将悬空的"+"放在一行的末尾,这样R知道命令未完成:

ggplot(...) + geom_whatever1(...) +
  geom_whatever2(...) +
  stat_whatever3(...) +
  geom_title(...) + scale_y_log10(...)
Run Code Online (Sandbox Code Playgroud)

不要把悬挂的"+"放在行的开头,因为这会使这个发痒 Error in "+ geom_whatever2(...) invalid argument to unary operator"

并且显然不要在两端放置悬空"+"并开始,因为这是语法错误.所以,要养成一致的习惯:总是把'+'放在行尾.

比照 回答"在R脚本中拆分多行代码"