Functions & variable names

30 views
Skip to first unread message

simeond

unread,
Dec 30, 2010, 9:06:57 AM12/30/10
to ggplot2
Hi folks

I think I am tying myself in knots with functions, variable names and
environments.

I would like to create a function within a function to facet groups of
time series data. The first function takes a data set and melts it,
adding a new column to group series of data. The second function
calls ggplot and plots two facets, one with a line chart, the other
with an area chart. The final output would be along the lines of
http://dl.dropbox.com/u/15384925/Economic%20Indicators.pdf

I get a variety of errors as i tinker with the code suggesting that
the ggplot function cant find the right variables (no layers,
undefined columns, variables not found in the molten data etc).

Very grateful for any help

Simeon

########################
###

library(ggplot2)
data(economics)

adGraphData <- function(data,period,indicator1,indicator2) {
df <- melt(data,id=period,measure=c(indicator1,indicator2))
df$panel <- "ind1"
df[df$variable %in% indicator2,"panel"] <- "ind2"
df$panel <- factor(df$panel,levels=c("ind1","ind2"),ordered=T)
df
}

adGraph <- function(data,period,indicator1,indicator2,...){
df <-
adGraphData(data=data,period=period,indicator1=indicator1,indicator2=indicator2)
p <- ggplot(df,aes_string(period,"value"))
p + geom_line()
p + geom_line(subset=.(panel=="ind1"),aes(colour=factor(df
$variable)),size=1.2)+
geom_smooth(subset=.(panel=="ind1"),size=1)+
geom_area(subset=.(panel=="ind2"),aes(fill=factor(df
$variable)),alpha=0.6)+
scale_y_continuous("",formatter=comma)+
scale_colour_grey("Ind1") +
scale_fill_brewer("Ind2",palette="Set1")+
facet_grid(df$panel ~. ,heights=c(2,1),scales="free")

}

adGraph(economics,period="date",indicator1="unemploy",indicator2=c("psavert","uempmed"))

simeon duckworth

unread,
Dec 30, 2010, 12:12:22 PM12/30/10
to Brandon Hurr, ggplot2
hi brandon

i was hoping the example would run with the 'economics' dataset that is bundled in the ggplot2 library.  let me know if that doesn't work - and i will put together an alternative.

simeon



On Thu, Dec 30, 2010 at 4:09 PM, Brandon Hurr <brando...@gmail.com> wrote:
Do you have some sample data that we could use to play around with? 

-B


--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2


Brandon Hurr

unread,
Dec 30, 2010, 12:08:44 PM12/30/10
to simeond, ggplot2
Wait, I see. Nevermind about the data. I think the important thing is that you are trying to feed variables to your ggplot call and it's not sure what to do with them. Try using aes_string(). 

?aes_string()

B

On Thu, Dec 30, 2010 at 16:09, Brandon Hurr <brando...@gmail.com> wrote:
Do you have some sample data that we could use to play around with? 

-B
On Thu, Dec 30, 2010 at 14:06, simeond <simeond...@gmail.com> wrote:

Brandon Hurr

unread,
Dec 30, 2010, 5:51:46 PM12/30/10
to simeon duckworth, ggplot2
I guess we went off list. I think we fixed it but so others can chip in here is my solution.

-b

On Thu, Dec 30, 2010 at 18:51, Brandon Hurr <brando...@gmail.com> wrote:
It's not perfect but... 

adGraph <- function(data,period, indicator1, indicator2,...){


df <- melt(data,id=period,measure=c(indicator1,indicator2))

   df$panel <- "ind1"

   df[df$variable %in% indicator2,"panel"] <- "ind2"

   df$panel <- factor(df$panel,levels=c("ind1","ind2"),ordered=T)

   

   print(head(df))

   

   p<-ggplot(df,aes_string(x = "date", y = "value")) +

geom_line() +

geom_line(subset=.(panel=="ind1"),aes_string(colour="factor(variable)"),size=1.2)+

        geom_smooth(subset=.(panel=="ind1"),size=1)+

        geom_area(subset=.(panel=="ind2"),aes_string(fill="factor(variable)"),alpha=0.6)+

        scale_y_continuous("",formatter=comma)+

        scale_colour_grey("Ind1") +

scale_fill_brewer("Ind2",palette="Set1")+

        facet_grid(panel ~. ,heights=c(2,1),scales="free")

print(p)

   }



adGraph(data=economics, period="date", indicator1="unemploy", indicator2=c("psavert", "uempmed"))


Seems to work. I hope that helps you get to where you're going. 


Brandon


On Thu, Dec 30, 2010 at 18:24, Brandon Hurr <brando...@gmail.com> wrote:
Simeon, 

I went through and followed all of your code outside of the function. The problems came up when you used "df$panel" or df$variable to tell ggplot what to plot. I believe that anywhere you are passing a variable (say from a function) then you need to put that inside of aes_string(). I could be wrong, but I think that is the problem. 

Brandon

On Thu, Dec 30, 2010 at 17:45, simeon duckworth <simeond...@gmail.com> wrote:
hi brandon,

thanks ...'aes_string' is already in there.  if i explicitly declare "x=" & "y=", i get the warning that 'df$variable is of type closure and is not subsettable'.  i suspect the issue is more how to get the subetting variable ("panel") to be evaluated in the right environment.

very grateful for any further thoughts..

Dennis Murphy

unread,
Dec 30, 2010, 10:59:35 PM12/30/10
to Brandon Hurr, simeon duckworth, ggplot2
Hi:

My interpretation of the original question was how to pass the value of period declared in the function call into aes_string(). My first thought was substitute(), but I couldn't figure out  the right mantra so I tried bquote() instead.

Here's my tweak on Brandon's modification:

# ---------------

adGraph <- function(data, period, indicator1, indicator2,...){

# Reshape the input data frame
df <- melt(data, id=period, measure=c(indicator1, indicator2))

   df$panel <- "ind1"
   df[df$variable %in% indicator2,"panel"] <- "ind2"
   df$panel <- factor(df$panel, levels=c("ind1","ind2"), ordered=T)

# Create the plot - pass value of period into aes_string() via bquote()
p<-ggplot(df, aes_string(x = bquote(.(period)), y = "value")) + geom_line() +

        geom_line(subset=.(panel=="ind1"),
                aes_string(colour="factor(variable)"), size = 1.2) +
        geom_smooth(subset=.(panel=="ind1"),size=1) +
        geom_area(subset=.(panel=="ind2"),
                  aes_string(fill="factor(variable)"), alpha=0.6) +
        scale_y_continuous("", formatter=comma) +
        scale_colour_grey("Ind1") +
        scale_fill_brewer("Ind2", palette="Set1")+
        facet_grid(panel ~ . , heights=c(2,1), scales="free_y")


print(p)
   }

adGraph(data=economics, period="date", indicator1="unemploy", indicator2=c("psavert", "uempmed"))

# ------------------
AFAICT, it yields the same plot as Brandon's solution and should be a little more general.

The warning is simply to state that scale_*_brewer() uses a minimum of three and a maximum of eight colors in the palette. I'm not quite sure of what's going on in the area plot, but I haven't looked at the data frame that closely and don't have much occasion to use area plots. It appears that the bottom panel is of the contemporaneous time series of psavert (whose minimum value is negative) and uempmed. The area plot looks nice, but the shadings from pink to dark red and from light to dark blue (not to mention the little section of black at the end) isn't represented in the legend so it kind of begs the question as to what it's supposed to represent...I'm guessing overlap of one series onto the other in combination with the alpha transparency, but it's not obvious, which means some explanation is likely to be necessary if the graph goes in a report as shown.

HTH,
Dennis

simeon duckworth

unread,
Dec 31, 2010, 4:34:15 AM12/31/10
to Dennis Murphy, Brandon Hurr, ggplot2
Thanks Dennis, Brandon and Ahmadou.  Works as intended.  'bquote' looks to be quite a handy function.

Dennis - the economics data is only for illustration.  The actual use is to plot sales v marketing investment (which is additive & hence the stacked area plot).

thanks for the help.  Happy new year to all.

simeon
Reply all
Reply to author
Forward
0 new messages