Hopefully I can explain it correctly. My data can be seen on two level: I have four main categories which consist of four subcategories each.
Inside my dataset I have about 50 observations, each observation can be sorted into one main category and one of the subcategories accordingly .Following picture display my categories:
1. MainCategory 1
- Subcategory 11
- Subcategory 12
- Subcategory 13
- Subcategory 14
2. MainCategory 2
- Subcategory 21
- Subcategory 22
- Subcategory 23
- Subcategory 14
etc...
One datarow can be shown as following:
ID Maincategory Subcategory
#1 Maincategory2 Subcategory 23
#2 Maincategory2 Subcategory 21
#3 Maincategory4 Subcategory 44
My aim was now to use geom_bar()
from ggplot to evaluate my datarows. This should consist of 4 bars, each one for the main categories. Inside this bar it should state the count of the subcateogires in a stacked manner. So each bar represents the count of the maincategories and inside each bar ift differs the counts of subcategories
My attempt so far is the following: (MWE added)
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")
This is resulting the following:
However I was looking to achieve something like this, where I also write the label (eg. Subcategory21) directly in barchart like it was did here:
As mentioned by the others, it would help to have a clearer picture of what you need, and code to reproduce.
Here is an approach that may help facilitate further discussion about what you might need.
Edit : A color palette is added to customize colors. After defining the colors your want for your subcategories, use scale_fill_manual
in your plot as below.
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)
Plot
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.