I'm struggling with trying to create a simple bar graph where the bars are next to each other(like pictured).
The data are from different databases.
Database A
Month Cost
1 Dec 3009
2 Dec 3468
3 Nov 3420
4 Oct 3162
Database B
Month Cost
1 Dec 309
2 Nov 3481
3 Nov 3419
4 Oct 3120
For the bar graph, I am trying to show a bar graph that has the X-axis as the month, and for month Dec, A would have 2 (the total count, not the value), B = 1, for month Nov, A would have 1, and B would have 2. Thank you!
You could add an id variable to your datasets, bind them by row and make a standard bar chart where you map id on fill
:
db1 <- read.table(text = "Month Cost
1 Dec 3009
2 Dec 3468
3 Nov 3420
4 Oct 3162", header = TRUE)
db2 <- read.table(text = "Month Cost
1 Dec 309
2 Nov 3481
3 Nov 3419
4 Oct 3120", header = TRUE)
library(ggplot2)
library(dplyr)
db <- dplyr::bind_rows(list(A = db1, B = db2), .id = "id")
db$Month <- factor(db$Month, levels = month.abb)
ggplot(data = db, mapping = aes(Month, fill = id)) +
geom_bar(position = "dodge")
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.