ggplot循环处理特殊字符

ggplot loop deal with special characters

您好,我正在尝试使用 gridExtra 绘制定义数量的图表。 这是有效的,但不幸的是它没有处理其名称中的特殊字符。我尝试通过使用 R 友好名称来解决这个问题,并将实际名称添加为副标题

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

如果我输入 bar[2],所有图表都会得到实际名称,但它始终是同一个名称,而如果我将 bar 设置为 i,所有图表都会得到 NA。

我用来适合 R 的名字是 Met1、Met2、Met3、Met4、Met5、Met6、Met7、Met8、Met9 和 Met10 我在地块上需要的名称示例是:

-(-)-科里内酯
-(2R)-2,3-二羟基丙酸
-(D-(+)-甘油酸?)
-1,5-萘二胺
-12-氨基十二烷酸
-2,5-di-tert-Butylhydroquinone
-2,6-di-tert-Butylphenol
-2-Amino-N,N-diethylacetamide
-2-乙基-2-苯基丙二酰胺
-2-萘磺酸

这是复制栏(名称)的输入:

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

这是重现数据帧的输入:

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

因为names(Dataframe)[2:10]不是数字。以下将起作用:

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