0

I am a R newbie. I am currently using Rstudio and trying to develop a program that graphs whatever the user gives it (assuming it is a csv file). My problem is, I do not know how to reference the columns in the data that the user is giving. Here is a portion of my code:

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

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

For the qplot function (or ggplot2), I want to give an x and y value (columns in the csv). Normally you would just use the fileName$ColumnName, but in this case, I don't know what's in the data the user is uploading (so I don't know the column names).

I've tried doing this, without success:

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"))

Any ideas guys?

EDIT: I'd also like to be able to display the column name on the graph. Is there a way to do this?

4

1 に答える 1

3

この問題を解決するための鍵は、aes_string代わりに使用することです(暗黙的に使用しているため、aes知っているかどうかはわかりません)。文字列で直接動作するため、これは動作するはずです:aesqplotaes_string

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