Home > Mobile >  Stacked bar chart: how to define fill
Stacked bar chart: how to define fill

Time:03-07

Once again, I need your help and I'm looking forward to listen & learn.

I would like to depict the interrelation between several types of birds (t1 - t7). It is observed how frequently they interact with their own type or other types of birds (o1 - o7)

The data (bird.data) looks as follows:

type o1 o2 o3 o4 o5 o6 o7
t1 77.6 3.5 1.5 9.1 3.3 1.4 3.6
t2 10 64.4 9 3.2 2.1 7.4 3.9
t3 8.6 12.1 63.2 2.4 0.6 8.8 4.3
t4 28.7 5.1 2.0 51.4 5.8 0.3 6.6
t5 4.1 12.2 9.5 1.0 0.4 64.5 8.3
t6 10.9 3.6 0.3 6.5 66.8 2.0 9.9
t7 15.9 8.2 4.5 5.6 9.4 5.4 51.0
bird.data <- structure(list(type = c("t1”, “t2", “t3”, “t4”, “t5”, “t6”, “t7”), o1 = c(77.6, 10, 8.6, 28.7, 4.1, 10.9, 15.9), o2 = c(3.5, 64.4, 12.1, 5.1, 12.2, 3.6, 8.2), o3 = c(1.5, 9, 63.2, 2, 9.5, 0.3, 4.5), o4 = c(9.1, 3.2, 2.4, 51.4, 1, 6.5, 5.6), o5 = c(3.3, 2.1, 0.6, 5.8, 0.4, 66.8, 9.4), o6 = c(1.4, 7.4, 8.8, 0.3, 64.5, 2, 5.4), o7 = c(3.6, 3.9, 4.3, 6.6, 8.3, 9.9, 51)), row.names = c(NA, -7L), class = c("tbl_df", "tbl", "data.frame"))

To show this graphically, I want to plot a flipped, stacked bar chart - similar to this one: enter image description here (Taken from this post: Add percentage labels to stacked bar chart ggplot2)

Instead of County, I want to depict the different types of birds on the x-axis (type), instead of Plan Types I want to fill the bar with the percentage of interactions between the different types of birds (o1-o7).

My questions now are:

  • How do I organize/group columns o1, o2, o3, o4, o5, o6, o7 to be used as fill?
  • What is my y?
ggplot()  
geom_bar(aes(y = ?, x = type, fill = ?),
         data = bird.data,
         stat="identity")  
         coord_flip()

Thanks a lot in advance for any advice! :-)

CodePudding user response:

You could achieve your desired result by converting your data to long format using e.g. tidyr::pivot_longer. Afterwards it's pretty straightforward what should be mapped on fill and y.

Note: Instead of geom_bar(.. stat="identity") I use geom_col(...). Also instead of coord_flip I switched the role of x and y.

library(tidyr)
library(ggplot2)

bird_data_long <- bird.data %>% 
  pivot_longer(-type, names_to = "name", values_to = "value")

head(bird_data_long)
#> # A tibble: 6 × 3
#>   type  name  value
#>   <chr> <chr> <dbl>
#> 1 t1    o1     77.6
#> 2 t1    o2      3.5
#> 3 t1    o3      1.5
#> 4 t1    o4      9.1
#> 5 t1    o5      3.3
#> 6 t1    o6      1.4

ggplot()  
  geom_col(aes(x = value, y = type, fill = name), data = bird_data_long)

  • Related