Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plotly bar and line chart

Tags:

r

plotly

I want to plot a bar together with a line chart in R with plotly.

My first attempt was

p <- plot_ly(
  x = c(1,2,3,4,5),
  y = c(1,2,1.5,3,2),
  type='scatter',
  mode='lines',
  line = list(color = 'black')
)
add_trace(
  p,
  x = c(1,2,3,4,5),
  y = c(0.5,0.7,0.6,0.9,0.8),
  type='bar',
  marker = list(color = 'red')
)

The result is right, but I get the following warning:

Warning message: The following attributes don't exist: 'mode', 'line'

I guess cause the bar plot in add_trace() cannot handle the line and mode parameter from the plot_ly() function. So I changed the order:

p <- plot_ly(
  x = c(1,2,3,4,5),
  y = c(0.5,0.7,0.6,0.9,0.8),
  type='bar',
  marker = list(color = 'red')
)
add_trace(
  p,
  x = c(1,2,3,4,5),
  y = c(1,2,1.5,3,2),
  type='scatter',
  mode='lines',
  line = list(color = 'black')
)

This time I get the following message and red markers are displayed on the black line chart.

A marker object has been specified, but markers is not in the mode Adding markers to the mode...

How can I fix this? (I'm using the R package plotly 4.1.0)

like image 840
elcombato Avatar asked Aug 02 '16 13:08

elcombato


People also ask

How do you create a combo chart in python?

Pandas use plot() method to create charts. Under the hood, it calls Matplotlib's API to create the chart by default (Note: the backend defaults to Matplotlib). The trick is to set the argument secondary_y to True to allow the 2nd chart to be plotted on the secondary y-axis. )# Plot the second x and y axes.

What three types of bar charts are available when using plotly?

Examples of grouped, stacked, overlaid, and colored bar charts. New to Plotly? Plotly is a free and open-source graphing library for R.


1 Answers

I'm running plotly 4.0.1, but if I add mode='lines+markers' instead of just mode='lines' the error message goes away for me.

--edit to add full code--

For the lazy (like me), here's the full code that worked on my end:

p <- plot_ly(x = c(1,2,3,4,5),
             y = c(0.5,0.7,0.6,0.9,0.8),
             type='bar',
             marker = list(color = 'red', opacity=0)
     )

add_trace(p,
          x = c(1,2,3,4,5),
          y = c(1,2,1.5,3,2),
          type='scatter',
          mode='lines+markers',
          line = list(color = 'black')
     )
like image 112
Whsky Steve Avatar answered Sep 23 '22 00:09

Whsky Steve