Line Plots in R
How to create line aplots in R. Examples of basic and advanced line plots, time series line plots, colored charts, and density plots.
Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Sign up for early access now.
Basic Line Plot
library(plotly)
x <- c(1:100)
random_y <- rnorm(100, mean = 0)
data <- data.frame(x, random_y)
fig <- plot_ly(data, x = ~x, y = ~random_y, type = 'scatter', mode = 'lines')
fig
Click to copy
Line Plots Mode
library(plotly)
trace_0 <- rnorm(100, mean = 5)
trace_1 <- rnorm(100, mean = 0)
trace_2 <- rnorm(100, mean = -5)
x <- c(1:100)
data <- data.frame(x, trace_0, trace_1, trace_2)
fig <- plot_ly(data, x = ~x)
fig <- fig %>% add_trace(y = ~trace_0, name = 'trace 0',mode = 'lines')
fig <- fig %>% add_trace(y = ~trace_1, name = 'trace 1', mode = 'lines+markers')
fig <- fig %>% add_trace(y = ~trace_2, name = 'trace 2', mode = 'markers')
Click to copy
It is also possible to pass the first trace in the plot_ly function. In such cases, the type of graph has to be specified, as shown below:
library(plotly)
trace_0 <- rnorm(100, mean = 5)
trace_1 <- rnorm(100, mean = 0)
trace_2 <- rnorm(100, mean = -5)
x <- c(1:100)
data <- data.frame(x, trace_0, trace_1, trace_2)
fig <- plot_ly(data, x = ~x, y = ~trace_0, name = 'trace 0', type = 'scatter', mode = 'lines')
fig <- fig %>% add_trace(y = ~trace_1, name = 'trace 1', mode = 'lines+markers')
fig <- fig %>% add_trace(y = ~trace_2, name = 'trace 2', mode = 'markers')
fig
Click to copy
Style Line Plots
library(plotly)
month <- c('January', 'February', 'March', 'April', 'May', 'June', 'July',
'August', 'September', 'October', 'November', 'December')
high_2000 <- c(32.5, 37.6, 49.9, 53.0, 69.1, 75.4, 76.5, 76.6, 70.7, 60.6, 45.1, 29.3)
low_2000 <- c(13.8, 22.3, 32.5, 37.2, 49.9, 56.1, 57.7, 58.3, 51.2, 42.8, 31.6, 15.9)
high_2007 <- c(36.5, 26.6, 43.6, 52.3, 71.5, 81.4, 80.5, 82.2, 76.0, 67.3, 46.1, 35.0)
low_2007 <- c(23.6, 14.0, 27.0, 36.8, 47.6, 57.7, 58.9, 61.2, 53.3, 48.5, 31.0, 23.6)
high_2014 <- c(28.8, 28.5, 37.0, 56.8, 69.7, 79.7, 78.5, 77.8, 74.1, 62.6, 45.3, 39.9)
low_2014 <- c(12.7, 14.3, 18.6, 35.5, 49.9, 58.0, 60.0, 58.6, 51.7, 45.2, 32.2, 29.1)
data <- data.frame(month, high_2000, low_2000, high_2007, low_2007, high_2014, low_2014)
#The default order will be alphabetized unless specified as below:
data$month <- factor(data$month, levels = data[["month"]])
fig <- plot_ly(data, x = ~month, y = ~high_2014, name = 'High 2014', type = 'scatter', mode = 'lines',
line = list(color = 'rgb(205, 12, 24)', width = 4))
fig <- fig %>% add_trace(y = ~low_2014, name = 'Low 2014', line = list(color = 'rgb(22, 96, 167)', width = 4))
fig <- fig %>% add_trace(y = ~high_2007, name = 'High 2007', line = list(color = 'rgb(205, 12, 24)', width = 4, dash = 'dash'))
fig <- fig %>% add_trace(y = ~low_2007, name = 'Low 2007', line = list(color = 'rgb(22, 96, 167)', width = 4, dash = 'dash'))
fig <- fig %>% add_trace(y = ~high_2000, name = 'High 2000', line = list(color = 'rgb(205, 12, 24)', width = 4, dash = 'dot'))
fig <- fig %>% add_trace(y = ~low_2000, name = 'Low 2000', line = list(color = 'rgb(22, 96, 167)', width = 4, dash = 'dot'))
fig <- fig %>% layout(title = "Average High and Low Temperatures in New York",
xaxis = list(title = "Months"),
yaxis = list (title = "Temperature (degrees F)"))
fig
Click to copy
Mapping data to linetype
library(plotly)
library(plyr)
tg <- ddply(ToothGrowth, c("supp", "dose"), summarise, length=mean(len))
fig <- plot_ly(tg, x = ~dose, y = ~length, type = 'scatter', mode = 'lines', linetype = ~supp, color = I('black'))
fig <- fig %>% layout(title = 'The Effect of Vitamin C on Tooth Growth in Guinea Pigs by Supplement Type',
xaxis = list(title = 'Dose in milligrams/day'),
yaxis = list (title = 'Tooth length'))
fig
Click to copy
Connect Data Gaps
library(plotly)
x <- c(1:15)
y <- c(10, 20, NA, 15, 10, 5, 15, NA, 20, 10, 10, 15, 25, 20, 10)
data <- data.frame(x, y)
fig <- plot_ly(data, x = ~x, y = ~y, name = "Gaps", type = 'scatter', mode = 'lines')
fig <- fig %>% add_trace(y = ~y - 5, name = "<b>No</b> Gaps", connectgaps = TRUE)
fig
Click to copy
Line Interpolation Options
library(plotly)
x <- c(1:5)
y <- c(1, 3, 2, 3, 1)
fig <- plot_ly(x = ~x)
fig <- fig %>% add_lines(y = ~y, name = "linear", line = list(shape = "linear"))
fig <- fig %>% add_lines(y = y + 5, name = "spline", line = list(shape = "spline"))
fig <- fig %>% add_lines(y = y + 10, name = "vhv", line = list(shape = "vhv"))
fig <- fig %>% add_lines(y = y + 15, name = "hvh", line = list(shape = "hvh"))
fig <- fig %>% add_lines(y = y + 20, name = "vh", line = list(shape = "vh"))
fig <- fig %>% add_lines(y = y + 25, name = "hv", line = list(shape = "hv"))
fig
Click to copy
Label Lines with Annotations
library(plotly)
x <- c(2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013)
y_television <- c(74, 82, 80, 74, 73, 72, 74, 70, 70, 66, 66, 69)
y_internet <- c(13, 14, 20, 24, 20, 24, 24, 40, 35, 41, 43, 50)
data <- data.frame(x, y_television, y_internet)
xaxis <- list(title = "",
showline = TRUE,
showgrid = FALSE,
showticklabels = TRUE,
linecolor = 'rgb(204, 204, 204)',
linewidth = 2,
autotick = FALSE,
ticks = 'outside',
tickcolor = 'rgb(204, 204, 204)',
tickwidth = 2,
ticklen = 5,
tickfont = list(family = 'Arial',
size = 12,
color = 'rgb(82, 82, 82)'))
yaxis <- list(title = "",
showgrid = FALSE,
zeroline = FALSE,
showline = FALSE,
showticklabels = FALSE)
margin <- list(autoexpand = FALSE,
l = 100,
r = 100,
t = 110)
# Build the annotations
television_1 <- list(
xref = 'paper',
yref = 'y',
x = 0.05,
y = y_television[1],
xanchor = 'right',
yanchor = 'middle',
text = ~paste('Television ', y_television[1], '%'),
font = list(family = 'Arial',
size = 16,
color = 'rgba(67,67,67,1)'),
showarrow = FALSE)
internet_1 <- list(
xref = 'paper',
yref = 'y',
x = 0.05,
y = y_internet[1],
xanchor = 'right',
yanchor = 'middle',
text = ~paste('Internet ', y_internet[1], '%'),
font = list(family = 'Arial',
size = 16,
color = 'rgba(49,130,189, 1)'),
showarrow = FALSE)
television_2 <- list(
xref = 'paper',
x = 0.95,
y = y_television[12],
xanchor = 'left',
yanchor = 'middle',
text = paste('Television ', y_television[12], '%'),
font = list(family = 'Arial',
size = 16,
color = 'rgba(67,67,67,1)'),
showarrow = FALSE)
internet_2 <- list(
xref = 'paper',
x = 0.95,
y = y_internet[12],
xanchor = 'left',
yanchor = 'middle',
text = paste('Internet ', y_internet[12], '%'),
font = list(family = 'Arial',
size = 16,
color = 'rgba(67,67,67,1)'),
showarrow = FALSE)
fig <- plot_ly(data, x = ~x)
fig <- fig %>% add_trace(y = ~y_television, type = 'scatter', mode = 'lines', line = list(color = 'rgba(67,67,67,1)', width = 2))
fig <- fig %>% add_trace(y = ~y_internet, type = 'scatter', mode = 'lines', line = list(color = 'rgba(49,130,189, 1)', width = 4))
fig <- fig %>% add_trace(x = ~c(x[1], x[12]), y = ~c(y_television[1], y_television[12]), type = 'scatter', mode = 'markers', marker = list(color = 'rgba(67,67,67,1)', size = 8))
fig <- fig %>% add_trace(x = ~c(x[1], x[12]), y = ~c(y_internet[1], y_internet[12]), type = 'scatter', mode = 'markers', marker = list(color = 'rgba(49,130,189, 1)', size = 12))
fig <- fig %>% layout(title = "Main Source for News", xaxis = xaxis, yaxis = yaxis, margin = margin,
autosize = FALSE,
showlegend = FALSE,
annotations = television_1)
fig <- fig %>% layout(annotations = internet_1)
fig <- fig %>% layout(annotations = television_2)
fig <- fig %>% layout(annotations = internet_2)
fig
Click to copy
Filled Lines
library(plotly)
month <- c('January', 'February', 'March', 'April', 'May', 'June', 'July',
'August', 'September', 'October', 'November', 'December')
high_2014 <- c(28.8, 28.5, 37.0, 56.8, 69.7, 79.7, 78.5, 77.8, 74.1, 62.6, 45.3, 39.9)
low_2014 <- c(12.7, 14.3, 18.6, 35.5, 49.9, 58.0, 60.0, 58.6, 51.7, 45.2, 32.2, 29.1)
data <- data.frame(month, high_2014, low_2014)
data$average_2014 <- rowMeans(data[,c("high_2014", "low_2014")])
#The default order will be alphabetized unless specified as below:
data$month <- factor(data$month, levels = data[["month"]])
fig <- plot_ly(data, x = ~month, y = ~high_2014, type = 'scatter', mode = 'lines',
line = list(color = 'transparent'),
showlegend = FALSE, name = 'High 2014')
fig <- fig %>% add_trace(y = ~low_2014, type = 'scatter', mode = 'lines',
fill = 'tonexty', fillcolor='rgba(0,100,80,0.2)', line = list(color = 'transparent'),
showlegend = FALSE, name = 'Low 2014')
fig <- fig %>% add_trace(x = ~month, y = ~average_2014, type = 'scatter', mode = 'lines',
line = list(color='rgb(0,100,80)'),
name = 'Average')
fig <- fig %>% layout(title = "Average, High and Low Temperatures in New York",
paper_bgcolor='rgb(255,255,255)', plot_bgcolor='rgb(229,229,229)',
xaxis = list(title = "Months",
gridcolor = 'rgb(255,255,255)',
showgrid = TRUE,
showline = FALSE,
showticklabels = TRUE,
tickcolor = 'rgb(127,127,127)',
ticks = 'outside',
zeroline = FALSE),
yaxis = list(title = "Temperature (degrees F)",
gridcolor = 'rgb(255,255,255)',
showgrid = TRUE,
showline = FALSE,
showticklabels = TRUE,
tickcolor = 'rgb(127,127,127)',
ticks = 'outside',
zeroline = FALSE))
fig
Click to copy
See more examples of filled line charts here.
Density Plot
library(plotly)
dens <- with(diamonds, tapply(price, INDEX = cut, density))
df <- data.frame(
x = unlist(lapply(dens, "[[", "x")),
y = unlist(lapply(dens, "[[", "y")),
cut = rep(names(dens), each = length(dens[[1]]$x))
)
fig <- plot_ly(df, x = ~x, y = ~y, color = ~cut)
fig <- fig %>% add_lines()
fig
Click to copy
Reference
See https://plotly.com/r/reference/#scatter for more information and chart attribute options!
What About Dash?
Dash for R is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.
Learn about how to install Dash for R at https://dashr.plot.ly/installation.
Everywhere in this page that you see fig
, you can display the same figure in a Dash for R application by passing it to the figure
argument of the Graph
component from the built-in dashCoreComponents
package like this:
library(plotly)
fig <- plot_ly()
# fig <- fig %>% add_trace( ... )
# fig <- fig %>% layout( ... )
library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)
app <- Dash$new()
app$layout(
htmlDiv(
list(
dccGraph(figure=fig)
)
)
)
app$run_server(debug=TRUE, dev_tools_hot_reload=FALSE)
Click to copy