2

I am trying to make a stacked area chart in R exactly like this ggplot2 one (below) only using plotly. enter image description here

Here is a link to my data.

To generate a plotly version of the above ggplot2 chart, I first have to add the values of each column in my dataframe, elw, on top of the values in the previous column like so. This is because plotly (as far as I'm aware) does not have the ability to automatically stack values in area charts.

With this new stacked data set, elw_stack, I use the following code to make my plotly chart:

el_plot2 = ggplot() +
  geom_area(aes(elw_stack$year, elw_stack$x99999, fill = 'green')) +
  geom_area(aes(elw_stack$year, elw_stack$x20000, fill = 'red')) +
  geom_area(aes(elw_stack$year, elw_stack$x19000, fill = 'blue')) +
  geom_area(aes(elw_stack$year, elw_stack$x12018, fill = 'purple')) +
  geom_area(aes(elw_stack$year, elw_stack$x10006, fill = 'yellow'))

ggplotly(el_plot2)

That code generates this chart: enter image description here

The issue is that the plotly labels refer to the cumulative elw_stack values. The green value pictured at year 1999 is actually ~3700 (i.e. 11,365 - 7957). But the description bar says the cumulative value of 11,365. Is there a way to fix this so that the labels aren't cumulative values?

Community
  • 1
  • 1
Jim
  • 715
  • 2
  • 13
  • 26
  • can you include your dataset in your post? – MLavoie Feb 12 '16 at 20:53
  • use `p.plotly <- plotly_build(el_plot2)`, this gives you a list with plot details and then change the value manually. use grep to figure where the value is – infominer Feb 12 '16 at 20:54
  • @MLavoie Yes, I included a link to my data near the top of the post. – Jim Feb 12 '16 at 21:11
  • @infominer Can you provide more information? Entering ?plotly_build does not provide much information about that function. – Jim Feb 12 '16 at 22:09

1 Answers1

3

I was having a similar problem and eventually decided not to use ggplotly, but instead i used the plot_ly function. Here is the code I used with your data:

elw <- read.csv("elw.csv")
elw_stack <- read.csv("elw_stack.csv")

plot <- plot_ly(data=elw_stack, x=year, y=x10006, fill="tonexty", mode="lines",
                text=round(elw$x10006, 0), hoverinfo='x+text+name', name="x10006")

plot <- add_trace(plot, data=elw_stack, x=year, y=x12018, fill="tonexty", mode="lines",
                  text=round(elw$x12018,0), hoverinfo='x+text+name', name="x12018")

plot <- add_trace(plot, data=elw_stack, x=year, y=x19000, fill="tonexty", mode="lines",
                  text=round(elw$x19000,0), hoverinfo='x+text+name', name="x19000")

plot <- add_trace(plot, data=elw_stack, x=year, y=x20000, fill="tonexty", mode="lines",
                  text=round(elw$x20000,0), hoverinfo='x+text+name', name="x20000")

plot <- add_trace(plot, data=elw_stack, x=year, y=x99999, fill="tonexty", mode="lines",
                  text=round(elw$x99999,0), hoverinfo='x+text+name', name="x99999")

plot <- layout(plot, yaxis=list(title="Whatever title you wanna use"))

And this is how the final plot looks:

plotly image

What I can't get to work is to add the different traces using a for loop. I wanted to write a function that takes a data frame with an arbitrary number of columns as input and returns the stacked area plot, but for some reason the plot won't show all the traces (only first and last)

Hope it helps...

  • This looks great! Thanks so much. Two things I'm still struggling with, though: Do you know how to edit the a) fill color and b) font? – Jim Feb 26 '16 at 08:59
  • (a) To change the fill color, you actually need to change the line color. Here is an example on how you can do that: `plot <- plot_ly(data=elw_stack, x=year, y=x10006, fill="tonexty", mode="lines", text=round(elw$x10006, 0), hoverinfo='x+text+name', name="x10006", line=list(color=rgb(0,0,1)))` (b) I think it depends on what font you actually want to change (axis font, legend font, etc), but it can be controlled globally like this: `plot <- layout(plot, yaxis=list(title="Whatever title you wanna use"), font=list(family="Times New Roman"))` – Carlos Celada Feb 29 '16 at 16:55
  • You can find more info on the [plotly R chart attribute reference](https://plot.ly/r/reference/)... Hope it helps. – Carlos Celada Feb 29 '16 at 16:57
  • @ Thanks! A few more questions I couldn't find in the attribute reference: 1. Is there a way to lengthen the space on the hover text labels? My hover label cuts off after 12 characters. (If not, is there a way to have abbreviated hover labels but keep the legend the same full-length text? 2. My hover labels freak out for x10006 when there are values of 0 and display dozens of numbers. How can I fix this? (I suppose I could likely just rearrange the stack order of the layers, but I'd like to know why this is occurring). 3. Is there a way to recognize the "Railway" font? – Jim Mar 02 '16 at 18:39
  • Thanks! Two more questions I couldn't find in the attribute reference: 1. Is there a way to lengthen the space on the hover text labels? My hover label cuts off after 12 characters. (If not, is there a way to have abbreviated hover labels but keep the legend the same full-length text? 2. My hover labels seem to freak out for x10006 when there are values of 0 and display dozens of numbers. How can I fix this? (I suppose I could likely just rearrange the stack order of the layers, but I'd like to know why this is occurring). – Jim Mar 02 '16 at 19:36