gpt4 book ai didi

Nest Subcategories in R on X axis(在X轴上的R上嵌套子范畴)

转载 作者:bug小助手 更新时间:2023-10-26 20:05:53 24 4
gpt4 key购买 nike



I have a dataset that I am playing with and learning about in R. I am attempting to accomplish a visualization where there are multiple models of a particular item with several sub categories of data for each item that I need to display. Each model has the same subcategories of data, I.E. Model A has Spec1, Spec2, Spec3, Spec4, and Spec5. I want to list out ModelA, ModelB, ModelC, and show their respective data values above each model. I have pivoted the table of data from one row listing the model and each subcategory, to each row displaying a different sub cateory of data, where you may see the model several times. I am not sure if that is how I need to wrangle the data to begin with, but I found a tutorial showing a similar visualization that worked for them, but it didnt work for me. I am somewhat stuck, and I am looking for advice on how to accomplish this quickly and effectively, as I am new to R. I am not sure I am asking the right questions or attempting to wrangle correctly in the first place. Thanks for any advice

我有一个数据集,我正在R中操作和学习。我正在尝试完成一个可视化,其中有一个特定项目的多个模型,每个项目都有几个子类别的数据,我需要显示。每个模型都具有相同的数据子类别,即模型A具有spec1、spec2、spec3、spec4和spec5。我想列出ModelA、ModelB、ModelC,并在每个模型上显示它们各自的数据值。我已经将数据表从列出模型和每个子类别的一行,旋转到每一行显示不同的数据子目录,在那里您可能会多次看到模型。我不确定一开始我是否需要这样处理数据,但我找到了一个教程,它展示了一个类似的可视化方法,对他们有效,但对我不起作用。我有些手足无措,我正在寻求关于如何快速有效地完成这项工作的建议,因为我刚接触R。我不确定我问的问题是正确的,还是试图在一开始就正确地争论。谢谢你的建议


I tried the following code

我尝试了以下代码


barchart <- ggplot()
barchart <- barchart +
geom_col(
ata = dataset,
aes(x = dataCategories, y = dataNumbers, ),
position = "dodge"
)
barchart

I also tried this

我也试过这个


barchart <- ggplot()
barchart <- barchart +
geom_col(
main.cat = c("productName"),
second.cat = c("subcat1", "subcat2", "subcat3", "supcat4", "subcat5"),
value = c("dataNumbers")
)

更多回答

Weclome to SO! From the description of what you are trying to achieve something like ggplot(dataset) + geom_col(aes(x = CATEGORIES, y = NUMBERS, fill = SUBCATEGORIES), position = "dodge") might be give you your desired result. For more help please provide a minimal reproducible example including a snippet of your data or some fake data. As is we can't tell whether your data is in the right shape or needs some additional wrangling.

我们来到了这样的地方!根据对您尝试实现的内容的描述,可能会得到您想要的结果,比如ggploy(数据集)+geom_ol(aes(x=类别,y=数字,填充=子类别),位置=“dodge”)。要获得更多帮助,请提供一个最小的可重现的示例,包括您的数据片段或一些虚假数据。也就是说,我们无法判断您的数据是否处于正确的状态,或者是否需要进行一些额外的争论。

优秀答案推荐

Something like this? Note that the test data is in the long format.

像这样的吗?请注意,测试数据是长格式的。


suppressPackageStartupMessages({
library(ggplot2)
library(dplyr)
})

df1 %>%
mutate(
productName = factor(productName, levels = productName %>% unique()),
subcat = sub("[[:alpha:]]+", "", subcat)
) %>%
group_by(productName) %>%
mutate(Group = interaction(subcat, productName, sep = "&")) %>%
ggplot(aes(Group, dataNumbers, group = subcat)) +
geom_col(position = position_dodge(), fill = "steelblue") +
labs(x = "Model", y = "Data Numbers") +
guides(x = ggh4x::guide_axis_nested(delim = "&")) +
theme_bw()


Created on 2023-09-10 with reprex v2.0.2

创建于2023-09-10,Reprex v2.0.2




Test data


set.seed(2023)
Model <- sprintf("Model%s", LETTERS[1:3])
subcat <- sprintf("Spec%d", 1:5)
df1 <- expand.grid(subcat, Model)[2:1]
names(df1) <- c("productName", "subcat")
df1$dataNumbers <- sample(10:20, nrow(df1), TRUE)
df1
#> productName subcat dataNumbers
#> 1 ModelA Spec1 14
#> 2 ModelA Spec2 18
#> 3 ModelA Spec3 17
#> 4 ModelA Spec4 12
#> 5 ModelA Spec5 19
#> 6 ModelB Spec1 11
#> 7 ModelB Spec2 10
#> 8 ModelB Spec3 10
#> 9 ModelB Spec4 10
#> 10 ModelB Spec5 10
#> 11 ModelC Spec1 14
#> 12 ModelC Spec2 17
#> 13 ModelC Spec3 11
#> 14 ModelC Spec4 12
#> 15 ModelC Spec5 13

Created on 2023-09-10 with reprex v2.0.2

创建于2023-09-10,Reprex v2.0.2


更多回答

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com