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:
(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! :-)
EDIT: I have implemented the great suggestions by stefan, and now encountered another problem:
When adding labels to the stacked bar chart, vjust=0.5 does not place them in the middle of the respective bar:
```
ggplot() +
geom_col(aes(x = value, y = type, fill = name),
data = bird_data_long,
position = position_stack(reverse = TRUE)) +
scale_y_discrete(limits=rev) +
geom_label(data = subset(bird_data_long, value > 10),
aes(x = value, y = type, label = paste0(round(value, digits = 0),"%")),
size = 3, position = position_stack(vjust = 0.5))
```
My question therefore is: how can I adjust the position of the labels so that they are placed exactly in the middle of the bar they are representing? Any idea why they are skewed? Any advice is much appreciated!