希望我能正确解释。我的数据可以从两个层次上看到:我有四个主要类别,每个类别包含四个子类别。
在我的数据集中,我有大约50个观察值,每个观察值可以分为一个主要类别和一个子类别。以下图片显示了我的类别:
1. MainCategory 1
- Subcategory 11
- Subcategory 12
- Subcategory 13
- Subcategory 14
2. MainCategory 2
- Subcategory 21
- Subcategory 22
- Subcategory 23
- Subcategory 14
etc...
一个数据行可以显示如下:
ID Maincategory Subcategory
#1 Maincategory2 Subcategory 23
#2 Maincategory2 Subcategory 21
#3 Maincategory4 Subcategory 44
我现在的目标是使用geom_bar()
ggplot评估我的数据行。该栏应由4个小节组成,每个小节分别代表主要类别。在此栏中,应以堆叠方式说明子类别的数量。因此,每个小节代表主要类别的计数,每个小节内部的ift代表子类别的计数
到目前为止,我的尝试如下:(添加了MWE)
readr::read_table(
" ID Main Sub
1 Tool View Information Processing Tool
2 Ensemble View Embedded System
3 Computational View Model
4 Tool View Social Relations
6 Tool View Information Processing Tool
7 Tool View Productivity Tool
8 Nominal View Absent Technology
9 Ensemble View Embedded System
10 Nominal View Absent Technology
11 Tool View Social Relations
14 Tool View Productivity Tool
15 Proxy View Technology Perception
17 Proxy View Computer Perception
16 Computational View Algorithm"
) %>%
ggplot() +
aes(Main, Sub, fill=Main) +
geom_bar(stat="identity", position = "stack", alpha=1, width =.6,aes(fill=Main),color="white")
但是我一直想实现这样的目标,在这里我也像在这里一样在条形图中直接写标签(例如Subcategory21):
正如其他人所提到的,这将有助于更清晰地了解您所需的内容,并复制代码。
这是一种有助于您进一步讨论可能需要的方法。
编辑:添加了调色板以自定义颜色。在为子类别定义了所需的颜色后,请scale_fill_manual
在以下绘图中使用。
library(tidyverse)
Palette <- c("Algorithm" = "#ff0000",
"Model" = "#ff6600",
"Embedded System" = "#99ff33",
"Absent Technology" = "#ffff00",
"Technology Perception" = "#003399",
"Information Processing" = "#cc99ff",
"Productivity Tool" = "#cc00ff",
"Social Relations" = "#cc0066")
readr::read_table(
"ID Main Sub
1 Tool View Information Processing Tool
2 Ensemble View Embedded System
3 Computational_View Model
4 Tool View Social Relations
5 Tool View Productivity Tool
6 Tool View Information Processing Tool
7 Tool View Productivity Tool
8 Nominal View Absent Technology
9 Ensemble View Embedded System
10 Nominal View Absent Technology
11 Tool View Social Relations
12 Tool View Productivity Tool
13 Tool View Productivity Tool
14 Tool View Productivity Tool
15 Proxy View Technology Perception
16 Computational_View Algorithm"
) %>%
ggplot(aes(x = Main, fill = Sub, label = str_wrap(Sub, 10))) +
geom_bar(position = "fill") +
geom_text(stat = 'count', position = position_fill(vjust = .5)) +
scale_fill_manual(name = "Sub", values = Palette)
情节
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句