【发布时间】:2018-02-06 20:24:13
【问题描述】:
我有一个函数可以从数据中生成散点图,其中提供了一个参数来选择用于为点着色的列。这是一个简化的版本:
library(ggplot2)
plot_gene <- function (df, gene) {
ggplot(df, aes(x, y)) +
geom_point(aes_string(col = gene)) +
scale_color_gradient()
}
其中df 是一个data.frame,其中包含x、y 列,然后是一堆基因名称。这适用于大多数基因名称;但是,有些有破折号,这些破折号:
print(plot_gene(df, "Gapdh")) # great!
print(plot_gene(df, "H2-Aa")) # Error: object "H2" not found
似乎gene 变量正在被解析("H2-Aa" 变为H2 - Aa)。我怎样才能解决这个问题?有没有办法在aes_string中指明字符串不应该经过eval?
可重复输入
如果你需要一些输入来玩,这会像我的数据一样失败:
df <- data.frame(c(1,2), c(2,1), c(1,2), c(2,1))
colnames(df) <- c("x", "y", "Gapdh", "H2-Aa")
对于我的真实数据,我使用read.table(..., header=TRUE) 并获取带有破折号的列名,因为原始数据文件有它们。
【问题讨论】:
标签: r dataframe ggplot2 aesthetics