gpt4 book ai didi

R plotly 填充颜色、字体和图例位置

转载 作者:行者123 更新时间:2023-12-02 03:17:36 25 4
gpt4 key购买 nike

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

enter image description here

关于R plotly 填充颜色、字体和图例位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35655712/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com