我在R中生成两个数据集的并排条形图时出现问题。我之前使用下面的代码创建了一个图表,该图表具有并排排列的两个数据集中每个数据集的相应条形图。数据集1为红色,数据集为2蓝色。现在,当我在任何一对数据集上运行相同的代码,包括在我保存的工作空间中仍未触及的原始数据时,我会并排显示每个数据集的单独图表,其中各个列在红色和蓝色之间交替显示。数据集。文档没有给我(我)任何(明显的)线索,我已经做了什么来改变显示。请帮忙!
## Sample data
set.seed(47)
BG.restricted.hs = round(runif(100, min = 47, max = 1660380))
FG.hs = round(runif(100, min = 0, max = 1820786))
BG.restricted.hs <- data.matrix(BG.restricted.hs, rownames.force = NA)
groups.bg.restricted.hs <- cut(x=BG.restricted.hs, breaks = seq(from = 0, to = 1900000, by = 10000))
rowsums.bg.restricted.hs <- tapply(BG.restricted.hs, groups.bg.restricted.hs, sum)
norm.bg.restricted.hs <- (rowsums.bg.restricted.hs / nrow(BG.restricted.hs))
FG.hs <- data.matrix(FG.hs, rownames.force = NA)
groups.fg.hs <- cut(x=FG.hs, breaks = seq(from = 0, to = 1900000, by = 10000))
rowsums.fg.hs <- tapply(FG.hs, groups.fg.hs, sum)
norm.fg.hs <- (rowsums.fg.hs / nrow(FG.hs))
data <- cbind(norm.fg.hs, norm.bg.restricted.hs)
barplot(height = data, xlab = "TSS Distance", ylab = "Density", col=c("red","blue"), beside = TRUE)
数据文件只包含一列整数。
答案 0 :(得分:0)
看看这或多或少是你想要的。它使用ggplot2
,但如果您愿意,可以针对barplot
进行调整:
set.seed(47)
BG.restricted.hs = round(runif(100, min = 47, max = 1660380))
FG.hs = round(runif(100, min = 0, max = 1820786))
我们将向量组合成一列(在另一列中跟踪它们的来源),以便我们可以同时对它们进行分类。
dat = data.frame(x = c(BG.restricted.hs, FG.hs),
source = c(rep("BG", length(BG.restricted.hs)),
rep("FG", length(FG.hs))))
dat$bin = cut(dat$x, breaks = seq(from = min(dat$x), to = max(dat$x), by = 10000))
简介:
library(ggplot2)
ggplot(dat, aes(x = bin, fill = source)) +
geom_bar(position = "dodge") +
theme_bw() +
scale_x_discrete(breaks = NULL)