0

I am very, very new to R so please forgive the basic nature of my question. In short, I have done a lot of Google searching to try to answer this, but I find that even the basic guides available, and simple discussions on forums are assuming more prior knowledge than I have, especially when it comes to outlining what all of the coding terms are and what changing them means for a plot.

In short I have a tab formatted table with three columns of data that I wish to plot densities for on a single graph. I would like the lines to be different patterns (dotted, dashed etc. whatever makes it easy to tell them apart, I cannot use colours as my supervisor is colour blind).

I have code that reads in the data and makes accessible the columns I am interested in:

mydata <- read.table("c:/Users/Demon/Desktop/Thesis/Fst_all_genome.txt", header=TRUE,
sep="\t")

fstdata <- data.frame(Fst_ceu_mkk =rnorm(10),
                      Fst_ceu_yri =rnorm(10),
                      Fst_mkk_yri =rnorm(10))

Where do I go from here?

user2439887
  • 61
  • 1
  • 11

3 Answers3

0

Appendix A of 'An Introduction to R' has a nice walkthrough tutorial you can do in ten minutes; it teaches among other things about line types etc

After that, plotting densities was explained dozens of times here too; search in the search box above for eg '[r] density'. There is also the R Graph Gallery (possibly down right now) and more.

A nice, free guide I often recommend is John Verzani's simpleR which stresses graphs a lot and will teach you what you need here.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • Thank you Dirk. I will take a look. Stressful stuff! I'm on a very tight time budget (was given only six months for my thesis), and the learning curve for R is pretty steep. Wish someone would cook up a more user friendly GUI for it. – user2439887 Jun 04 '13 at 01:19
  • @user2439887 There is also the ggplot-centric [cookbook for R](http://www.cookbook-r.com/Graphs/) -- Also you can get [colorblind-safe colour palettes](http://www.mollietaylor.com/2012/10/color-blindness-and-palette-choice.html) which are available in the `RColorBrewer` package. – mnel Jun 04 '13 at 01:24
  • I don't wish to sound lazy on this, but despite the simpleR guide seeming like quite a comprehensive tool, it does beat around the bush a little too much when it comes to templates. Ideally I need these plots in the next day or so, so I really just need a quick and dirty solution, but explained in simple enough terms that I can apply it and know which variables to change or leave alone. If anyone could provide a template that I could just switch out the data vectors in, with a brief run-down of how to alter visual aspects of the plot I would be appreciative. – user2439887 Jun 04 '13 at 01:32
  • Thanks mnel - that's quite useful. – user2439887 Jun 04 '13 at 01:32
  • Hmmm. It keeps saying "ggplot does not know how to deal with class numeric". No idea what that means... – user2439887 Jun 04 '13 at 01:38
  • 1
    Note that my answer did not suggest gpplot or ggplot2. You are also on the edge of abusing this site as your vioplot question is essentially identical. – Dirk Eddelbuettel Jun 04 '13 at 01:51
  • Apologies - I had no intention to abuse the resource, that question had been answered and this was relating to a different kind of plot. I have made an appointment with a statistician on campus to see if I can find solutions to these issues. So thank you anyhow. I am also looking into using software other than R as I will need a lot more instruction in it before I can use it without a heck of a lot of assistance. – user2439887 Jun 04 '13 at 02:04
0

Two options for you to explore using high-level graphics.

# dummy data
d = data.frame(x = rnorm(10), y = rnorm(10), z = rnorm(10))

You first need to reshape the data from wide to long format,

require(reshape2)
m = melt(d)

ggplot2 graphics

require(ggplot2)
ggplot(data = m, mapping = aes(x = value, linetype = variable)) + 
        geom_line(stat = "density")

Lattice graphics

Using the same melt()ed data,

require(lattice)
densityplot( ~ value, data = m, group = variable, 
            auto.key = TRUE, par.settings = col.whitebg())
baptiste
  • 75,767
  • 19
  • 198
  • 294
0

If you need something very simple, you could do simply:

plot(density(mydata$col_1))
lines(density(mydata$col_2), lty = 2)
lines(density(mydata$col_2), lty = 3)

If the second and third density curves are far away from the first, you'll need define xy limits of the plotting region explicitly:

dens1 <- density(mydata$col_1)
dens2 <- density(mydata$col_2)
dens3 <- density(mydata$col_3)

plot(dens1, xlim = range(dens1$x, dens2$x, dens3$x),
  ylim = range(dens1$y, dens2$y, dens3$y))
lines(density(mydata$col_2), lty = 2)
lines(density(mydata$col_2), lty = 3)

Hope this helps.

René
  • 13
  • 5