[英]setting consistent x-axis intervals for bar-plot using R
如何获得R来为条形图的x轴输出一致的起点,终点和间隔? 我有两组数据(矩阵),我想从220开始,在360结束,并且具有相同的刻度线间隔。 绘制图形的目的是轻松比较两个数据集。 所附示例显示了两个图,其中的条形图位于274,但是每个图的比例尺略有不同,因此,条形图没有排列。
barplot(as.matrix(new_data), ylim = c(0,topmax), ylab = "Reads",
col = rainbow(30), cex.lab=1.5, cex.axis=1.5, cex.sub=1.5, col.lab = "blue")
axis(1, seq(220,360))
这是我正在使用的数据矩阵的示例。 假设两个数据集的矩阵相同。
clonename = c("IGH_V4", "IGH_V2", "IGH_V8", "IGH_V7")
readlength = c(456, 654, 457, 345)
P <- matrix(c(0,55,0,65,0,0,4,100,0,0,67,6,0,56,0,0), nrow = 4, byrow = TRUE, dimnames = list(clonename, readlength))
print(P)
提前谢谢你的帮助
我敢肯定有一种方法可以用来处理基本的R图形,这是您用来制作绘图的。 但是,我发现ggplot2软件包使这种事情变得容易得多。 在下面的示例中,我将您提供的数据用作矩阵,然后
library(dplyr) # for mutate and tibble
library(reshape2) # for melt function
library(ggplot2) # for plotting
# Data as you provided
rownames = c("IGH_V4", "IGH_V2", "IGH_V8", "IGH_V7")
colnames = c(456, 654, 457, 345)
reads.m <- matrix(c(0,55,0,65,0,0,4,100,0,0,67,6,0,56,0,0),
nrow = 4, byrow = TRUE,
dimnames = list(rownames, colnames))
# Melt the matrix into the shape of a dataframe
reads.m <- melt(reads.m)
# Convert the matrix to a data frame (tibble is a type of data frame with
# some nice extra features
reads.df <- as_tibble(reads.m)
# Name the columns
names(reads.df) <- c("Sequence", "Clone", "Reads")
# Create a data frame called diluted with an extra column indicating its
# concentration
diluted.df <- mutate(reads.df, Concentration = "Diluted")
# Do the same for undiluted aka straight then add a clone to change the
# range of clones. Because clones are on the x-axis of your plot, having
# different ranges of data is what presents the problem you're trying to
# solve by aligning the plot axes.
straight.df <- mutate(reads.df,
Concentration = "Straight",
Reads = round(Reads*1.2))
straight.df <- bind_rows(straight.df, tibble(Sequence = NA,
Clone = 700,
Reads = 8,
Concentration = "Straight"))
# Concatenate the tables for the two dilutions.
reads.df <- bind_rows(diluted.df, straight.df)
# Sanity check
print(reads.df, n = nrow(reads.df))
# # A tibble: 33 x 4
# Sequence Clone Reads Concentration
# <fct> <dbl> <dbl> <chr>
# 1 IGH_V4 456 0 Diluted
# 2 IGH_V2 456 0 Diluted
# 3 IGH_V8 456 0 Diluted
# 4 IGH_V7 456 0 Diluted
# 5 IGH_V4 654 55 Diluted
# 6 IGH_V2 654 0 Diluted
# 7 IGH_V8 654 0 Diluted
# 8 IGH_V7 654 56 Diluted
# 9 IGH_V4 457 0 Diluted
# 10 IGH_V2 457 4 Diluted
# 11 IGH_V8 457 67 Diluted
# 12 IGH_V7 457 0 Diluted
# 13 IGH_V4 345 65 Diluted
# 14 IGH_V2 345 100 Diluted
# 15 IGH_V8 345 6 Diluted
# 16 IGH_V7 345 0 Diluted
# 17 IGH_V4 456 0 Straight
# 18 IGH_V2 456 0 Straight
# 19 IGH_V8 456 0 Straight
# 20 IGH_V7 456 0 Straight
# 21 IGH_V4 654 66 Straight
# 22 IGH_V2 654 0 Straight
# 23 IGH_V8 654 0 Straight
# 24 IGH_V7 654 67 Straight
# 25 IGH_V4 457 0 Straight
# 26 IGH_V2 457 5 Straight
# 27 IGH_V8 457 80 Straight
# 28 IGH_V7 457 0 Straight
# 29 IGH_V4 345 78 Straight
# 30 IGH_V2 345 120 Straight
# 31 IGH_V8 345 7 Straight
# 32 IGH_V7 345 0 Straight
# 33 NA 700 8 Straight
ggplot(reads.df, aes(x = Clone, y = Reads, fill = Concentration)) +
geom_col(width = 4) +
facet_grid(rows = vars(Concentration)) +
theme_bw() + # White background, rather than default grey
ylab("Reads")
ggplot(reads.df, aes(x = Clone, y = Reads, fill = Concentration)) +
geom_col(width = 5) +
facet_wrap(facets = vars(Concentration)) +
theme_bw() + # White background, rather than default grey
ylab("Reads")
注意:ggplot返回的警告如下所示:
# Warning messages:
# 1: position_stack requires non-overlapping x intervals
# 2: position_stack requires non-overlapping x intervals
这是手动设置geom_col宽度的结果。 如果不手动设置该宽度,则列的宽度是如此之窄以至于难以阅读。 使它们变宽不会导致它们重叠,因此我们可以忽略警告。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.