# Multiple Chart Types in R

How to design figures with multiple chart types in Plotly for R.

New to Plotly?

Plotly is a free and open-source graphing library for R. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.

## Multiple Chart Types in R

How to design figures with multiple chart types in R.

### Chart Types versus Trace Types

Plotly's figure data structure supports defining subplots of various types (e.g. cartesian, polar, 3-dimensional, maps etc) with attached traces of various compatible types (e.g. scatter, bar, choropleth, surface etc). This means that Plotly figures are not constrained to representing a fixed set of "chart types" such as scatter plots only or bar charts only or line charts only: any subplot can contain multiple traces of different types.

### Multiple Trace Types with Plotly

Figures produced with Plotly have the add_trace() method, so it is easy to start with a Plotly figure containing only traces of a given type, and add traces of another type.

library(plotly)
data <- data.frame(
Fruits = c ("apples", "bananas", "oranges"),
Line = c(1,3,2),
Bar = c(2,1,3))

fig <- plot_ly(data , x = ~Fruits, y = ~Bar, type = 'bar', name = 'Last Year') %>%
add_trace(data , x = ~Fruits, y = ~Line, type = 'scatter',  mode = 'lines', name = 'This year')

fig <- fig %>% layout(yaxis = list(title = "Amount"))
fig <- fig %>% layout(legend=list(title=list(text='<b> Time Period </b>')))
fig


#### Line Chart and a Bar Chart

library(plotly)
data <- data.frame(
X = c (0, 1, 2, 3, 4, 5),
Line = c(1.5, 1, 1.3, 0.7, 0.8, 0.9),
Bar = c(1, 0.5, 0.7, -1.2, 0.3, 0.4))

fig <- plot_ly(data , x = ~X, y = ~Bar, type = 'bar') %>%
add_trace(data , x = ~X, y = ~Line, type = 'scatter',  mode = 'lines+markers')

fig


#### A Contour and Scatter Plot of the Method of Steepest Descent

library(plotly)
library(jsonlite)
urlfile<-'https://raw.githubusercontent.com/plotly/datasets/master/steepest.json'
data<-fromJSON(url(urlfile))
X <- data[["contour_x"]][,]
Y <- data[["contour_y"]][,]
Z <- data[["contour_z"]][,,]
fig <- plot_ly() %>%
add_trace(x = X, y= Y, z = Z, type = "contour") %>%
hide_colorbar()%>% layout(showlegend = FALSE) %>%
add_trace(x = data$trace_x, y = data$trace_y, type = "scatter",
mode = "lines+markers", name = 'steepest', inherit =  FALSE,
marker = list(color = 'black'), line = list(color = 'black'))
fig


#### Reference

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