0

This is a function I am using.

corr.plot.contour<-function(data,x,y,xl,yl,save){
  ggplot(data, aes_string(x=x, y=y)) +
    geom_density2d(colour="black")+
    scale_color_manual(name = "Climate Classes",values=cols)+
    geom_smooth(method=lm, se=FALSE,color="black")  +  # Don't add shaded confidence region    
    theme_bw()+
    theme(panel.grid.major = element_line(colour = "#808080"))+
    xlab(xl)+
    ylab(yl)+
    ggsave(save,width=6, height=4,dpi=300)
  }

I would like to pass argument to XLAB and YLAB that contains subscript.

corr.plot.contour(data=foo1,x='log(area)',y='log(fd)',xl= 'expression("Basin Area" ~ (km^{2})',
                      yl='expression("Flood Duration" ~ (hour))',save='fd_area_log_facet.png')

But the code is not reading the Expression. How can I enter km^2 etc in an argument?

dput(droplevels(head(foo1)))

structure(list(id = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "Csb", class = "factor"), 
    fips = c(4011L, 4011L, 4011L, 4011L, 4011L, 4011L), gauge = c(9444500L, 
    9444500L, 9444500L, 9444500L, 9442000L, 9442000L), lat = c(33.049444, 
    33.049444, 33.049444, 33.049444, 32.965833, 32.965833), lon = c(-109.295278, 
    -109.295278, -109.295278, -109.295278, -109.309722, -109.309722
    ), start = structure(c(5L, 2L, 4L, 1L, 3L, 6L), .Label = c("1/12/1993 7:00", 
    "1/19/1993 2:15", "1/30/2008 9:15", "11/12/1994 13:30", "3/2/1991 7:30", 
    "9/22/1997 20:45"), class = "factor"), end = structure(c(5L, 
    2L, 4L, 1L, 3L, 6L), .Label = c("1/12/1993 13:30", "1/19/1993 6:30", 
    "1/30/2008 9:30", "11/13/1994 11:00", "3/2/1991 20:30", "9/23/1997 17:00"
    ), class = "factor"), peakq = c(438.9111, 1248.7729, 600.3171, 
    404.9309, 267.0279, 478.5547), peakt = structure(c(5L, 2L, 
    4L, 1L, 3L, 6L), .Label = c("1/12/1993 8:45", "1/19/1993 6:30", 
    "1/30/2008 9:23", "11/12/1994 21:15", "3/2/1991 13:15", "9/23/1997 8:15"
    ), class = "factor"), dt = c(5.75, 4.25, 7.75, 1.75, 0.1333, 
    11.5), HUC = c(15040004L, 15040004L, 15040004L, 15040004L, 
    15040002L, 15040002L), agency = structure(c(1L, 1L, 1L, 1L, 
    1L, 1L), .Label = "USGS", class = "factor"), regulation = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L), .Label = "Undefined", class = "factor"), 
    gname = structure(c(2L, 2L, 2L, 2L, 1L, 1L), .Label = c("GILA RIVER NEAR CLIFTON, AZ.", 
    "SAN FRANCISCO RIVER AT CLIFTON, AZ."), class = "factor"), 
    area = c(7163.9071, 7163.9071, 7163.9071, 7163.9071, 10385.8523, 
    10385.8523), carea = c(7156.1371, 7156.1371, 7156.1371, 7156.1371, 
    0, 0), q2 = c(196.2357, 196.2357, 196.2357, 196.2357, 168.2021, 
    168.2021), q5 = c(512.5349, 512.5349, 512.5349, 512.5349, 
    325.6437, 325.6437), q10 = c(855.1688, 855.1688, 855.1688, 
    855.1688, 475.723, 475.723), q25 = c(1500.7929, 1500.7929, 
    1500.7929, 1500.7929, 739.0697, 739.0697), q50 = c(2171.9021, 
    2171.9021, 2171.9021, 2171.9021, 1002.4164, 1002.4164), q100 = c(3029.9026, 
    3029.9026, 3029.9026, 3029.9026, 1330.8918, 1330.8918), q200 = c(4134.2596, 
    4134.2596, 4134.2596, 4134.2596, 1741.4861, 1741.4861), q500 = c(6059.8052, 
    6059.8052, 6059.8052, 6059.8052, 2449.4072, 2449.4072), action = c(297.3269, 
    297.3269, 297.3269, 297.3269, 263.6298, 263.6298), minor = c(509.7032, 
    509.7032, 509.7032, 509.7032, -0.0283, -0.0283), moderate = c(736.238, 
    736.238, 736.238, 736.238, -0.0283, -0.0283), major = c(2035.9813, 
    2035.9813, 2035.9813, 2035.9813, -0.0283, -0.0283), usgs_area = c(7163.91, 
    7163.91, 7163.91, 7163.91, 10385.9, 10385.9), est_area = c(7255, 
    7255, 7255, 7255, 10272, 10272), error = c(1.27155, 1.27155, 
    1.27155, 1.27155, 1.09622, 1.09622), el = c(0.521385, 0.521385, 
    0.521385, 0.521385, 0.434142, 0.434142), k = c(0.213505, 
    0.213505, 0.213505, 0.213505, 0.148031, 0.148031), bl = c(184338, 
    184338, 184338, 184338, 263421, 263421), r = c(2054.38, 2054.38, 
    2054.38, 2054.38, 2066.19, 2066.19), rr = c(0.0111447, 0.0111447, 
    0.0111447, 0.0111447, 0.00784367, 0.00784367), si = c(0.00757568, 
    0.00757568, 0.00757568, 0.00757568, 0.00513591, 0.00513591
    ), rdd = c(0.816995, 0.816995, 0.816995, 0.816995, 0.928348, 
    0.928348), rbm = c(5460, 5460, 5460, 5460, 8809, 8809), rfocf = c(0.762154, 
    0.762154, 0.762154, 0.762154, 0.848173, 0.848173), rrg = c(1.67842, 
    1.67842, 1.67842, 1.67842, 1.91814, 1.91814), precip = c(529.942, 
    529.942, 529.942, 529.942, 465.025, 465.025), temp = c(10.4292, 
    10.4292, 10.4292, 10.4292, 12.1328, 12.1328), fd = c(13, 
    4.25, 21.5, 6.5, 0.25, 20.25), tp = c(5.75, 4.25, 7.75, 1.75, 
    0.133333333333333, 11.5), rt = c(7.25, 0, 13.75, 4.75, 0.116666666666667, 
    8.75), nfd = c(0.00181465223076385, 0.000593251690826644, 
    0.00300115561241714, 0.000907326115381926, 2.40712069436997e-05, 
    0.00194976776243968), ntp = c(0.000802634640530165, 0.000593251690826644, 
    0.00108181190680153, 0.000244280107987442, 1.28379770366399e-05, 
    0.00110727551941019), nrt = c(0.00101201759023369, 0, 0.00191934370561561, 
    0.000663046007394484, 1.12332299070599e-05, 0.000842492243029491
    ), nq = c(0.0612670005170782, 0.17431450220788, 0.0837974434369759, 
    0.0565237508454011, 0.0257107353625662, 0.0460775568703206
    ), Group.1 = c(4011L, 4011L, 4011L, 4011L, 4011L, 4011L), 
    county = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "Greenlee", class = "factor"), 
    class = c("Csb", "Csb", "Csb", "Csb", "Csb", "Csb"), prop = c(0.742, 
    0.742, 0.742, 0.742, 0.742, 0.742), state = c("arizona", 
    "arizona", "arizona", "arizona", "arizona", "arizona")), .Names = c("id", 
"fips", "gauge", "lat", "lon", "start", "end", "peakq", "peakt", 
"dt", "HUC", "agency", "regulation", "gname", "area", "carea", 
"q2", "q5", "q10", "q25", "q50", "q100", "q200", "q500", "action", 
"minor", "moderate", "major", "usgs_area", "est_area", "error", 
"el", "k", "bl", "r", "rr", "si", "rdd", "rbm", "rfocf", "rrg", 
"precip", "temp", "fd", "tp", "rt", "nfd", "ntp", "nrt", "nq", 
"Group.1", "county", "class", "prop", "state"), row.names = 99096:99101, class = "data.frame")
1
  • It would be very nice that when asking questions about plotting, you include a sample data set so that we can run the code and see the plot. It makes it much easier to test possible solutions. Commented Jul 21, 2014 at 21:17

1 Answer 1

3

You should just pass those parameters in as expressions, not as strings

corr.plot.contour(data=foo1,
    x='log(area)',y='log(fd)',
    xl= expression("Basin Area" ~ (km^{2})),
    yl= expression("Flood Duration" ~ (hour)), "save.png")

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

I use this function several dozen times. So I would like to have in function. How can I do it? I know how to do this one though. Thanks.
I didn't change your function, I only changed what you were passing in to the function. I just removed the single quotes basically.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.