Suggested solution using dplyr
, tidyr
and 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")
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.