Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly does not show lines

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!

like image 736
Richard Avatar asked Jun 16 '18 16:06

Richard


People also ask

How do you show figures in plotly?

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.

How do you plot multiple lines in plotly?

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.

What is trace in plotly?

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.


2 Answers

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

like image 167
Vicky Hughes Avatar answered Sep 25 '22 14:09

Vicky Hughes


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"))
like image 29
Ameya Avatar answered Sep 23 '22 14:09

Ameya