R-help I want to add two smooth lines (geom_smooth()) for each scatter plot. How do I do that? ggplot() + geom_point(data=data, aes(x=timeline, y=deliveries), color="blue") + geom_point(data=data, aes(x=timeline, y=launches), color="red") + xlab("Deliveries") + ylab("Launches") + ggtitle("Scatterplot of Launches vs. Deliveries") Jeff [[alternative HTML version deleted]]
Jeff, You need to reshape your data frame. If you use ggplot, you will often have to present your data in "long format" Use the reshape2 package. I made a sample data frame because you didn't provide one. I also change your x and y labels because they made no sense. data <- data.frame( timeline = 1:10, launches = sample(10:20, 10), deliveries = sample(10:20, 10) ) library(reshape2) dataNew <- melt(data = data, id.vars = 'timeline', variable.name = 'launchOrDelivery') ggplot(data=dataNew, aes(x=timeline, y=value), color= launchOrDelivery) + geom_point(aes(color= launchOrDelivery)) + geom_smooth(aes(group = launchOrDelivery, color= launchOrDelivery), se FALSE) + xlab("timeline") + ylab("Launches/Deliveries") + ggtitle("Scatterplot of Launches vs. Deliveries") On Thu, Aug 23, 2018 at 9:39 PM Jeff Reichman <reichmanj at sbcglobal.net> wrote:> R-help > > > > I want to add two smooth lines (geom_smooth()) for each scatter plot. How > do I do that? > > > > ggplot() + > > geom_point(data=data, aes(x=timeline, y=deliveries), color="blue") + > > geom_point(data=data, aes(x=timeline, y=launches), color="red") + > > xlab("Deliveries") + > > ylab("Launches") + > > ggtitle("Scatterplot of Launches vs. Deliveries") > > > > Jeff > > > [[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]]
Hello, The trick is to reshape your data from wide to long format. There are many ways to do this, I will use package reshape2. Make up a dataset: library(ggplot2) library(reshape2) set.seed(9773) n <- 20 data <- data.frame(timeline = 1:n, deliveries = log(1:n) + runif(n), launches = (1:n)/4 + runif(n)) # reformat it long <- melt(data, id.vars = "timeline") head(long) # et voila! ggplot(long, aes(timeline, value, colour = variable)) + geom_point() + stat_smooth() + xlab("Deliveries") + ylab("Launches") + ggtitle("Scatterplot of Launches vs. Deliveries") Use the smoothing function of your choice, I left it with the default loess. Hope this helps, Rui Barradas On 24/08/2018 03:38, Jeff Reichman wrote:> R-help > > > > I want to add two smooth lines (geom_smooth()) for each scatter plot. How > do I do that? > > > > ggplot() + > > geom_point(data=data, aes(x=timeline, y=deliveries), color="blue") + > > geom_point(data=data, aes(x=timeline, y=launches), color="red") + > > xlab("Deliveries") + > > ylab("Launches") + > > ggtitle("Scatterplot of Launches vs. Deliveries") > > > > Jeff > > > [[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. >--- This email has been checked for viruses by AVG. https://www.avg.com
Sorry, should be geom_smooth, not stat_smooth. They both work the same way or very close to it. Rui Barradas On 24/08/2018 05:08, Rui Barradas wrote:> Hello, > > The trick is to reshape your data from wide to long format. > There are many ways to do this, I will use package reshape2. > > Make up a dataset: > > > library(ggplot2) > library(reshape2) > > set.seed(9773) > n <- 20 > data <- data.frame(timeline = 1:n, > ?????????????????? deliveries = log(1:n) + runif(n), > ?????????????????? launches = (1:n)/4 + runif(n)) > > # reformat it > long <- melt(data, id.vars = "timeline") > head(long) > > # et voila! > ggplot(long, aes(timeline, value, colour = variable)) + > ??? geom_point() + > ??? stat_smooth() + > ??? xlab("Deliveries") + > ??? ylab("Launches") + > ??? ggtitle("Scatterplot of Launches vs. Deliveries") > > > Use the smoothing function of your choice, I left it with the default > loess. > > Hope this helps, > > Rui Barradas > > > On 24/08/2018 03:38, Jeff Reichman wrote: >> R-help >> >> >> I want to add two smooth lines (geom_smooth()) for each scatter plot. >> How >> do I do that? >> >> >> ggplot() + >> >> ?? geom_point(data=data, aes(x=timeline, y=deliveries), color="blue") + >> >> ?? geom_point(data=data, aes(x=timeline, y=launches), color="red") + >> >> ?? xlab("Deliveries") + >> >> ?? ylab("Launches") + >> >> ?? ggtitle("Scatterplot of Launches vs. Deliveries") >> >> >> Jeff >> >> >> ????[[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. >> > > --- > This email has been checked for viruses by AVG. > https://www.avg.com > > ______________________________________________ > 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.
Hello, if you want to fit different models to each of deliveries and launches, use the wide format instead: ggplot(data = data, aes(x = timeline)) + geom_point(aes(y = deliveries), color = "blue") + geom_smooth(aes(y = deliveries), color = "blue", method = lm, formula = y ~ log(x)) + geom_point(aes(y = launches), color = "red") + geom_smooth(aes(y = launches), color = "red", method = lm, formula = y ~ x) + xlab("Deliveries") + ylab("Launches") + ggtitle("Scatterplot of Launches vs. Deliveries") Hope this helps, Rui Barradas On 24/08/2018 05:08, Rui Barradas wrote:> Hello, > > The trick is to reshape your data from wide to long format. > There are many ways to do this, I will use package reshape2. > > Make up a dataset: > > > library(ggplot2) > library(reshape2) > > set.seed(9773) > n <- 20 > data <- data.frame(timeline = 1:n, > ?????????????????? deliveries = log(1:n) + runif(n), > ?????????????????? launches = (1:n)/4 + runif(n)) > > # reformat it > long <- melt(data, id.vars = "timeline") > head(long) > > # et voila! > ggplot(long, aes(timeline, value, colour = variable)) + > ??? geom_point() + > ??? stat_smooth() + > ??? xlab("Deliveries") + > ??? ylab("Launches") + > ??? ggtitle("Scatterplot of Launches vs. Deliveries") > > > Use the smoothing function of your choice, I left it with the default > loess. > > Hope this helps, > > Rui Barradas > > > On 24/08/2018 03:38, Jeff Reichman wrote: >> R-help >> >> >> I want to add two smooth lines (geom_smooth()) for each scatter plot. >> How >> do I do that? >> >> >> ggplot() + >> >> ?? geom_point(data=data, aes(x=timeline, y=deliveries), color="blue") + >> >> ?? geom_point(data=data, aes(x=timeline, y=launches), color="red") + >> >> ?? xlab("Deliveries") + >> >> ?? ylab("Launches") + >> >> ?? ggtitle("Scatterplot of Launches vs. Deliveries") >> >> >> Jeff >> >> >> ????[[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. >> > > --- > This email has been checked for viruses by AVG. > https://www.avg.com > > ______________________________________________ > 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.
Got it thank you From: Riley Finn <rileyfinn3 at gmail.com> Sent: Thursday, August 23, 2018 10:24 PM To: reichmanj at sbcglobal.net Cc: R-help at r-project.org Subject: Re: [R] How to add a geom_smooth() line Jeff, You need to reshape your data frame. If you use ggplot, you will often have to present your data in "long format" Use the reshape2 package. I made a sample data frame because you didn't provide one. I also change your x and y labels because they made no sense. data <- data.frame( timeline = 1:10, launches = sample(10:20, 10), deliveries = sample(10:20, 10) ) library(reshape2) dataNew <- melt(data = data, id.vars = 'timeline', variable.name <http://variable.name> = 'launchOrDelivery') ggplot(data=dataNew, aes(x=timeline, y=value), color= launchOrDelivery) + geom_point(aes(color= launchOrDelivery)) + geom_smooth(aes(group = launchOrDelivery, color= launchOrDelivery), se = FALSE) + xlab("timeline") + ylab("Launches/Deliveries") + ggtitle("Scatterplot of Launches vs. Deliveries") On Thu, Aug 23, 2018 at 9:39 PM Jeff Reichman <reichmanj at sbcglobalnet <mailto:reichmanj at sbcglobal.net> > wrote: R-help I want to add two smooth lines (geom_smooth()) for each scatter plot. How do I do that? ggplot() + geom_point(data=data, aes(x=timeline, y=deliveries), color="blue") + geom_point(data=data, aes(x=timeline, y=launches), color="red") + xlab("Deliveries") + ylab("Launches") + ggtitle("Scatterplot of Launches vs. Deliveries") Jeff [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org <mailto: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]]