R用曲线填充颜色,字体和图例位置

时间:2016-02-26 15:33:04

标签: r plotly

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")

根据数据,我使用以下代码在R中生成了一个图表:

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"))

我仍然在努力做三件事:

  1. 将填充颜色更改为我选择的特定RGB代码颜色。 (我试图使用plotly命令fillcolor,但我不确定我是否正确实现它。)
  2. 更改所有文字的字体样式(特别是Gotham Narrow)。
  3. 降低图例,使其不位于图表的顶部。
  4. 非常感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分:0)

这是一个做你想要的例子。我刚刚使用了字体&#34; Times&#34;:

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"))

enter image description here