在 ggplotly 中格式化轴上的数字和工具提示
Format numbers on axes and tooltips in ggplotly
我已经使用 sprintf 和 formatC 取一个双精度值并将其四舍五入到小数点后两位。但是,当我在 ggplot 和 ggplotly 中使用它时,它使我的视觉效果表现出来。
输出:
structure(list(Date = structure(c(18328, 18329, 18330, 18331,
18332, 18333), class = "Date"), State = c("Louisiana", "Louisiana",
"Louisiana", "Louisiana", "Louisiana", "Louisiana"), variablename1 = c(0,
0, 1, 1, 6, 14), variablename2 = c(5, 5, 5, 11, 37, 37), death = c(0,
0, 0, 0, 0, 0), variablename3 = c(5, 5, 6, 12, 43, 51), variablename4 = c(0,
0, 0, 0, 0, 0), variablename5 = c(0, 0, 0, 0, 0, 0), variablename6 = c(0,
0, 0, 6, 26, 0), variablename7 = c(0, 0, 1, 0, 5, 8), variablename8 = c(0,
5, 1, 6, 31, 8), Percent = c(0, 0, 16.6666666666667, 8.33333333333333,
13.953488372093, 27.4509803921569)), row.names = c(NA, -6L), groups = structure(list(
State = "Louisiana", .rows = list(1:6)), row.names = c(NA,
-1L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
编辑:在寻找技巧的过程中,我发现 'non-short' 做事的方式更有效。只需使用 round(variable, 2) 将值四舍五入到小数点后第二位并使用它。现在。
这可以通过 scales::percent
或 scales::percent_format
轻松实现。例如,我通过 Date
绘制了 Percent
的线图,其中我使用 scales::percent_format
来格式化 y 轴标签,并使用 scales::percent
来格式化工具提示中的百分比值:
library(ggplot2)
library(plotly)
p <- df %>%
ungroup() %>%
ggplot(aes(Date, Percent, color = State, group = State,
text = sprintf("State: %s<br>Date: %s<br>Percent: %s",
State, Date, scales::percent(Percent, scale = 1, accuracy = .01)))) +
geom_line() +
scale_y_continuous(labels = scales::percent_format(scale = 1, accuracy = .01))
ggplotly(p, tooltip = 'text')
我已经使用 sprintf 和 formatC 取一个双精度值并将其四舍五入到小数点后两位。但是,当我在 ggplot 和 ggplotly 中使用它时,它使我的视觉效果表现出来。
输出:
structure(list(Date = structure(c(18328, 18329, 18330, 18331,
18332, 18333), class = "Date"), State = c("Louisiana", "Louisiana",
"Louisiana", "Louisiana", "Louisiana", "Louisiana"), variablename1 = c(0,
0, 1, 1, 6, 14), variablename2 = c(5, 5, 5, 11, 37, 37), death = c(0,
0, 0, 0, 0, 0), variablename3 = c(5, 5, 6, 12, 43, 51), variablename4 = c(0,
0, 0, 0, 0, 0), variablename5 = c(0, 0, 0, 0, 0, 0), variablename6 = c(0,
0, 0, 6, 26, 0), variablename7 = c(0, 0, 1, 0, 5, 8), variablename8 = c(0,
5, 1, 6, 31, 8), Percent = c(0, 0, 16.6666666666667, 8.33333333333333,
13.953488372093, 27.4509803921569)), row.names = c(NA, -6L), groups = structure(list(
State = "Louisiana", .rows = list(1:6)), row.names = c(NA,
-1L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
编辑:在寻找技巧的过程中,我发现 'non-short' 做事的方式更有效。只需使用 round(variable, 2) 将值四舍五入到小数点后第二位并使用它。现在。
这可以通过 scales::percent
或 scales::percent_format
轻松实现。例如,我通过 Date
绘制了 Percent
的线图,其中我使用 scales::percent_format
来格式化 y 轴标签,并使用 scales::percent
来格式化工具提示中的百分比值:
library(ggplot2)
library(plotly)
p <- df %>%
ungroup() %>%
ggplot(aes(Date, Percent, color = State, group = State,
text = sprintf("State: %s<br>Date: %s<br>Percent: %s",
State, Date, scales::percent(Percent, scale = 1, accuracy = .01)))) +
geom_line() +
scale_y_continuous(labels = scales::percent_format(scale = 1, accuracy = .01))
ggplotly(p, tooltip = 'text')