1

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: enter image description here

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:

enter image description here

SRel
  • 383
  • 1
  • 11
  • Hi SRel. Welcome to StackOverflow! Please read the info about how to give a [minimale reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). That way you can help others to help you! – dario Feb 23 '20 at 14:16
  • I have added a MWE – SRel Feb 23 '20 at 14:30
  • I'm sorry but your MRE is not reproducible with the information you provided. But maybe someone else has more luck... (again, just for your information: the idea behind a MRE is to provide as **little** code and data as necessary to reproduce the problem/question. There should be no need to install ggpubr only to rotate labels etc. at least that's what I'd expect. Others might see this differently) – dario Feb 23 '20 at 14:35
  • oh okay i was not aware of this i will modify my mwe – SRel Feb 23 '20 at 14:38
  • If you were not aware of this I'd highly suggest to hava a look at the link I provided in the first comment: [how to make a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). – dario Feb 23 '20 at 14:41
  • Why is your aim to use `geom_bar` from ggplot? Is that a must? Are you not allowed to use any other R command to get the graph you show? Just curious. ☺ – Edward Feb 23 '20 at 14:44
  • @Edward nono that is no must that was just the first way i was thinking of for representing the data – SRel Feb 23 '20 at 14:46

1 Answers1

1

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

plot with subcategories with customized colors based on palette

Ben
  • 28,684
  • 5
  • 23
  • 45
  • I added `scale_fill_brewer(type = "seq", palette ="Greys", direction = 1,aesthetics = "fill")` to get some nice colours, however all of the different subcategories have their own color. Is it possible that maincategory 1 have one own colour and all subcategories inside also have this colour? So I am finally using four different colours for each Main.Modifiy `fill=Sub` to `fill=Main` did not work unfortunately... – SRel Feb 23 '20 at 17:03
  • I'm not sure I'm understanding. Do you want the same color scheme across all of the bar? In other words, if you have 4 different colors in a palette, do you want to apply that to each bar, so that the subcategories in each bar use one of those 4 colors? – Ben Feb 23 '20 at 17:05
  • yes correct, so that each of the 4 bars has its own colour and the sections inside the bar is of one colour – SRel Feb 23 '20 at 17:13
  • @SRel see edited answer - you can customize individual colors for your subcategories. See if this will work for you. You could use different color shades in a common main category, or any strategy you think is effective. – Ben Feb 23 '20 at 17:55