Hello,
Try the following.
dd <- read.table(text="
chrom length
1 chr1 249250621
2 chr2 243199373
[... etc ...]
22 chr19 59128983
23 chr22 51304566
24 chr21 48129895
", header=TRUE)
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)
barplot(dd$length[ord], names.arg=dd$chrom[ord])
Note that it uses base graphics. Do you really need ggplot2? If the goal
is to make it prettier but it wasn't working, you still don't know where
you can go with the simpler...
Hope this helps,
Rui Barradas
Em 21-06-2012 17:33, Kevin Parrish escreveu:
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
9 chr8 146364022
10 chr9 141213431
11 chr10 135534747
12 chr11 135006516
13 chr12 133851895
14 chr13 115169878
15 chr14 107349540
16 chr15 102531392
17 chr16 90354753
18 chr17 81195210
19 chr18 78077248
20 chr20 63025520
21 chrY 59373566
22 chr19 59128983
23 chr22 51304566
24 chr21 48129895
I want to drop the "chr" and order the chrom column in ascending order with
X & Y at the end.
I have tried converting the chrom into a character vector and using the
strsplit function, but then I have to circle back and do a lot of editing
to make it work.
I could do this in excel or some other program, but again I'm really trying
to learn R and if I had to do something 100 times working in R would be so
much better. I did however write the above to an excel spreadsheet and did
the simple edits I need to make a bar chart. Using bar plot I was able to
make a nice simple figure, but it isn't necessarily as aesthetically
pleasing as the figures made in ggplot2 or other packages. So when I made
the same figure in ggplot2 it worked, but reordered the chrom column as 1,
10, 11, 12......X,Y. I figured out this is because the package orders
things in an a-b-c type of fashion. I could resort the column as a factors
and manually define the levels, but again I'm trying to learn R and make it
work more efficiently for my purposes in the future.
Could someone help me understand how I could accomplishing taking the above
data.frame, splitting it / dropping chr, ordering it, and then graphing it?
Thank you in advance for your help. I've tried working on this for the past
day and a half but can't figure it out, and the learning phase is really
costing me in terms of being productive.
[[alternative HTML version deleted]]
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.