Hi again!

I feel like I cannot do anything by myself but I would now like to plot 
for all numeric variables I have (14 of them). I wanted to add a loop then.
The code is:

------
#defines the function for the plots (as written by Duncan Murdoch)
twoplots <- function(x, y) {
  ylab <- deparse(substitute(y))  # get the expression passed as y
  xlab <- deparse(substitute(x))  # get the expression passed as x
  hist(y, main=paste("Histogram of ", ylab), xlab=ylab)
  boxplot(y ~ x,  main=paste("Boxplot of", ylab, "by", xlab), xlab=xlab, 
ylab=ylab)
}

#run the function on ssfa with TO_POS as x and ssfa[[i]] as y, the 
numerical variables are from column 7 to 21
for (i in 7:21) {
   with(ssfa, twoplots(TO_POS, ssfa[[i]]))
}
------

I have therefore two questions:
- The code above works fine, but in the titles I get "Histogram of 
ssfa[[i]]" instead of "Histogram of 'variable name'"
- What if I don't want to loop on all variables, but for example, 
variables (=columns) 7 to 10 and 12 to 17? How do I give such breaks and 
ranges?
I admit I'm thinking about it since yesterday and I don't have a clue...

I hope you will be able to help me.
Thanks in advance,
Ivan.



Duncan Murdoch a écrit :
> On 18/01/2010 9:02 AM, Ivan Calandra wrote:
>> Hi everybody!
>>
>> I'm trying to write a script to plot a histogram, a boxplot and a 
>> qq-plot (under Windows XP, R2.10 if it matters)
>>
>> What I want to do: define the variables (x and y) to be used at the 
>> very beginning, so that I don't have to change all occurrences in the 
>> script when I want to plot a different variable.
>>
>> The dataset is called "ssfa". TO_POS is a categorical variable 
>> containing the tooth position for each sample. Asfc is a numerical 
>> variable. In my dataset, I have more variables but it wouldn't 
>> change; I want to plot one numeric vs one category. Do I need to 
>> supply some data? I don't think it's really necessary but let me know 
>> if you would like to.
>>
>> The code of what I do up to now:
>> ---
>> x <- ssfa$TO_POS
>> y <- ssfa$Asfc
>> hist(y, main="Histogram of Asfc", xlab="Asfc")
>> boxplot(y~x, main="Boxplot of Asfc by TO_POS", xlab="TO_POS", 
>> ylab="Asfc")
>> ---
>>
>> I would like something like: hist(y, main="Histogram of y", xlab="y") 
>> but that will add "Asfc" where I write "y".
>> And the same for boxplot(y~x, main="Boxplot of y by x", xlab="x", 
>> ylab="y")
>> I thought about something like:
>> ---
>> cat <- "TO_POS"
>> num <- "Asfc"
>> x <- paste("ssfa$", "TO_POS", sep="")
>> y <- paste("ssfa$", "Asfc", sep="")
>> hist(y, main=paste("Histogram of ", cat, sep=""), xlab=num)
>> ---
>> but it doesn't work since y is a string. I don't know how to get the 
>> syntax correctly. I am on the right path at least?!
>
> I think you're on the wrong path.  You want to write a function, and 
> pass either x and y as arguments, or pass a formula containing both 
> (the former is easier).  For example,
>
> twoplots <- function(x, y) {
>  ylab <- deparse(substitute(y))  # get the expression passed as y
>  xlab <- deparse(substitute(x))  # get the expression passed as x
>  hist(y, main=paste("Histogram of ", ylab), xlab=ylab)
>  boxplot(y ~ x,  main=paste("Boxplot of", ylab, "by", xlab), 
> xlab=xlab, ylab=ylab)
> }
>
> Then
>
> with(ssfa, twoplots(TO_POS, Asfc))
>
> will give you your plots.
>
> Duncan Murdoch
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
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