作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有如下所示的数据:
library(dplyr)
library(plotly)
df<-data_frame(Color=c("Green","Green","Yellow","Yellow","Gray","Gray","Red","Red"))
# A tibble: 8 x 1
Color
<chr>
Green
Green
Yellow
Yellow
Gray
Gray
Red
Red
Color
中的颜色。列并包含百分比以及给定颜色出现次数的原始计数。我试过这个:
df %>%
group_by(Color) %>%
summarize(count = n()) %>%
plot_ly(labels = ~Color, values = ~count,colors=c("Green","Yellow","Red","Gray")) %>%
add_pie(hole = 0.6) %>%
layout(title = "test chart", showlegend = T,
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
最佳答案
这是所需的语法:
library(plotly)
df %>%
group_by(Color) %>%
summarize(count = n()) %>%
plot_ly(labels = ~Color,
values = ~count,
text = ~count,
textinfo = 'label+text',
marker = list(colors = c("Green","Yellow","Red","Gray"))) %>%
add_pie(hole = 0.6) %>%
layout(title = "test chart", showlegend = T,
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
marker = list(colors =
更改切片的颜色。
text
+ `textinfo - 定义自定义文本
plotly
中绘制分类变量的顺序取决于因子水平的顺序。然而,在这种情况下,顺序由提供的数据帧的顺序决定。要更改顺序可以使用
match
.
df %>%
group_by(Color) %>%
summarize(count = n()) -> df
colors = c("Green","Yellow","Red","Gray")
plot_ly(df[match(colors, df$Color),],
labels = ~Color,
values = ~count,
text = ~count,
marker = list(colors = colors)) %>%
add_pie(hole = 0.6) %>%
layout(title = "test chart", showlegend = T,
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
关于r - R 中的圆环图自定义问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49416958/
我是一名优秀的程序员,十分优秀!