0

I'm trying to do a plot from a file generated with the vcftools program at linux. The file looks like:

CHROM   BIN_START   SNP_COUNT   VARIANTS/KB
chr0    0       46  4.6
chr0    10000   34  3.4
chr0    20000   0   0
chr0    30000   21  2.1
chr0    40000   15  1.5
chr0    50000   22  2.2
chr0    60000   22  2.2
chr0    70000   9   0.9
chr0    80000   20  2
chr0    90000   17  1.7
chr0    100000  17  1.7
chr1    0       87  8.7
chr1    10000   96  9.6
chr1    20000   90  9
chr1    30000   124 12.4
chr1    40000   60  6
chr1    50000   89  8.9
chr1    60000   80  8
chr1    70000   170 17
chr1    80000   152 15.2
chr1    90000   98  9.8
chr1    100000  99  9.9
chr2    0       65  6.5
chr2    10000   97  9.7
chr2    20000   68  6.8
chr2    30000   83  8.3
chr2    40000   67  6.7
chr2    50000   76  7.6
chr2    60000   91  9.1
chr2    70000   62  6.2
chr2    80000   40  4
chr2    90000   32  3.2
chr2    100000  45  4.5

With that file I want to create a grouped ggplot with chr0, chr1, chr2 at x-axis and the SNP_COUNT value at y-axis. I tried this:

filepath <- "/example/ggplot.txt"
df <- read.table(filepath, sep = "\t")
ggplot(df, aes(CHROM, SNP_COUNT))

With that code I get the correct distribution:enter image description here

But the ggplot is empty, any advice would be appreciated thanks.

My desired ggplot with my values:

enter image description here

zx8754
  • 52,746
  • 12
  • 114
  • 209
J0ki
  • 302
  • 2
  • 20
  • You need to add a `geom_*`, e.g. `+ geom_bar(stat = "identity")`. See [here](http://ggplot2.tidyverse.org/reference/geom_bar.html) for examples. – Maurits Evers Apr 20 '18 at 09:29
  • `Error: stat_count() must not be used with a y aesthetic.` – J0ki Apr 20 '18 at 09:33
  • `ggplot(x, aes(CHROM, SNP_COUNT)) + geom_bar(stat = "identity")` not grouped like desired ggplot and wrong values at y-axis – J0ki Apr 20 '18 at 09:42
  • Are you looking for this? `ggplot(my_df, aes(SNP_COUNT)) + geom_histogram(aes(y = ..density..), binwidth = 1) + facet_wrap(~CHROM)` Please specify the grouping variable you want to have.... or better read the [ggplot2-reference](http://ggplot2.tidyverse.org/) – kath Apr 20 '18 at 09:47
  • 1
    You will have much more success if you read the basics of `ggplot2` first. – Jack Brookes Apr 20 '18 at 09:55
  • @kath Thanks but I was looking for something like my empty file, chrom at x-axis and snp_count at y-axis. – J0ki Apr 20 '18 at 10:02
  • Then was the issue with @MauritsEvers solution? – kath Apr 20 '18 at 10:03
  • @JackBrookes I read them already but still get the error at 2nd comment – J0ki Apr 20 '18 at 10:03
  • @kath No because I don't get a grouped histograms or points or anything. And the values differs from desired. – J0ki Apr 20 '18 at 10:05

1 Answers1

0

Thanks to @kath @MauritsEvers comments I did:

ggplot(x, aes(BIN_START, SNP_COUNT)) + 
    geom_point() + facet_wrap(~CHROM)

And I get my desired output:

enter image description here

J0ki
  • 302
  • 2
  • 20