繁体   English   中英

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

[英]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")

有了数据,我使用以下代码在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. 降低图例,使其不位于图表顶部。

任何帮助将不胜感激。 谢谢!

这是一个做你想要的例子。 我只使用了字体“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"))

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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