[R] plotting a function with given formula in ggplot2

Curiouslearn curiouslearn at gmail.com
Thu Nov 10 23:04:57 CET 2011


Thanks Rainer. I did not know about dput(). Here is the data.

mom_hs <- c(1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0,
1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1,
0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1,
1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1,
1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1,
1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1,
1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1,
1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0,
1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1,
1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1,
1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1,
0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1)

and,

kid_score <- c(65L, 98L, 85L, 83L, 115L, 98L, 69L, 106L, 102L, 95L, 91L, 58L,
84L, 78L, 102L, 110L, 102L, 99L, 105L, 101L, 102L, 115L, 100L,
87L, 99L, 96L, 72L, 78L, 77L, 98L, 69L, 130L, 109L, 106L, 92L,
100L, 107L, 86L, 90L, 110L, 107L, 113L, 65L, 102L, 103L, 111L,
42L, 100L, 67L, 92L, 100L, 110L, 56L, 107L, 97L, 56L, 95L, 78L,
76L, 86L, 79L, 81L, 79L, 79L, 56L, 52L, 63L, 80L, 87L, 88L, 92L,
100L, 94L, 117L, 102L, 107L, 99L, 73L, 56L, 78L, 94L, 110L, 109L,
86L, 92L, 91L, 123L, 102L, 105L, 114L, 96L, 66L, 104L, 108L,
84L, 83L, 83L, 92L, 109L, 95L, 93L, 114L, 106L, 87L, 65L, 95L,
61L, 73L, 112L, 113L, 49L, 105L, 122L, 96L, 97L, 94L, 117L, 136L,
85L, 116L, 106L, 99L, 94L, 89L, 119L, 112L, 104L, 92L, 86L, 69L,
45L, 57L, 94L, 104L, 89L, 144L, 52L, 102L, 106L, 98L, 97L, 94L,
111L, 100L, 105L, 90L, 98L, 121L, 106L, 121L, 102L, 64L, 99L,
81L, 69L, 84L, 104L, 104L, 107L, 88L, 67L, 103L, 94L, 109L, 94L,
98L, 102L, 104L, 114L, 87L, 102L, 77L, 109L, 94L, 93L, 86L, 97L,
97L, 88L, 103L, 87L, 87L, 90L, 65L, 111L, 109L, 87L, 58L, 87L,
113L, 64L, 78L, 97L, 95L, 75L, 91L, 99L, 108L, 95L, 100L, 85L,
97L, 108L, 90L, 100L, 82L, 94L, 95L, 119L, 98L, 100L, 112L, 136L,
122L, 126L, 116L, 98L, 94L, 93L, 90L, 70L, 110L, 104L, 83L, 99L,
81L, 104L, 109L, 113L, 95L, 74L, 81L, 89L, 93L, 102L, 95L, 85L,
97L, 92L, 78L, 104L, 120L, 83L, 105L, 68L, 104L, 80L, 120L, 94L,
81L, 101L, 61L, 68L, 110L, 89L, 98L, 113L, 50L, 57L, 86L, 83L,
106L, 106L, 104L, 78L, 99L, 91L, 40L, 42L, 69L, 84L, 58L, 42L,
72L, 80L, 58L, 52L, 101L, 63L, 73L, 68L, 60L, 69L, 73L, 75L,
20L, 56L, 49L, 71L, 46L, 54L, 54L, 44L, 74L, 58L, 46L, 76L, 43L,
60L, 58L, 89L, 43L, 94L, 88L, 79L, 87L, 46L, 95L, 92L, 42L, 62L,
52L, 101L, 97L, 85L, 98L, 94L, 90L, 72L, 92L, 75L, 83L, 64L,
101L, 82L, 77L, 101L, 50L, 90L, 103L, 96L, 50L, 47L, 73L, 62L,
77L, 64L, 52L, 61L, 86L, 41L, 83L, 64L, 83L, 116L, 100L, 42L,
74L, 76L, 92L, 98L, 96L, 67L, 84L, 111L, 41L, 68L, 107L, 82L,
89L, 83L, 73L, 74L, 94L, 58L, 76L, 61L, 38L, 100L, 84L, 99L,
86L, 94L, 90L, 50L, 112L, 58L, 87L, 76L, 68L, 110L, 88L, 87L,
54L, 49L, 56L, 79L, 82L, 80L, 60L, 102L, 87L, 42L, 119L, 84L,
86L, 113L, 72L, 104L, 94L, 78L, 80L, 67L, 104L, 96L, 65L, 64L,
95L, 56L, 75L, 91L, 106L, 76L, 90L, 108L, 86L, 85L, 104L, 87L,
41L, 106L, 76L, 100L, 89L, 42L, 102L, 104L, 59L, 93L, 94L, 76L,
50L, 88L, 70L)

Thanks again.

Bharat

On Thu, Nov 10, 2011 at 3:35 PM, Rainer Schuermann
<rainer.schuermann at gmx.net> wrote:
> You can use dput() to share your data. It is very difficult to replicate a
> certain problem without concrete data, and if you want to encourage people
> to look into your problem, you should make it easy. Pasting data from a
> dput() output takes half a second instead of minutes otherwise spent with
> making up data that might not even fit to the problem!
>
> Rgds,
> Rainer
>
>
> On Thursday 10 November 2011 13:47:10 Curiouslearn wrote:
>> Hi All,
>>
>> I have a scatter plot produced using ggplot2 and I want to add the
>> regression line to this scatter plot. I suppose I can use
>> geom_smooth() to do this, but for the sake of learning ( I am new both
>> to R and ggplot2), I want to try and add it as a function (something
>> that curve() does in the standard R plotting). I did some search and
>> found that stat_function() can be used for this. But somehow it is not
>> working. The following is my code. Can you please tell me where I am
>> going wrong and what the correct code would be:
>>
>> reg1 <- lm(kid_score ~ mom_hs, data=iqdata)
>>
>> scatter  <-  ggplot() + geom_point(data=iqdata,
>>
>> aes(x=as.factor(mom_hs), y=kid_score) )
>>                                + xlab("Mother's High School Status")
>>                                + ylab("Children's Test Score")
>>                                + stat_function(fun=function(x)
>> coef(reg1)[1] + coef(reg1)[2] * x)
>>
>> I understand that you probably do not have the data I am using (I am
>> trying this out from Gelman Hill's book on Multilevel models). But I
>> was hoping you may be able to point out the error without the data.
>>
>> Thanks for your help.
>>
>> ______________________________________________
>> R-help at 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.
>



More information about the R-help mailing list