Hi Ken,
As far as I can see, ggtitle accepts a single string. The help page is
a bit obscure, implying that you can change the title with the "labs"
function(?), but using the same explicit string in the "ggtitle" line,
perhaps for didactic purposes. You seem to be asking to substitute
your own version of a string that is popping out in ggplot
automatically, e.g. "Tue" -> "Tuesday". The help page doesn't discuss
whether it is possible to access the string that will be automatically
used as the title. If ggtitle automatically uses the name of the
object that you are plotting, something like this may work:

my_x<-1:5
myfun<-function(x,mytitle) {
 plot(x)
 old_title<-deparse(substitute(x))
 title(gsub("x",mytitle,old_title))
}
myfun(my_x,"wonderful X")

The reason that I have done this in base graphics is that I could not
get an equivalent plot in ggplot.

Jim


On Wed, Jun 29, 2016 at 9:48 AM, Bert Gunter <bgunter.4...@gmail.com> wrote:
> I frankly don't know what the heck you are doing but,
>
> (inline below)
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along
> and sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
> On Tue, Jun 28, 2016 at 1:46 PM, KMNanus <kmna...@gmail.com> wrote:
>> Thanks for getting back to me.  None of them work, because I’m trying to use 
>> the string in the function  call - myfun(z, “string”) to replace the empty 
>> space in ggtitle.
>>
>> ***When I call myfun(z, gsub(“______”, “string (or any word)”, myfun), I get 
>> an error msg because gsub is looking for a data frame, not a function.***
>
> That is absolutely, unequivocally, positively, FALSE. See ?gsub for
> what gsub() *does* want.
>
> This suggests to me that you may also not understand functions and/or
> function arguments, so I would recommend that you try a web tutorial
> or two on R function to see where your confusion may lie. However, I
> freely admit (see my initial remark) that I may not understand what
> you are trying to do, so maybe that's not it. I will say if I wanted
> to give an arbitrary character string to a function that called a
> title function, titleFUN, I'd do it like this:
>
> myfun <- function(..., mytitle){
>
> ## lots of stuff
>
> titleFUN(mytitle)
>
> ## more stuff
>
> }
>
> and call it by:
>
> myfun(..., "myReallyCuteTitle")
>
>
> I do not use ggplot and so do not know its detailed syntax; but I
> would be surprised if it did not accept something along these lines...
>
> Cheers,
> Bert
>
>
>
>
>
>
>>
>> Paste or paste0 doesn’t work, either because I still have to replace the 
>> space in ggtitle with something.
>>
>> In short, I want to put another argument into the function that will enable 
>> me to call it and fill that space.
>>
>> I’m stumped.
>>
>>
>> Ken
>> kmna...@gmail.com
>> 914-450-0816 (tel)
>> 347-730-4813 (fax)
>>
>>
>>
>>> On Jun 28, 2016, at 3:42 PM, Greg Snow <538...@gmail.com> wrote:
>>>
>>> There are several options.  The option that is most like search and
>>> replace is to use the `sub` or `gsub` function (or similar functions
>>> in added packages).  But you may be able to accomplish what you want
>>> even simpler by using the `paste`, `paste0`, or `sprintf` functions.
>>>
>>> On Tue, Jun 28, 2016 at 12:10 PM, KMNanus <kmna...@gmail.com> wrote:
>>>> Thanks for getting back to me, I’m sorry if I was unclear.
>>>>
>>>> What I’m trying to figure out is the equivalent of “find and replace” in 
>>>> Word.
>>>>
>>>> I have a function -
>>>>
>>>> myfun <- function(z){
>>>> ggplot(df, aes(x,y)+
>>>> geom_point() +
>>>> ggtitle (“_______ quick brown fox jumped over the lazy dog”)}
>>>>
>>>> Calling myfun(z) works perfectly.  What I’m trying to do is add a string 
>>>> to myfun so that it would read:  function(z, “string”){
>>>>
>>>> Then I could call myfun(z, “string”) to replace the space in ggtitle.  Is 
>>>> there a straightforward way to do that?
>>>>
>>>> Ken
>>>> kmna...@gmail.com
>>>> 914-450-0816 (tel)
>>>> 347-730-4813 (fax)
>>>>
>>>>
>>>>
>>>>> On Jun 28, 2016, at 12:20 PM, David Winsemius <dwinsem...@comcast.net> 
>>>>> wrote:
>>>>>
>>>>>>
>>>>>> On Jun 27, 2016, at 6:12 PM, KMNanus <kmna...@gmail.com> wrote:
>>>>>>
>>>>>> I’m inexperience but am trying to get my head around using functions to 
>>>>>> make a number of ggplots easier to do.
>>>>>>
>>>>>> I have a function that creates a ggplot taking one input variable as an 
>>>>>> argument. The variable name is shorthand for the actual variable 
>>>>>> (variable name = tue, Actual name = Tuesday).  Since I want to use the 
>>>>>> actual variable name in ylab and ggtitle, I’d like to add a second 
>>>>>> argument, new.name, to the function which would allow me to utilize both 
>>>>>> inputs as arguments but have not been successful.  I tried creating a 
>>>>>> function within the function to accomplish this, using 
>>>>>> deparse(substitute(new.name))and also using the code you see below.
>>>>>>
>>>>>>
>>>>>> myfun <- function(myvar, new.name){
>>>>>> function(new.name){return(as.character(substitute(new.name)))}
>>>>>> ggplot(b12.2, aes(x= games,  y = myvar, col = Group))+
>>>>>> geom_point() +
>>>>>> geom_line()+
>>>>>> xlab("Minimum Games" ) +
>>>>>> ylab(paste(new.name, “Average Change"))+
>>>>>> ggtitle(new.name, "Change \n as a Function of Minimum Number of Games”)+
>>>>>> theme_bw()
>>>>>>
>>>>>> When call myfun(myvar, new.name), I get an error msg “new.name is not 
>>>>>> found” whether I call new.name or Tuesday.
>>>>>
>>>>> Q1: At the moment we have no idea _how_ you might be "calling" this 
>>>>> function. We also do not know what might be assigned to `myvar` or 
>>>>> `new.name` in the calling environment. Sounds unlikely that you are 
>>>>> typing:
>>>>>
>>>>> myfun(myvar, new.name)  ## ?, so was there a loop/lapply calling method?
>>>>>
>>>>>
>>>>> Q2: You should not imagine that the inner anonymous function would be 
>>>>> altering the value of `new.name`. (That function is only defined and 
>>>>> never called, and even if it were called, it would not change the value 
>>>>> of the `new.name` in the calling environment.)
>>>>>
>>>>> Since more than 12 hours have passed with no response, we can surmise 
>>>>> that many people have passed the question over after concluding there was 
>>>>> an incomplete problem description. You should post code that can be 
>>>>> cut-pasted into a session and produce the error you are getting. It would 
>>>>> include data setup and a loop or loop equivalent to show how the function 
>>>>> is being called.
>>>>>
>>>>> --
>>>>> David.
>>>>>
>>>>>
>>>>>>
>>>>>> I want ggplot to automatically insert Tuesday into ylab and ggtitle.
>>>>>>
>>>>>> Can anyone help me with this?  Thanks for your patience.
>>>>>>
>>>>>> Ken
>>>>>> kmna...@gmail.com <mailto:kmna...@gmail.com>
>>>>>> 914-450-0816 (tel)
>>>>>> 347-730-4813 (fax)
>>>>>>
>>>>>>
>>>>>>
>>>>>> ______________________________________________
>>>>>> R-help@r-project.org <mailto:R-help@r-project.org> mailing list -- To 
>>>>>> UNSUBSCRIBE and more, see
>>>>>> https://stat.ethz.ch/mailman/listinfo/r-help 
>>>>>> <https://stat.ethz.ch/mailman/listinfo/r-help>
>>>>>> PLEASE do read the posting guide 
>>>>>> http://www.R-project.org/posting-guide.html 
>>>>>> <http://www.r-project.org/posting-guide.html>
>>>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>>
>>>>> David Winsemius
>>>>> Alameda, CA, USA
>>>>
>>>> ______________________________________________
>>>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting guide 
>>>> http://www.R-project.org/posting-guide.html
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>>
>>>
>>> --
>>> Gregory (Greg) L. Snow Ph.D.
>>> 538...@gmail.com
>>
>> ______________________________________________
>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to