为R中的直方图分配颜色
Assign color to histogram in R
我的数据框看起来像:
Global.df.Selected <- structure(list(Year.of.Issuance = c(
2014L, 2017L, 2017L, 2017L,
2016L, 2017L
), Region.of.Risk = c(
"North America", "Europe",
"Europe", "Europe", "North America", "North America"
), color = c(
"#2874A6",
"#5DADE2", "#5DADE2", "#5DADE2", "#2874A6", "#2874A6"
)), row.names = c(
NA,
-6L
), class = "data.frame")
我想创建一个直方图并分配颜色(已经基于风险区域)。
我做了以下事情:
plotly::plot_ly(Global.df, x = ~Year.of.Issuance, color = ~Region.of.Risk) %>%
plotly::add_histogram() %>%
plotly::layout(yaxis = list(title="Y axis label"),
legend = list(
orientation = "h",
# show entries horizontally
xanchor = "center",
# use center of legend as anchor
x = 0.5,
y = -0.15
))
我怎样才能精确地使用 Global.df.Selected$color
向量
上的颜色
实现此目的的一种方法是使用命名的颜色向量,它可以通过参数 colors
传递给 ploly。试试这个:
library(plotly)
# Make named color vector
colors <- Global.df.Selected %>%
select(Region.of.Risk, color) %>%
distinct() %>%
tibble::deframe()
plotly::plot_ly(Global.df.Selected, x = ~Year.of.Issuance, color = ~Region.of.Risk, colors = colors) %>%
plotly::add_histogram() %>%
plotly::layout(
yaxis = list(title = "Y axis label"),
legend = list(
orientation = "h",
# show entries horizontally
xanchor = "center",
# use center of legend as anchor
x = 0.5,
y = -0.15
)
)
我的数据框看起来像:
Global.df.Selected <- structure(list(Year.of.Issuance = c(
2014L, 2017L, 2017L, 2017L,
2016L, 2017L
), Region.of.Risk = c(
"North America", "Europe",
"Europe", "Europe", "North America", "North America"
), color = c(
"#2874A6",
"#5DADE2", "#5DADE2", "#5DADE2", "#2874A6", "#2874A6"
)), row.names = c(
NA,
-6L
), class = "data.frame")
我想创建一个直方图并分配颜色(已经基于风险区域)。 我做了以下事情:
plotly::plot_ly(Global.df, x = ~Year.of.Issuance, color = ~Region.of.Risk) %>%
plotly::add_histogram() %>%
plotly::layout(yaxis = list(title="Y axis label"),
legend = list(
orientation = "h",
# show entries horizontally
xanchor = "center",
# use center of legend as anchor
x = 0.5,
y = -0.15
))
我怎样才能精确地使用 Global.df.Selected$color
向量
实现此目的的一种方法是使用命名的颜色向量,它可以通过参数 colors
传递给 ploly。试试这个:
library(plotly)
# Make named color vector
colors <- Global.df.Selected %>%
select(Region.of.Risk, color) %>%
distinct() %>%
tibble::deframe()
plotly::plot_ly(Global.df.Selected, x = ~Year.of.Issuance, color = ~Region.of.Risk, colors = colors) %>%
plotly::add_histogram() %>%
plotly::layout(
yaxis = list(title = "Y axis label"),
legend = list(
orientation = "h",
# show entries horizontally
xanchor = "center",
# use center of legend as anchor
x = 0.5,
y = -0.15
)
)