[R] colour input in geom line ggplote2

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Wed Dec 23 18:07:14 CET 2015


There is a problem built into your question: you are treating carb graphically as a continuous variable, yet by asking for a line plot of mean values you seem to be assuming it is discrete. Below are several possible interpretations of your intent. 

Continuous:

library(ggplot2)
p <- ggplot( mtcars, aes( x=carb, y=mpg ) ) + geom_point()
p + stat_summary( geom="ribbon", fun.ymin="min", fun.ymax="max" )
p2 <- p + stat_summary( geom="ribbon"
, fun.ymin="min"
, fun.ymax="max"
, alpha=0.2
, fill="blue"
)

# local regression
p + geom_smooth()
# literal interpretation 
p2 + geom_hline( yintercept = mean( mtcars$mpg ), colour="red" )
# linear regression 
p2 + geom_smooth( method="lm", se=FALSE, colour="red" )

Discrete:

mtcarsmean <- aggregate( mtcars$mpg, mtcars[ , "carb", drop=FALSE ], mean )

# discrete means
p2 + geom_line( data=mtcarsmean, mapping=aes( x=carb, y=x ), colour="red" )

The reason this has to be done explicitly outside of the geom_smooth mechanism is because of this conflict between graphically continuous and conceptually discrete data.

-- 
Sent from my phone. Please excuse my brevity.

On December 22, 2015 11:43:23 PM PST, pushan chakraborty <pushan.zoology at gmail.com> wrote:
>Dear list
>
>I want to put a line command in the following and also want to
>attribute
>colour in the geom line command line. It would be a ribbon plot showing
>the
>mean line. Is there any help?
>
>
>p <- ggplot(mtcars, aes(x=carb, y=mpg)) + geom_point()
>
>p + stat_summary(geom="ribbon", fun.ymin="min", fun.ymax="max")
>
>p + stat_summary(geom="ribbon", fun.ymin="min", fun.ymax="max",
>        alpha=0.2, fill="blue")
>
>
>
>
>
>-- 
>Pushan Chakraborty
>CSIR - SRF
>
>Center for Pollination Studies, University of Calcutta
>35, Ballyguanje Circular Road, Kolkata - 700019
>          &
>Wildlife Institute of India
>Chandrabani, Dehradun - 248001
>
>webpage:
>
>http://cpscu.in/?page_id=51
>
>Skype: cpushan
>
>	[[alternative HTML version deleted]]
>
>______________________________________________
>R-help at 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.

	[[alternative HTML version deleted]]



More information about the R-help mailing list