首页 > 解决方案 > 使用 R 在绘图图中选择前 3 个父母(组)

问题描述

我想要的是只绘制我的 3个,用下面的编码parents花费最高的那些。cost

parent <- as.character(c("Sam","Elena","Sam","Jhon","Raul","Sam","Jhon","Sara","Paul","Chris"))
cost <- as.numeric(as.character(c(15000,10000,12000,15000,10000,12000,15000,14000,19000,2000)))
topic <- as.character(c("Banana","Banana","Berries","Apple","Watermelon","Banana","Berries","Avocado","Watermelon","Pinneaple"))

sample <- as.data.frame(cbind(parent,cost,topic))
sample$cost <- as.numeric(as.character(sample$cost))
sample$parent <- as.character(sample$parent)
sample$topic <- as.character(sample$topic)

# Color setting
ramp2 <- colorRamp(c("deepskyblue4", "white"))
ramp.list2 <- rgb( ramp2(seq(0, 1, length = 15)), max = 255)

plot_ly(sample, x = ~parent, y = ~cost, type = 'bar', color = ~topic) %>%
  layout(yaxis = list(title = 'Cost'), xaxis = list(title = 'Parent'), barmode = 'stack', colorway = ramp.list2) %>%
  config(displayModeBar = FALSE)

我尝试使用transforms内部plotly函数,如下所示:

transforms = list(
list(
type = 'aggregate',
groups = sample$parent,
aggregations = list(
list(
target = 'x', 
func = 'max', 
enabled = T))
))

但它仍然给我相同的输出,我只想选择 3 个。另外,尝试像这样使用它:

transforms = list(
list(
type = 'filter',
target = 'y',
operation = '>',
value = cost[-3:-1]))

但是它只需要花费而不需要全部cost parent花费,并且只给了我2个父母而不是3个。最后,它不是ramp.list2用来选择颜色的。

标签: rplotly

解决方案


根据我的理解,可以使用下面的代码分别获取top 3的父母,如下:

top_3 <- sample %>% 
         group_by(parent) %>% 
         summarise(cost = sum(cost)) %>% 
         arrange(-cost) %>% 
         head(3)

这将为您提供以下信息:

# A tibble: 3 x 2
#   parent  cost
#   <chr>  <dbl>
# 1 Sam    39000
# 2 Jhon   30000
# 3 Paul   19000

然后,在你的 中plot_ly,你可以只引用这些 top_3 父母,如下:

plot_ly(sample[sample$parent %in% top_3$parent,], x = ~parent, y = ~cost, type = 'bar', color = ~topic) %>%
   layout(yaxis = list(title = 'Cost'), xaxis = list(title = 'Parent'), barmode = 'stack', colorway = ramp.list2) %>%
   config(displayModeBar = FALSE)

这将产生以下情节:

阴谋

希望能帮助到你。


推荐阅读