如何将文本标签添加到 R plotly 聚合图表?
How to add text label to R plotly aggregate chart?
我绘制了一个汇总图表来显示平均值。我想知道如何向图表添加文本标签以说明图表上显示的平均值。
下面是基于我的代码的代表。在这段代码中,我制作了一个图表来展示平均降雪量和降雨量。我已经在图表中添加了文本标签,但它们对应于图表上的每个数据点(不是平均值,这是我所追求的)。谢谢你的建议。
当前代码:
my_tibble <- tibble(precipitation = as.numeric(runif(10,3,10)),
week = c(1,2,3,4,5,1,2,3,4,5),
fall_type = c("rain", "snow", "snow", "rain", "rain",
"rain", "rain", "snow", "snow", "snow"),
region = c("south", "south", "south", "south", "south",
"north", "north", "north", "north", "north"))
my_tibble <- my_tibble %>%
mutate(chart_cat = paste(region, fall_type, sep = ","))
make_a_chart <- function(location){
my_tibble %>%
filter(region==location) %>%
plot_ly() %>%
layout(
yaxis = list(title = none),
xaxis = list(title = none,
tickformat = ","),
barmode = 'stack',
showlegend = F) %>%
add_trace(
x = ~precipitation,
y = ~fall_type,
type = "bar",
transforms = list(
list(
type = "aggregate",
groups = ~chart_cat,
aggregations = list(
list(
target = "x", func = "avg", enabled = T))))) %>%
add_text(
x = ~precipitation*1.25,
y = ~fall_type,
text = ~round(precipitation,0),
showlegend = F,
)
}
make_a_chart("south")
您可以像聚合 Plotly 图中的任何其他元素一样聚合文本。
例如,您可以这样做:
make_a_chart <- function(location){
my_tibble %>%
filter(region==location) %>%
plot_ly() %>%
layout(yaxis = list(title = none),
xaxis = list(title = none, tickformat = ","),
barmode = 'stack', showlegend = F) %>%
add_trace(
x = ~precipitation,
y = ~fall_type,
type = "bar",
transforms = list(
list(
type = "aggregate",
groups = ~chart_cat,
aggregations = list(
list(target = "x", func = "avg", enabled = T))))) %>%
add_text(
x = ~precipitation*1.25,
y = ~fall_type,
text = ~round(precipitation,0),
transforms = list(
list(
type = "aggregate",
groups = ~chart_cat,
aggregations = list(
list(target = "text", func = "avg", enabled = T),
list(target = "x", func = "avg", enabled = T)))),
showlegend = F,
)
}
您也可以设置文本样式。
make_a_chart("south") %>% layout(font = list(color = "#b21e29", size = 15))
我绘制了一个汇总图表来显示平均值。我想知道如何向图表添加文本标签以说明图表上显示的平均值。
下面是基于我的代码的代表。在这段代码中,我制作了一个图表来展示平均降雪量和降雨量。我已经在图表中添加了文本标签,但它们对应于图表上的每个数据点(不是平均值,这是我所追求的)。谢谢你的建议。
当前代码:
my_tibble <- tibble(precipitation = as.numeric(runif(10,3,10)),
week = c(1,2,3,4,5,1,2,3,4,5),
fall_type = c("rain", "snow", "snow", "rain", "rain",
"rain", "rain", "snow", "snow", "snow"),
region = c("south", "south", "south", "south", "south",
"north", "north", "north", "north", "north"))
my_tibble <- my_tibble %>%
mutate(chart_cat = paste(region, fall_type, sep = ","))
make_a_chart <- function(location){
my_tibble %>%
filter(region==location) %>%
plot_ly() %>%
layout(
yaxis = list(title = none),
xaxis = list(title = none,
tickformat = ","),
barmode = 'stack',
showlegend = F) %>%
add_trace(
x = ~precipitation,
y = ~fall_type,
type = "bar",
transforms = list(
list(
type = "aggregate",
groups = ~chart_cat,
aggregations = list(
list(
target = "x", func = "avg", enabled = T))))) %>%
add_text(
x = ~precipitation*1.25,
y = ~fall_type,
text = ~round(precipitation,0),
showlegend = F,
)
}
make_a_chart("south")
您可以像聚合 Plotly 图中的任何其他元素一样聚合文本。
例如,您可以这样做:
make_a_chart <- function(location){
my_tibble %>%
filter(region==location) %>%
plot_ly() %>%
layout(yaxis = list(title = none),
xaxis = list(title = none, tickformat = ","),
barmode = 'stack', showlegend = F) %>%
add_trace(
x = ~precipitation,
y = ~fall_type,
type = "bar",
transforms = list(
list(
type = "aggregate",
groups = ~chart_cat,
aggregations = list(
list(target = "x", func = "avg", enabled = T))))) %>%
add_text(
x = ~precipitation*1.25,
y = ~fall_type,
text = ~round(precipitation,0),
transforms = list(
list(
type = "aggregate",
groups = ~chart_cat,
aggregations = list(
list(target = "text", func = "avg", enabled = T),
list(target = "x", func = "avg", enabled = T)))),
showlegend = F,
)
}
您也可以设置文本样式。
make_a_chart("south") %>% layout(font = list(color = "#b21e29", size = 15))