-1

Hi there I'm trying to plot a defined number of graphs using gridExtra. This is working but unfortunately it is not dealing with special characters in its name. I tried to work around by using R friendly names and add in the actual name as a subtitle

library(gridExtra)
library(ggplot2)

Dataframe<-read.csv2(File_with_R_friendly_names.csv)

names<-read.csv2(File_with_actual_names.csv)
bar<-colnames(names)

list_of_plots<-lapply(names(Dataframe)[2:10], function(i) {
  ggplot(Dataframe, aes_string(x="X1", y=i)) + geom_point()+labs(x=i,       y="Intensity", subtitle=bar[i])
})
do.call(grid.arrange, c(list_of_plots, ncol=3))

If I put in bar[2] all graphs get the actual name but it is the same one for all while if I set bar to i, all graphs get NA.

The names I use to suit R are Met1, Met2, Met3, Met4, Met5, Met6, Met7, Met8, Met9 and Met10 Examples of names that I need on the plots are:

-(-)-Corey lactone
-(2R)-2,3-Dihydroxypropanoic acid
-(D-(+)-Glyceric acid?)
-1,5-Naphthalenediamine
-12-Aminododecanoic acid
-2,5-di-tert-Butylhydroquinone
-2,6-di-tert-Butylphenol
-2-Amino-N,N-diethylacetamide
-2-Ethyl-2-phenylmalonamide
-2-Naphthalenesulfonic acid

Here is the dput to reproduce the bar (names):

`bar<-c("X1", "(-)-Corey lactone", "(2R)-2,3-Dihydroxypropanoic acid (D-(+)-  Glyceric acid?)", "1,5-Naphthalenediamine", "12-Aminododecanoic acid", "2,5-di-  tert-Butylhydroquinone", "2,6-di-tert-Butylphenol", "2-Amino-N,N- diethylacetamide", "2-Ethyl-2-phenylmalonamide", "2-Naphthalenesulfonic acid")`

Here is the dput to reproduce the dataframe:

Dataframe<-structure(list(X1 = c(0, 0, 0.25, 0.25, 0.5, 0.5, 1, 1, 2, 2), 
Met1 = c(0, 0, 38096319.85, 45978353.93, 35077691.7, 42146132.41, 
62606961.17, 32786049.6, 51054004.82, 48898547.32), Met2 = c(0, 
0, 1288905.771, 948466.4001, 645979.6463, 1228663.251, 1137957.136, 
940928.9344, 1443680.706, 1755726.385), Met3 = c(0, 0, 575887.464, 
693692.0349, 1362477.6, 1515767.293, 2241120.502, 2417932.908, 
3866432.112, 3894701.876), Met4 = c(0, 0, 16737068.73, 21915551.3, 
12088089.1, 16003037.3, 17720785.29, 11957614.24, 13127281.5, 
14192542.13), Met5 = c(0, 0, 4556006.426, 4782909.936, 4484706.271, 
8019957.826, 5112289.476, 8537488.48, 6680688.948, 5959748.061
), Met6 = c(0, 0, 16874476.32, 15721984.25, 18093323.61, 
18619817.92, 22055835.04, 19754379.11, 29211315.88, 27321333.35
), Met7 = c(0, 0, 6604385.457, 6396794.568, 13823034.64, 
15449539.63, 26013299.82, 20262673.28, 35301685.57, 33367520.66
), Met8 = c(0, 0, 6727973.448, 7166827.569, 13238311.46, 
13986568.69, 20957194.23, 19186953.76, 34513697.47, 31192991.75
), Met9 = c(0, 0, 2373752.304, 3259738.104, 1998529.732, 
2387445.15, 2479309.442, 26924139.6, 4611277.427, 2439602.098
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-10L), .Names = c("X1", "Met1", "Met2", "Met3", "Met4", "Met5", 
"Met6", "Met7", "Met8", "Met9"), spec = structure(list(cols =    structure(list(
X1 = structure(list(), class = c("collector_double", "collector"
)), Met1 = structure(list(), class = c("collector_double", 
"collector")), Met2 = structure(list(), class = c("collector_double", 
"collector")), Met3 = structure(list(), class = c("collector_double", 
"collector")), Met4 = structure(list(), class = c("collector_double", 
"collector")), Met5 = structure(list(), class = c("collector_double", 
"collector")), Met6 = structure(list(), class = c("collector_double", 
"collector")), Met7 = structure(list(), class = c("collector_double", 
"collector")), Met8 = structure(list(), class = c("collector_double", 
"collector")), Met9 = structure(list(), class = c("collector_double", 
"collector"))), .Names = c("X1", "Met1", "Met2", "Met3", 
"Met4", "Met5", "Met6", "Met7", "Met8", "Met9")), default =    structure(list(), class = c("collector_guess", 
"collector"))), .Names = c("cols", "default"), class = "col_spec"))
  • 2
    Please provide a reproducible example with dput – Robin Gertenbach Apr 04 '17 at 09:53
  • I'm not familiar with dput usage, should i point it (x) to dataframe of list of plots? 'dput(x, file = "", control = c("keepNA", "keepInteger", "showAttributes"))' – user5305792 Apr 04 '17 at 10:03
  • jus do dput(Dataframe) so we can replicate the data – Robin Gertenbach Apr 04 '17 at 10:07
  • Cheers, for that. I have now done that and can see a file called dput in the same directory. I have done the same fr "bar" which is the names that I had to replace to plot the dataframe "DF" How to I upload it onto here? – user5305792 Apr 04 '17 at 10:14
  • on a sidenote: dput and other tricks to create a reproducible example are explained here : http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Joris Meys Apr 04 '17 at 11:24

1 Answers1

1

Because names(Dataframe)[2:10] is not number. Below will work:

list_of_plots<-lapply(as.numeric(names(Dataframe)[2:10]), function(i) {
ggplot(Dataframe, aes_string(x="X1", y=i)) + geom_point()+labs(x=i, 
y="Intensity", subtitle=bar[i])
})
do.call(grid.arrange, c(list_of_plots, ncol=3))
Erdem Akkas
  • 2,062
  • 10
  • 15
  • Hi there, I tried running and got the following warnings: `Warning message: In lapply(as.numeric(names(Dataframe)[2:10]), function(i) { : NAs introduced by coercion > do.call(grid.arrange, c(list_of_plots, ncol=3)) Warning messages: 1: Removed 10 rows containing missing values (geom_point).` This went on til line 9:..... The plots were empty and the NA were still there as subtitle – user5305792 Apr 04 '17 at 13:19
  • You have to convert your colnames into numeric. If not, you can use as below: `list_of_plots<-lapply(2:10, function(i) { ggplot(Dataframe, aes_string(x="X1", y=i)) + geom_point()+labs(x=i, y="Intensity", subtitle=bar[i]) }) do.call(grid.arrange, c(list_of_plots, ncol=3))` – Erdem Akkas Apr 04 '17 at 13:26
  • That almost worked. Unfortunately the intensities are now all the same (the seem to be set to the same value at the bottome to the graph, i.e in 1st graph, they are all 2, 2nd one thare are all at 3 and so on). Thanks for your advice, this is a major improvement – user5305792 Apr 04 '17 at 13:36
  • In order to refer column, you can use `colnames` like below: `list_of_plots<-lapply(2:10, function(i) { ggplot(Dataframe, aes_string(x="X1", y=colnames(Dataframe[,i]))) + geom_point()+labs(x=i, y="Intensity", subtitle=bar[i]) }) do.call(grid.arrange, c(list_of_plots, ncol=3))` – Erdem Akkas Apr 04 '17 at 13:43
  • That worked!! Its very useful as for what I'm doing, seeing the names is very important – user5305792 Apr 04 '17 at 13:56