如何在 R 中绘制具有不同级别的可折叠树?

发布于 2025-01-18 00:56:31 字数 829 浏览 0 评论 0原文

我一直在尝试使用 collapsibletree 包绘制可折叠树,但我没有得到我需要的东西。此外,应使用 R 脚本视觉功能将该图上传到 Power BI。

想象一下,我有一个像这样的数据框:

parent <- c("", "Carlos", "Carlos", "María", "María", "Paula", "Alex")
child <- c("Carlos", "María", "Alex", "Javier", "Paula", "Pablo", "Pepe")

df <- data.frame(parent, child)

然后,我想绘制一棵可折叠树:

collapsibleTree(df = df,
                hierarchy = c("parent", "child"),
                root = "Carlos")

但我得到这个结果:

在此处输入图像描述

我想要的结果是这样的: 输入图片此处描述

有人知道如何获得该解决方案吗?

提前致谢,祝您有美好的一天!

I have been trying to plot a collapsible tree with collapsibletree package but I don't get what I need. Also, this plot should be uploaded to power BI using the R script visual feature.

Imagine that I have a dataframe like this:

parent <- c("", "Carlos", "Carlos", "María", "María", "Paula", "Alex")
child <- c("Carlos", "María", "Alex", "Javier", "Paula", "Pablo", "Pepe")

df <- data.frame(parent, child)

And then, I want to plot a collapsible tree:

collapsibleTree(df = df,
                hierarchy = c("parent", "child"),
                root = "Carlos")

But I get this result:

enter image description here

And the result I want is this:
enter image description here

Does anyone know how to get that solution?

Thanks in advance and have a great day!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少钕鈤記 2025-01-25 00:56:31

您的数据中应该具有一个值为na的值,因为您需要有一个起点。这就是为什么我在parent> parent向量为na中更改您的“”的原因。之后,您可以使用以下代码:

parent <- c(NA, "Carlos", "Carlos", "María", "María", "Paula", "Alex")
child <- c("Carlos", "María", "Alex", "Javier", "Paula", "Pablo", "Pepe")

df <- data.frame(parent, child)

library(collapsibleTree)
collapsibleTreeNetwork(df, collapsed = FALSE)

输出:

”在此处输入图像说明”

You should have one value as NA in your data, because you need to have a beginning point. That's why I changed your "" in the parent vector to NA. After that you can use the following code:

parent <- c(NA, "Carlos", "Carlos", "María", "María", "Paula", "Alex")
child <- c("Carlos", "María", "Alex", "Javier", "Paula", "Pablo", "Pepe")

df <- data.frame(parent, child)

library(collapsibleTree)
collapsibleTreeNetwork(df, collapsed = FALSE)

Output:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文