我有四个单独的饼图,它们都具有相同的指定配色方案(除了数据框之外,代码是相同的)。
指定颜色是因为我想将它们组合在 4 个网格图中,并使用一个图例为 5 组中的每一组使用相同的指定颜色(即,当未指定颜色时,颜色会根据组的大小)。
示例数据:
# Data
g = c("D","L","X","A","N","B")
v = c(49,14,9,7,6,5)
df1 = data.frame(group = g, value = v)
set.seed(9) # Just for reproductibility
df2 = data.frame(group = sample(g,size = nrow(df1),replace = F),
value = sample(v,size = nrow(df1),replace = F)
)
set.seed(8)
df3 = data.frame(group = sample(g,size = nrow(df1),replace = F),
value = sample(v,size = nrow(df1),replace = F)
)
set.seed(7)
df4 = data.frame(group = sample(g,size = nrow(df1),replace = F),
value = sample(v,size = nrow(df1),replace = F)
)
代码:
BC <-
plot_ly(b_c, labels = ~group, values = ~value, marker = list(colors = c( '#2ca02c', '#d62728','#9467bd', '#FF7F0E', '#1F77B4')), type = 'pie',textposition = 'outside',textinfo = 'label+percent') %>%
layout(title = 'b_c',autosize = F, width = 690, height = 690, margin = m,
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
BC
我已经查看了这些帖子,但无法确定从这些帖子中梳理 plotly 的答案。
Subplots deploying in R Plotly
Plotting multiple pie charts in plotly
我在 ggplot 中寻找类似于 ggarrange 的东西
我有四组数据,其中一些具有相同的组(即相同的行名)。对于这些,我想使用相同的颜色。
我真的不介意配色方案(即可以是任何颜色),但希望所有 4 个饼图都有一个统一的颜色图例。
最佳答案
你好@sar,看看它是否解决了你的问题:
library(plotly)
library(dplyr)
# Data
g = c("D","L","X","A","N","B")
v = c(49,14,9,7,6,5)
df1 = data.frame(group = g, value = v)
set.seed(9) # Just for reproductibility
df2 = data.frame(group = sample(g,size = nrow(df1),replace = F),
value = sample(v,size = nrow(df1),replace = F)
)
set.seed(8)
df3 = data.frame(group = sample(g,size = nrow(df1),replace = F),
value = sample(v,size = nrow(df1),replace = F)
)
set.seed(7)
df4 = data.frame(group = sample(g,size = nrow(df1),replace = F),
value = sample(v,size = nrow(df1),replace = F)
)
#Plot
plot_ly(labels = ~group, values = ~value, legendgroup = ~group,
textposition = 'outside',textinfo = 'label+percent') %>%
add_pie(data = df1, name = "DF1", domain = list(row = 0, column = 0))%>%
add_pie(data = df2, name = "DF2", domain = list(row = 0, column = 1))%>%
add_pie(data = df3, name = "DF3", domain = list(row = 1, column = 0))%>%
add_pie(data = df4, name = "DF4", domain = list(row = 1, column = 1))%>%
layout(title = "Pie Charts in Grid", showlegend = T,
grid=list(rows=2, columns=2),
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
输出:
编辑 1: 要为每个饼图显示“字幕”,您可以使用 annotations , 您还可以更改 legend位置。注释的缺点是你必须指定位置(在这种情况下是手动的)。
为避免重叠,我建议删除 textposition = 'outside'
。
您可以使用绘图右上角的按钮将绘图下载为 .png。
#Plot
plot_ly(labels = ~group, values = ~value, legendgroup = ~group,
textinfo = 'label+percent') %>%
add_pie(data = df1, name = "DF1", domain = list(row = 0, column = 0))%>%
add_pie(data = df2, name = "DF2", domain = list(row = 0, column = 1))%>%
add_pie(data = df3, name = "DF3", domain = list(row = 1, column = 0))%>%
add_pie(data = df4, name = "DF4", domain = list(row = 1, column = 1))%>%
layout(title = "Pie Charts in Grid", showlegend = T,
grid=list(rows=2, columns=2),
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
legend = list(y = 0.5),
annotations = list(x = c(.08, .62, .08, .62),
y = c(.78, .78, .22, .22),
text = c("Pie 1","Pie 2","Pie 3","Pie 4"),
xref = "papper",
yref = "papper",
showarrow = F
)
)
您可以使用 template 随意更改文本和悬停文本.
这是一个建议取消标签以获得更多空间的百分比和四舍五入百分比到小数点后一位的编辑:
#Plot
plot_ly(labels = ~group, values = ~value, legendgroup = ~group, textinfo = 'label+percent',
texttemplate = "%{percent:.1%}",
hovertemplate = "%{label} <br> %{percent:.1%} <br> %{value}") %>%
add_pie(data = df1, name = "DF1", domain = list(row = 0, column = 0))%>%
add_pie(data = df2, name = "DF2", domain = list(row = 0, column = 1))%>%
add_pie(data = df3, name = "DF3", domain = list(row = 1, column = 0))%>%
add_pie(data = df4, name = "DF4", domain = list(row = 1, column = 1))%>%
layout(title = "Pie Charts in Grid", showlegend = T,
grid=list(rows=2, columns=2),
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
legend = list(y = 0.5),
annotations = list(x = c(.08, .62, .08, .62),
y = c(.78, .78, .22, .22),
text = c("Pie 1","Pie 2","Pie 3","Pie 4"),
xref = "papper",
yref = "papper",
showarrow = F
)
)
新输出:
关于r - 在 R 中排列 4 个 plotly 饼图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59672562/