0

我是R新手。我目前正在使用 Rstudio 并尝试开发一个程序来绘制用户提供的任何内容(假设它是一个 csv 文件)。我的问题是,我不知道如何引用用户提供的数据中的列。这是我的代码的一部分:

library(shiny)
library(datasets)
library(ggplot2)

X <- read.csv(file.choose())
print(qplot(data=X, **x=?????, y=?????**, main="la"))

对于 qplot 函数(或 ggplot2),我想给出一个 x 和 y 值(csv 中的列)。通常你只会使用fileName$ColumnName,但在这种情况下,我不知道用户上传的数据中有什么(所以我不知道列名)。

我试过这样做,但没有成功:

library(shiny)
library(datasets)
library(ggplot2)

X <- read.csv(file.choose())
headers <- names(X)
print(qplot(data=X, **x=X$headers[1], y=X$headers[2]**, main="la"))

有什么想法吗?

编辑:我还希望能够在图表上显示列名。有没有办法做到这一点?

4

1 回答 1

3

解决这个问题的关键是使用aes_string而不是aes(我不确定你是否知道aes,因为你qplot使用的是隐式使用它)。aes_string直接使用字符串,所以这应该工作:

ggplot(data=X)  +
  geom_point(aes_string(x=headers[1], y=headers[2]))
于 2013-07-25T00:47:04.640 回答