r - 使用 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
用来选择颜色的。
解决方案
根据我的理解,可以使用下面的代码分别获取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)
这将产生以下情节:
希望能帮助到你。
推荐阅读
- google-apps-script - Gmail 两次发送电子邮件
- python - django:将模型邮件字段导入表单导致 SMTPRecipientsRefused 错误
- php - 注意:未定义索引 userkullanici
- ios - 如何在屏幕上不出现提示的情况下获得快速的 UIButton 来执行电话呼叫
- angular - 如何从 Angular 组件标记重定向到它在 Visual Studio 中的定义?
- pygments - 如何更改 Pygments 中特定行的背景?
- javascript - Typescript mongoose - 我的数组中未定义的对象
- python - 如何在 Google Colab 中安装 fastai v0.7?
- android - AudioFocus 请求拒绝 OpenTok(Tokbox) 在相同添加的呼叫中,android 9 和 10
- java - 如何使用 darker() 方法?