简体   繁体   中英

R plotly fill color, font, and legend location

elw <- structure(list(year = 1975:1979, x10006 = c(0L, 0L, 0L, 0L, 0L),
   x12018 = c(285.4309, 265.1403, 369.1682, 604.1203, 587.2926
), x19000 = c(26.48335, 36.45504, 37.28563, 126.8903, 182.8447
), x20000 = c(229.9651, 369.8476, 496.058, 504.2717, 445.3687
), x99999 = c(1707.498, 2223.986, 2599.086, 2661.213, 3207.065
)), .Names = c("year", "x10006", "x12018", "x19000", "x20000", 
"x99999"), row.names = c(NA, -5L), class = "data.frame") 

elw_stack <- structure(list(year = 1975:1979, x10006 = c(0L, 0L, 0L, 0L, 0L), 
    x12018 = c(285L, 265L, 369L, 604L, 587L), 
    x19000 = c(312L, 302L, 406L, 731L, 770L), 
    x20000 = c(542L, 671L, 903L, 1235L, 1216L), 
    x99999 = c(2249L, 2895L, 3502L, 3896L, 4423L)), 
    .Names = c("year", "x10006", "x12018", "x19000", "x20000", "x99999"), 
    row.names = c(NA, -5L), class = "data.frame")

With the data aboce, I have generated a plotly chart in R using the following code:

install.packages("plotly")
library(plotly)

plot_ly(data = elw_stack, x = ~year, y = ~x10006, fill="tonexty", mode="lines",
        text = round(elw$x10006, 0), hoverinfo='x+text+name', name="x10006") %>%
  add_trace(y=~x12018, mode="lines", type = "scatter", text = round(elw$x12018,0),
            hoverinfo='x+text+name', name="x12018") %>%
  add_trace(y=~x19000, mode="lines", text=round(elw$x19000,0), 
            hoverinfo='x+text+name', name="x19000") %>%
  add_trace(y=~x20000, mode="lines", text=round(elw$x20000,0), 
            hoverinfo='x+text+name', name="x20000") %>%
  add_trace(y=~x99999, mode="lines", text=round(elw$x99999,0), 
            hoverinfo='x+text+name', name="x99999") %>%
  layout(yaxis=list(title="Y axis label"))

I am still struggling to do three things, though:

  1. Change the fill color to specific RGB-code colors of my choosing. (I attempted the plotly command fillcolor but am not sure if I'm implementing it correctly.)
  2. Change the font style of all the text (specifically to Gotham Narrow).
  3. Lower the legend so that it's not positioned at the top of the chart.

Any help would be greatly appreciated. Thanks!

Here is an example that does what you want. I just used the font "Times":

clrs <- c('#66c2a5','#fc8d62','#8da0cb','#e78ac3')

plot_ly(data = elw_stack, x = ~year, y = ~x10006, fill="tonexty", mode="lines",
        text = round(elw$x10006, 0), hoverinfo='x+text+name', name="x10006", type = "scatter") %>%
  add_trace(y=~x12018, text = round(elw$x12018, 0), name="x12018", fillcolor = clrs[1]) %>%
  add_trace(y=~x19000, text = round(elw$x19000, 0), name="x19000", fillcolor = clrs[2]) %>%
  add_trace(y=~x20000, text = round(elw$x20000, 0), name="x20000", fillcolor = clrs[3]) %>%
  add_trace(y=~x99999, text = round(elw$x99999, 0), name="x99999", fillcolor = clrs[4]) %>%
  layout(yaxis = list(title="Y axis label"),
         legend = list(x = 1, y = 0),
         font = list(family = "Times"))

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM