Предлагаемое решение с использованием dplyr
, tidyr
и ggplot2
facet_
:
df <- read.table(text='year A B C
1983 1 2 10
1983 2 3 7
1984 1 3 7
1984 2 4 8
1985 1 6 6
1985 2 5 10', header=TRUE, stringsAsFactors=FALSE)
library(dplyr)
library(tidyr)
library(ggplot2)
df %>%
pivot_longer(cols = c(B, C)) %>%
ggplot(aes(factor(A), value, fill=factor(name, levels= c("C", "B")))) +
geom_bar(stat="identity", position="stack") +
facet_grid(~year) +
labs(x="A", fill="Variable")
![enter image description here](https://i.stack.imgur.com/mMute.png)