r - 在 R 中的 ggplot 中指定 x 轴值

标签 r ggplot2

我正在使用 ggplot 来绘制图,但在指定 x 轴的值时遇到问题。我希望每个样本值都显示在图表上,即 1-50。

这是代码的一小部分:

   chrom chr_start  chr_stop num_positions normal_depth tumor_depth log2_ratio gc_content sample
   324202     1 156249804 156249858            55         12.3         4.7     -1.399       34.5     10
   324203     1 156250463 156250473            11         10.0         4.6     -1.109       27.3     10
   324204     1 156250664 156250705            42         12.0         7.4     -0.704       19.0     10
   324205     1 156250816 156250847            32         11.7         4.6     -1.343       40.6     10
   324206     1 156251108 156251132            25         10.6         3.6     -1.569       60.0     10
   324207     1 156251411 156251464            54         12.3         6.8     -0.863       46.3     10

这是ggplot函数:

newHist = ggplot(resultsPileup1COMBINED[resultsPileup1COMBINED$sample <= 25,],
                 aes(x=sample)) +
  geom_histogram(fill="blue") +
  geom_histogram(data=resultsPileup1COMBINED[resultsPileup1COMBINED$sample > 25,], 
                 aes(x=sample), fill="gray50")

最佳答案

我会这样绘制:

ggplot(resultsPileup1COMBINED[resultsPileup1COMBINED$sample <= 25, ],
         aes(x=sample)) +
       geom_histogram(fill = "blue",binwidth = 1) +
       geom_histogram(data = resultsPileup1COMBINED[resultsPileup1COMBINED$sample > 25, ], 
         aes(x=sample), fill = "gray50", binwidth = 1) + 
       scale_x_continuous(limits = c(0, 50), breaks = 0:50)

两个主要的补充是binwidth = 1以确保每个样本都有自己的列,以及scale_x_continuous以限制比例,其中breaks = 0:50调用手动标记轴

以下是用于测试第二个绘图调用的 40 秒数据:

dput(resultsPileup1COMBINED)

structure(list(chrom = c(1L, 1L, 1L, 1L, 1L, 1L), chr_start = c(156249804L, 
156250463L, 156250664L, 156250816L, 156251108L, 156251411L), 
    chr_stop = c(156249858L, 156250473L, 156250705L, 156250847L, 
    156251132L, 156251464L), num_positions = c(55L, 11L, 42L, 
    32L, 25L, 54L), normal_depth = c(12.3, 10, 12, 11.7, 10.6, 
    12.3), tumor_depth = c(4.7, 4.6, 7.4, 4.6, 3.6, 6.8), log2_ratio = c(-1.399, 
    -1.109, -0.704, -1.343, -1.569, -0.863), gc_content = c(34.5, 
    27.3, 19, 40.6, 60, 46.3), sample = c(10L, 10L, 10L, 10L, 
    40L, 40L)), .Names = c("chrom", "chr_start", "chr_stop", 
"num_positions", "normal_depth", "tumor_depth", "log2_ratio", 
"gc_content", "sample"), class = "data.frame", row.names = c("324202", 
"324203", "324204", "324205", "324206", "324207"))

关于r - 在 R 中的 ggplot 中指定 x 轴值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31011987/

相关文章:

r - ggplot 无法绘制最小点

R:如何将我的数据格式化为多项式 Logit?

r - 如何找到值低于 0 的 5 行或更多行(日期)系列的第一行(日期)

r - ggplot2:每层单独的图例

r - 使用 ggplot 移动绘图

r - 在 Windows 10 上安装旧版本的 R

r - 具有特定形状的文字云

r - ggplot scale_alpha_manual 用于数据中的离散变量和连续变量

r - 在ggplot中绘制具有不同日期的时间线的问题

r - 根据条件为 geom_text 中的文本添加颜色