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)
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.
Examples of grouped, stacked, overlaid, and colored bar charts. New to Plotly? Plotly is a free and open-source graphing library for R.
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')
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With