我有以下数据集.
structure(list(Rf = c(60.105, 62.205, 64.305, 64.305, 66.405,
66.405), Es = c(0, -0.07, -0.36, -0.47, -0.39, -1.54), H = c(32.3,
-6.9, -5.59, -14.4, -6.5, -21), S = c(267, 136, 151, 114, 143,
90.4), G = c(-46.8, -47.3, -50.7, -48.5, -49, -47.8)), .Names = c("Rf",
"Es", "H", "S", "G"), class = "data.frame", row.names = c("Me",
"Et", "Pr", "iPr", "Bu", "tBu"))
Run Code Online (Sandbox Code Playgroud)
我需要绘制Rf与H,S和G的关系,对于Es列也是如此.共有六个地块.
要绘制单个图,我使用以下函数:
ggplot(data=df, aes(x=Rf, y=H, label=row.names(df))) +
geom_point(size=4) +
geom_text(vjust=2) +
ylab(expression(list(Delta*H^o,~kJ/mol))) +
xlab("Molecular refraction") +
ylim((min(df$H) - 0.2*(abs(min(df$H)))), max(df$H)) +
opts(axis.line = theme_segment(size=1),
axis.text.x = theme_text(colour="black", size=15),
axis.text.y = theme_text(colour="black", size=15),
axis.title.x = theme_text(colour="black", size=15),
axis.title.y = theme_text(colour="black", size=15, angle=90),
panel.background=theme_rect(colour="white"),
panel.grid.minor = theme_blank(),
panel.grid.major = theme_blank())
Run Code Online (Sandbox Code Playgroud)

我想创建一个像这样的函数
f1 <- function(x.label, y.label, df, xtitle, filename) {
g <- ggplot(data=df, aes(x=x.label, y=y.label, label=row.names(df))) +
geom_point(size=4) +
geom_text(vjust=2) +
ylab(expression(list(Delta*y.label^o,~kJ/mol))) +
xlab(xtitle) +
ylim((min(df[,y.label]) - 0.2*(abs(min(df[,y.label])))), max(df[,y.label])) +
opts(axis.line = theme_segment(size=1),
axis.text.x = theme_text(colour="black", size=15),
axis.text.y = theme_text(colour="black", size=15),
axis.title.x = theme_text(colour="black", size=15),
axis.title.y = theme_text(colour="black", size=15, angle=90),
panel.background=theme_rect(colour="white"),
panel.grid.minor = theme_blank(),
panel.grid.major = theme_blank())
ggsave(filename, g)
}
Run Code Online (Sandbox Code Playgroud)
要为上面的例子调用它,比如
f1(Rf, H, df, "Molecular refraction", "D:/temp/1.jpg")
Run Code Online (Sandbox Code Playgroud)
难的是如何转移和正确处理x.label和y.label,因为它们在使用aes的选项ggplot,ylab(expression())并ylim调用.Curent函数返回错误Error in eval(expr, envir, enclos) : object 'x.label' not found
回答:
以下功能正常工作.aes_string应使用insted的的aes的ggplot选项,substitute而不是expression在ylab.
f1 <- function(x.label, y.label, df, x.title, filename) {
g <- ggplot(data=df, aes_string(x=x.label, y=y.label)) +
geom_point(size=4) +
geom_text(aes(label=row.names(df)), vjust=2) +
ylab(substitute(list(Delta*y.label^o,~kJ/mol), list(y.label=y.label))) +
xlab(x.title) +
ylim((min(df[,y.label]) - 0.2*(abs(min(df[,y.label])))), max(df[,y.label])) +
opts(axis.line = theme_segment(size=1),
axis.text.x = theme_text(colour="black", size=15),
axis.text.y = theme_text(colour="black", size=15),
axis.title.x = theme_text(colour="black", size=15),
axis.title.y = theme_text(colour="black", size=15, angle=90),
panel.background = theme_rect(colour="white"),
panel.grid.minor = theme_blank(),
panel.grid.major = theme_blank())
ggsave(filename=filename, plot=g)
}
Run Code Online (Sandbox Code Playgroud)
如果输入x.label为字符(或字符串),即"Rf",则可以使用aes_string映射美观:
aes_string(x = x.label, etc)
Run Code Online (Sandbox Code Playgroud)
对 then进行子集化data.frame也变得很容易:
df[[x.label]]
Run Code Online (Sandbox Code Playgroud)
对于您的表达式,ylab您可能需要使用parseand/or eval,但我现在无法访问 R 并且无法检查。