r - How to superimpose bargraphs on top of each other
问题描述
Suppose I have the following data
set.seed(85)
test <- factor(replicate(10,sample(0:3,5,rep=TRUE)))
set.seed(108)
test2 <- factor(replicate(10,sample(0:3,5,rep=TRUE)))
and the following bargraphs:
barplot(table(test), col=rgb(0,1,0,.5))
barplot(table(test2), col=rgb(1,0,0,.5))
How do I combine these into 1 graph with 1 bargraph superimposed on the other? Something similar to this:
Any help is much appreciated.
解决方案
This combines two bar charts as set out in the question.
library(tibble)
library(dplyr)
library(tidyr)
library(ggplot2)
tib <-
tibble(t1 = table(test),
t2 = table(test2)) %>%
mutate(group = 1:4)
ggplot()+
geom_bar(data = select(tib, -2), aes(group, t1), stat = "identity", fill = "red", alpha = 0.25, width = 1)+
geom_bar(data = select(tib, -1), aes(group, t2), stat = "identity", fill = "green", alpha = 0.25,width = 1)
Which results in:
However, what I think you might be really looking for is:
library(tibble)
library(dplyr)
library(tidyr)
library(ggplot2)
set.seed(85)
test <- replicate(10,sample(0:3,5,rep=TRUE))
set.seed(108)
test2 <- replicate(10,sample(0:3,5,rep=TRUE))
tib1(tibble)
tib1 <-
tibble(t1 = c(test),
t2 = c(test2)) %>%
pivot_longer(cols = c(t1, t2), names_to = "test", values_to = "score")
ggplot(tib1, aes(score, fill = test))+
geom_histogram(bins = 4, alpha = 0.5, position = "identity")
Which gives you:
If the number of observations for each test are different and you want to plot a histogram: create two dataframes and combine them for graphing
set.seed(85)
test1 <- tibble(score = c(replicate(20, sample(0:3, 5, rep = TRUE))),
test = "t1")
set.seed(108)
test2 <- tibble(score = c(replicate(10, sample(0:3, 5, rep = TRUE))),
test = "t2")
tib1 <-
test1 %>%
bind_rows(test2)
ggplot(tib1, aes(score, fill = test))+
geom_histogram(bins = 4, alpha = 0.5, position = "identity")
If you prefer the geom_bar version you can adapt the previous code as follows:
ggplot()+
geom_bar(data = test1, aes(score), stat = "count", fill = "red", alpha = 0.25, width = 1)+
geom_bar(data = test2, aes(score), stat = "count", fill = "green", alpha = 0.25,width = 1)
By the way you could probably simplify your code, unless you have other reasons for using replicate as: c(replicate(10, sample(0:3, 5, rep = TRUE))) == sample(0:3, 50, rep = TRUE)
set.seed(108)
s1 <- c(replicate(10, sample(0:3, 5, rep = TRUE)))
set.seed(108)
s2 <- sample(0:3, 50, rep = TRUE)
tib <-
tibble(t1 = s1,
t2 = s2) %>%
pivot_longer(t1:t2, names_to = "test", values_to = "score")
ggplot(tib, aes(score, fill = test))+
geom_histogram(bins = 4, alpha = 0.5, position = "identity") +
facet_wrap(~test)
推荐阅读
- sql - 您如何连接多个表并查找行项目的计数?
- c++ - 如何在 C++ 中重新定位一个数组中的元素
- pandas - 处理包含多个排列和组合的熊猫中超过 600 列的 excel 文件的最佳方法是什么?
- php - 基于碳日期的准确日期格式
- php - Docker php:X-fpm-stretch with apache
- android - Android Material 文本输入布局结束图标不显示
- ios - Kotlin Native (iOS),使用 CValuesRef 和 CCCrypt
- regex - 将多个子文件夹/子目录重定向到一个域
- ansible - 我们可以使用没有 ansible tower 的 playbook 来跟踪 windows 中无法访问的主机吗
- python - Python string.replace() 函数仅替换字符串的一半