[R] How to add a geom_smooth() line

Riley Finn r||ey||nn3 @end|ng |rom gm@||@com
Fri Aug 24 05:23:40 CEST 2018


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 using 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 using 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