bar chart
I am having an issue producing a side-by-side bar plot of two datasets in R. I previously used the code below to create a plot which had corresponding bars from each of two datasets juxtaposed side by side, with columns from dataset 1 colored red and from dataset 2 colored blue. Now when I run the same code on any pair of datasets, including the originals which are still untouched in my saved workspace, I get separate plots for each dataset, side by side, in which individual columns alternate between red and blue between bins from the dataset. Documentation is not giving (me) any (obvious) clues as to what I've done to change the display. Please help!
## 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)
Data files contain only a single column of integers.
See if this is more or less what you want. It uses ggplot2
, but could be adapted for barplot
if you prefer:
set.seed(47)
BG.restricted.hs = round(runif(100, min = 47, max = 1660380))
FG.hs = round(runif(100, min = 0, max = 1820786))
We combine the vectors into one column (keeping track of their source in another column) so that we can simultaneously bin both of them.
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))
Plot:
library(ggplot2)
ggplot(dat, aes(x = bin, fill = source)) +
geom_bar(position = "dodge") +
theme_bw() +
scale_x_discrete(breaks = NULL)
链接地址: http://www.djcxy.com/p/30956.html
上一篇: heatmap3如何从价值转变为色彩?
下一篇: 条形图