在 R plotly 中显示甜甜圈图中的总值/总和
show total /sum of values in donut plot in R plotly
我有一个示例 data set
,我正在尝试通过 plotly
在 R
中绘制一个基本的 donut
图。代码几乎(目前下面的代码不更新第二种颜色)工作正常。现在,我想在 respective
percentages
.
旁边显示 Type
下 IDs
的 values
的总和
我该怎么做?
数据
Value = c(50124, 9994, 9822, 13580, 5906, 7414, 16847, 59, 80550, 6824, 3111, 16756, 7702, 23034, 38058, 6729, 6951, 2, 408,
37360, 20517, 18714, 352, 3, 42922, 30850, 21, 4667, 12220, 8762, 445, 1875, 719, 188, 26, 124, 996, 10,
27, 304, 55, 34980, 67, 3, 25, 1012, 3588, 77, 847, 47, 1057, 924, 233, 40, 2, 2362, 3,
1866, 16, 0, 0, 0)
Type = c("A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B",
"B", "B", "B", "B", "B" "B", "B", "B", "B", "B", "B",
"B", "B", "B", "B", "B", "B", "B")
df = data.frame(Type, Value)
代码
library(tidyverse)
library(plotly)
color = c('rgb(0,255,255)', 'rgb(255,127,80)')
fig = df %>% plot_ly(labels = ~Type,
values = ~Value,
#colors = c("grey50", "blue"),
marker = list(colors = color))
fig = fig %>% add_pie(hole = 0.6,
text = ~paste(sum(Value)),
textinfo = "text + percent"))
fig = fig %>% layout(title = "Title", showlegend = T,
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = T),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = T))
fig
期望的输出
在您的数据框中计算一个 sum
列并将其粘贴到 text = ~paste(sum)
df1 <- df %>%
group_by(Type) %>%
mutate(sum = sum(Value))
library(tidyverse)
library(plotly)
color = c('rgb(0,255,255)', 'rgb(255,127,80)')
fig = df1 %>% plot_ly(labels = ~Type,
values = ~Value,
#colors = c("grey50", "blue"),
marker = list(colors = color))
fig = fig %>% add_pie(hole = 0.6,
text = ~paste(sum),
textinfo = "text + percent")
fig = fig %>% layout(title = "Title", showlegend = T,
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = T),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = T))
fig
我有一个示例 data set
,我正在尝试通过 plotly
在 R
中绘制一个基本的 donut
图。代码几乎(目前下面的代码不更新第二种颜色)工作正常。现在,我想在 respective
percentages
.
Type
下 IDs
的 values
的总和
我该怎么做?
数据
Value = c(50124, 9994, 9822, 13580, 5906, 7414, 16847, 59, 80550, 6824, 3111, 16756, 7702, 23034, 38058, 6729, 6951, 2, 408,
37360, 20517, 18714, 352, 3, 42922, 30850, 21, 4667, 12220, 8762, 445, 1875, 719, 188, 26, 124, 996, 10,
27, 304, 55, 34980, 67, 3, 25, 1012, 3588, 77, 847, 47, 1057, 924, 233, 40, 2, 2362, 3,
1866, 16, 0, 0, 0)
Type = c("A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B",
"B", "B", "B", "B", "B" "B", "B", "B", "B", "B", "B",
"B", "B", "B", "B", "B", "B", "B")
df = data.frame(Type, Value)
代码
library(tidyverse)
library(plotly)
color = c('rgb(0,255,255)', 'rgb(255,127,80)')
fig = df %>% plot_ly(labels = ~Type,
values = ~Value,
#colors = c("grey50", "blue"),
marker = list(colors = color))
fig = fig %>% add_pie(hole = 0.6,
text = ~paste(sum(Value)),
textinfo = "text + percent"))
fig = fig %>% layout(title = "Title", showlegend = T,
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = T),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = T))
fig
期望的输出
在您的数据框中计算一个 sum
列并将其粘贴到 text = ~paste(sum)
df1 <- df %>%
group_by(Type) %>%
mutate(sum = sum(Value))
library(tidyverse)
library(plotly)
color = c('rgb(0,255,255)', 'rgb(255,127,80)')
fig = df1 %>% plot_ly(labels = ~Type,
values = ~Value,
#colors = c("grey50", "blue"),
marker = list(colors = color))
fig = fig %>% add_pie(hole = 0.6,
text = ~paste(sum),
textinfo = "text + percent")
fig = fig %>% layout(title = "Title", showlegend = T,
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = T),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = T))
fig