0

How to do ggplot data.frame columns by column index? This is needed when column names are the same or when you have 10000 columns. For example, how to plot histogram of 2nd column below?

dt <- data.table(cars); names(dt) <- c("A", "A") 
ggplot(dt) + geom_histogram(aes(A))
IVIM
  • 2,167
  • 1
  • 15
  • 41
  • 1
    Does this answer your question? [R: ggplot specifying column with its index rather than name](https://stackoverflow.com/questions/16187091/r-ggplot-specifying-column-with-its-index-rather-than-name) – broti Mar 26 '20 at 14:49
  • @broti that solution has been deprecated. – Noah Mar 26 '20 at 14:55

2 Answers2

1

First off, you should never have datasets (datatables, and so on...) with identical column names. dataframe structures prevent you from doing this for good reason.

Now, to specify by column name or index, you can use the .data notation. e.g

library(ggplot2)
library(data.table)
dt <- data.table(cars)
ggplot(dt) + geom_histogram(aes(.data[[names(dt)[1]]]))
RoB
  • 1,833
  • 11
  • 23
0

Thanks for useful comments. There's very simple solution for this -
Now I can read any third party spreadsheet and quickly visualize all its columns without worrying about column names:

 for (i in 1:ncols(dt)) 
    ggplot(dt) + geom_histogram(aes_string(names(dt)[i]))
IVIM
  • 2,167
  • 1
  • 15
  • 41