Ho il seguente set di dati.Fare una funzione che disegnano una trama e salvarlo
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"))
Ho bisogno di tracciare Rf vs H, S e G, e lo stesso per colonna Es. Ci sono sei trame in totale.
Per disegnare trama singolo Io uso la seguente funzione:
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())
Voglio creare una funzione come
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)
}
Chiamarlo per l'esempio precedente come
f1(Rf, H, df, "Molecular refraction", "D:/temp/1.jpg")
la difficoltà è come trasferire e gestire correttamente x.label
e y.label
, dal momento che sono utilizzati in aes
possibilità di ggplot
, ylab(expression())
e ylim
chiamate. Funzione Curent restituisce un errore Error in eval(expr, envir, enclos) : object 'x.label' not found
RISPOSTA:
La seguente funzione funziona correttamente. aes_string
deve essere utilizzato da aes
nell'opzione ggplot
e substitute
anziché expression
in 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)
}
'aes_string' dovrebbe funzionare ma quando cambio' aes' a 'aes_string' sembra che l'opzione' label' non funzioni correttamente, perché la funzione restituisce un errore: 'eval (expr, envir, enclos): object ' Me 'non trovato'. Grazie per il suggerimento di utilizzare le funzioni 'eval' e' parse', cercherò di usarle. – DrDom