# Draw a trend line using ggplot-Quick Guide

**Methods – finnstats**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Draw a trend line using ggplot, Technical analysts utilize the trendline as one of their most significant tools. A trendline aids technical analysts in determining the data set direction.

Let’s start by making a data frame.

data <- data.frame(x=c(1, 2, 5, 3, 5, 5, 9,10,12), y=c(18, 10, 10, 20, 22, 13, 15,16,17)) data x y 1 1 18 2 2 10 3 5 10 4 3 20 5 5 22 6 5 13 7 9 15 8 10 16 9 12 17

## Approach1: Add Linear Trend Line

In ggplot2, the following code demonstrates how to add a linear trend line to a scatterplot.

How to Plot Categorical Data in R-Quick Guide »

library(ggplot2) ggplot(data, aes(x=x, y=y)) + geom_point() + geom_smooth(method=lm)

## Approach 2: Add Linear Trend Line & Confidence Region

The level option can be used to determine the level of confidence to use for the shaded confidence region in the plot.

Dot Plots in R-Strip Charts for Small Sample Size »

The default level of confidence is 0.95. Our shaded confidence region on the plot became much bigger when we specified a confidence level of 0.99.

ggplot(data, aes(x=x, y=y)) + geom_point() + geom_smooth(method=lm, level=0.99)

## Approach 3: Add Linear Trend Line & No-Confidence Region

We can use the se=FALSE argument to hide the shaded confidence region around the trend line

Adding text labels to ggplot2 Bar Chart »

ggplot(data, aes(x=x, y=y)) + geom_point() + geom_smooth(method=lm, se=FALSE, col='blue', size=2)

## Approach 4: Curved loess Trend Line

If we don’t specify a technique for geom smooth(), we’ll get a curved loess line by default

ggplot(data, aes(x=x, y=y)) + geom_point() + geom_smooth()

Note: Traders can utilize trendlines to identify probable locations of support and resistance, which can assist assess the possibility of the trend continuing.

How to Change Background Color in ggplot2 »

The post Draw a trend line using ggplot-Quick Guide appeared first on finnstats.

**leave a comment**for the author, please follow the link and comment on their blog:

**Methods – finnstats**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.