21 Jun 2012 18:33

## Simple Question?

```Greetings,

I am new to R, but trying to put in the time to learn. I have read the R
manual and several other introductory texts; however, there is nothing like
actually putting it into practice. So here is my problem, and its more of a
learning exercise for myself than anything else, but I'm stuck and getting
extremely frustrated that I can't figure it out.

I'm trying to make a fairly simple figure, but also trying to make it look
"better" using ggplot2.

I have data.frame that contains

chrom    length

1   chr1 249250621

2   chr2 243199373

3   chr3 198022430

4   chr4 191154276

5   chr5 180915260

6   chr6 171115067

7   chr7 159138663

8   chrX 155270560
```

21 Jun 2012 19:11

### Re: Simple Question?

```??
ggplot did not reorder the columns -- it just plotted them in the order of
the levels of factor(chrom), which is exactly what you wanted afaics. There
is no need to reorder to do what you want.

Comment: ?dput to put reproducible data into an email that people can
conveniently copy and paste into R.

However, if you wish to do so: (untested since you did not use dput)
Assume your data.frame is named df

1. First change chrom into a character column:
chrm <- as.character(df\$chrom)

2. Use regular expressions to get rid of the chr.
df\$chrom <- sub("chr","",chrm)

3. Use ?order to reorder (since the default ordering is what you want at
least in English locales)
chrom <- chrom[order(df\$chrom,]

Does this give you what you wanted?

-- Bert

On Thu, Jun 21, 2012 at 9:33 AM, Kevin Parrish <kparrish0 <at> gmail.com> wrote:

> Greetings,
>
> I am new to R, but trying to put in the time to learn. I have read the R
```

21 Jun 2012 20:15

### Re: Simple Question?

```Hello,

Try the following.

chrom    length

1   chr1 249250621

2   chr2 243199373

[... etc ...]

22 chr19  59128983

23 chr22  51304566

24 chr21  48129895

str(dd)
dd\$chrom <- as.character(dd\$chrom)
dd\$chrom <- sub("chr", "", dd\$chrom)

# This 'chrom' is NOT a data.frame column
chrom <- dd\$chrom
chrom[chrom == "X"] <- 100 # bigger than any dd\$chrom
chrom[chrom == "Y"] <- 200 # bigger than for dd\$chrom == "X"
chrom <- as.integer(chrom)
ord <- order(chrom)
```