I am using plotly to visualize a dataset into an interactive plot that can be used in a shiny application in a dashboard. I'm using the plotly package as it seems very suitable for me to do something like this. I want to create a plot with lines and markers. When trying to do with a trace using the code below I only get markers and no lines in between
plot_ly(cube_eduexpgdp, x = ~year) %>%
add_trace(y = ~expenditure_gdp, mode = "lines+markers",
color = ~country_name, line = list(shape = "linear")) %>%
layout(title = "Government expenditures as percentage of GDP",
yaxis = list(title = "Expenditures (%)"),
xaxis = list(title = "Year"))
This tutorial from plotly suggests to do it with the following code. When I use this code I get an empty canvas that does show the grouped datapoints' values on a tooltip when I hover over it.
plot_ly(cube_eduexpgdp, x = ~year, y = ~expenditure_gdp,
color = ~country_name) %>%
add_lines() `
Is there anyone that could help me? Would be very much appreciated!
To display a figure using the renderers framework, you call the . show() method on a graph object figure, or pass the figure to the plotly. io. show function.
To create multiple line charts on the same plot with plotly graph objects, all you have to do is add another trace to the plot. If you look closely, you can see that I have also added the name parameter to add the labels for the legend and also explicitly added the mode='lines' to tell plotly that i want a line chart.
A plotly. graph_objects. Image trace is a graph object in the figure's data list with any of the named arguments or attributes listed below. Display an image, i.e. data on a 2D regular raster. By default, when an image is displayed in a subplot, its y axis will be reversed (ie.
I had the same error and found it was caused by trying to plot a grouped dataframe, so had to ungroup()
first. As found here: R - plotly invisible lines
Without a reproducible example, I can only guess here, but it is probably due to inheritance and/or plotly
defaults. Does the code below help?
plot_ly(cube_eduexpgdp, x = ~year, type = 'scatter', mode = 'markers') %>%
add_trace(y = cube_eduexpgdp[['expenditure_gdp']], mode = "lines+markers",
color = cube_eduexpgdp[['country_name']], line = list(shape = "linear"), inherit = FALSE) %>%
layout(title = "Government expenditures as percentage of GDP",
yaxis = list(title = "Expenditures (%)"),
xaxis = list(title = "Year"))
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