ggplot在X轴上具有分类变量的R重叠线图

时间:2018-09-11 11:29:08

标签: r ggplot2 linegraph

在尝试绘制基本线图时出现了我不明白的错误。我敢肯定有一个简单的解决方法。这是我的df(Chrom有24个值,一些数字和一些字母)。

> df
# A tibble: 375 x 4
   Sample BasesCovered FractionOfTotal Chrom
   <chr>         <int>           <dbl> <chr>
 1 AE        169850837           0.682 1    
 2 BE        112368817           0.451 1    
 3 HE        116402736           0.468 1    
 4 C         142399396           0.572 1    
 5 AE:BE      93870879           0.377 1    
 6 AE:HE      98319854           0.395 1    
 7 AE:C      108852071           0.437 1    
 8 BE:HE      69040576           0.277 1    
 9 BE:C       72772760           0.292 1    
10 HE:C       74645628           0.300 1    
# ... with 365 more rows

这是我根据此line graph example

进行绘图的代码

ggplot(df, aes(y="FractionOfTotal", X="Chrom", group=1)) +
  geom_line(aes(colour="Sample"))

ggplot(df, aes(y="FractionOfTotal", X="Chrom")) +
 geom_line(aes(group=1))

ggplot(df, aes(y="FractionOfTotal", X="Chrom", colour="Sample")) +
  geom_line()

这些尝试中的每一个都会引发此错误:

Error in order(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,  : 
  argument 3 is not a vector

我不确定参数3是什么?因此,我尝试了一下,但没有帮助:

> is.vector(df$Sample)
[1] TRUE
> is.vector(df$Chrom)
[1] TRUE
> is.vector(df$FractionOfTotal)
[1] TRUE

我试图搜索错误并找到了this,但在这种情况下,df似乎有问题,我看不到是我的情况。

我确定这很简单吗?

1 个答案:

答案 0 :(得分:1)

根据我上面的评论,我实际上不确定您要绘制的内容,但可以进行以下操作(请注意x中的小写aes

ggplot(df, aes(y = FractionOfTotal, x = Chrom, group = 1)) +
    geom_line(aes(colour = Sample))

enter image description here


样本数据

df <- read.table(text =
    "   Sample BasesCovered FractionOfTotal Chrom
 1 AE        169850837           0.682 1
 2 BE        112368817           0.451 1
 3 HE        116402736           0.468 1
 4 C         142399396           0.572 1
 5 AE:BE      93870879           0.377 1
 6 AE:HE      98319854           0.395 1
 7 AE:C      108852071           0.437 1
 8 BE:HE      69040576           0.277 1
 9 BE:C       72772760           0.292 1
10 HE:C       74645628           0.300 1    ", header = T)