这是绘制饼图的R代码
library(data.table)
# Data
data <- data.table(
class = c("class 1", "class 2", "class 3", "class 4"),
count = c(2403, 4949, 18, 570))
# Pie
data %>% plot_ly() %>%
add_trace(
type = 'pie',
labels = ~class,
values = ~count,
marker = list(
color = "rgb(211, 211, 211)",
line = list(color = "rgb(20, 20, 20)", width = 1))
) %>%
layout(
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
当我需要灰度时会绘制一个“彩色”图表。
答案 0 :(得分:2)
要获得关于类变量长度的灵活方法,您可以简单地使用:
colors = gray.colors(length(data$class))
情节1:
或者,如果您想要其他灰色调,则可以使用以下方式指定自己的颜色:
colors = list("rgb(80, 80, 80)", "rgb(120, 120, 120)", "rgb(160, 160, 160)", "rgb(160, 160, 160)")
图2:
完整代码:
library(data.table)
library(dplyr)
library(plotly)
# Data
data <- data.table(
class = c("class 1", "class 2", "class 3", "class 4"),
count = c(2403, 4949, 18, 570))
# Pie
data %>% plot_ly() %>%
add_trace(
type = 'pie',
labels = ~class,
values = ~count,
marker = list(
#colors = list("rgb(120, 120, 120)", "rgb(160, 160, 160)", "rgb(180, 180, 180)", "rgb(220, 220, 220)"),
colors = gray.colors(length(data$class)),
line = list(color = "rgb(20, 20, 20)", width = 1))
) %>%
layout(
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))