Make a function that draws a plot and saves it

I have the following dataset.

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

I need to build Rf vs H, S and G, as well as for column Es. Only 6 sites.

To draw a single graph, I use the following function:

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

enter image description here

I want to create a function like

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

To call it for the example above, for example

f1(Rf, H, df, "Molecular refraction", "D:/temp/1.jpg")

The difficulty lies in how to transmit and process correctly, x.labeland y.labelsince they are used in an aesoption ggplot, ylab(expression())and by ylimcalls. Curent function returns an errorError in eval(expr, envir, enclos) : object 'x.label' not found

ANSWER:

The following function is working correctly. aes_stringshould use instable from aesin options ggplotand substituteinstead of expressionin 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)
}
+5
source share
1 answer

x.label ( ), .. "Rf", aes_string :

aes_string(x = x.label, etc)

data.frame :

df[[x.label]]

ylab parse / eval, R , .

+3

All Articles