[R] column and line graphs in R

John Kane jrkrideau at inbox.com
Thu Mar 14 16:27:04 CET 2013


 
 The easiest way to supply data  is to use the dput() function.  Example with your file named "testfile": 
dput(testfile) 
Then copy the output and paste into your email.  For large data sets, you can just supply a representative sample.  Usually, 
dput(head(testfile, 100)) will be sufficient.    

Generally speaking two y-axis scales are to be avoided if at all possible. Faceting is likely to give you better results although I see that the scale differences are annoying large. It is possible to plot the two facets of the graph independently in order to have two independent y-axes but it takes more work and may or may not be needed

Here is a possible approach based on ggplot2 . You will probably have to install ggplot2 and reshape2 using install.packages()  Notice I've changed your variable names around and turned your data into a dataframe with the matrix row.names as another variable.

##===================begin code======================#

library(reshape2)
 library(ggplot2)
  
  dat1<-read.table(text="
        place       abund     freq
        MOTU2      0.003    0.083
        MOTU4      0.029    0.167
        MOTU6      0.033    0.167
        MOTU7      0.023    0.083
        MOTU9      0.009    0.083
        MOTU11     0.042    0.250
        MOTU14     0.069    0.083
        MOTU16     0.059    0.167
        MOTU17     0.034    0.083
        MOTU18     0.049    0.083
        MOTU19     0.084    0.333
        MOTU20     0.015    0.083
        MOTU21     0.059    0.083
        MOTU22     0.032    0.167
        MOTU23     0.142    0.250
        MOTU24     0.031    0.083
        MOTU25     0.034    0.083
        MOTU29     0.010    0.083
        MOTU30     0.011    0.083
        MOTU33     0.004    0.083
        MOTU36     0.034    0.333
        MOTU34     0.182    0.417
        ",sep="",header=TRUE,stringsAsFactors=FALSE)
str(dat1)

  dm1  <-  melt(dat1, id = "place",
          variable.name="type", value.name="freq")
  str(dm1)
  
# plot first alternative
  ggplot(dm1, aes(place, freq, colour = type, group = type )) + geom_line(group = 1) +
    facet_grid(type ~ . )
  # or plot second alternative.
  ggplot(dm1, aes(place, freq, colour = type, group = type )) + geom_line(group = 1) +
    facet_grid(. ~ type )
     
  ##====================end code=======================#


> -----Original Message-----
> From: gian.benucci at gmail.com
> Sent: Thu, 14 Mar 2013 15:40:53 +0100
> To: r-help at r-project.org
> Subject: Re: [R] column and line graphs in R
> 
> Hi again,
> 
> Thank you all for your support. I would love to have a graph in which two
> variables are contemporary showed. For example a histogram and a curve
> should be the perfect choice. I tried to use twoord.plot() but I am not
> sure I understand how to manage the the arguments lx, ly, rx, ry...
> Anyway
> these are my data:
> 
>> nat_af
>        rel.abund rel.freq
> MOTU2      0.003    0.083
> MOTU4      0.029    0.167
> MOTU6      0.033    0.167
> MOTU7      0.023    0.083
> MOTU9      0.009    0.083
> MOTU11     0.042    0.250
> MOTU14     0.069    0.083
> MOTU16     0.059    0.167
> MOTU17     0.034    0.083
> MOTU18     0.049    0.083
> MOTU19     0.084    0.333
> MOTU20     0.015    0.083
> MOTU21     0.059    0.083
> MOTU22     0.032    0.167
> MOTU23     0.142    0.250
> MOTU24     0.031    0.083
> MOTU25     0.034    0.083
> MOTU29     0.010    0.083
> MOTU30     0.011    0.083
> MOTU33     0.004    0.083
> MOTU36     0.034    0.333
> MOTU34     0.182    0.417
> 
> First column is the relative abundance of the given MOTU and second
> column
> is the relative frequency of the same MOTU.
> Thank you very much in advance,
> 
> --
> Gian
> 
> 
> On 14 March 2013 14:51, John Kane <jrkrideau at inbox.com> wrote:
> 
>> 
>> http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example
>> 
>> You really need to read the  posting guide and supply some sample data
>> at
>> the very least.
>> 
>> Here is about as simple minded a plot as R will do as an example however
>> 
>> dat1  <-   structure(list(abond = c(17L, 3L, 6L, 11L, 5L, 8L, 13L, 16L,
>>                15L, 2L), freq = c(17L, 14L, 7L, 13L, 19L, 5L, 3L, 20L,
>> 9L,
>> 10L
>>          )), .Names = c("abond", "freq"), row.names = c(NA, -10L),
>>                        class = "data.frame")
>> 
>> 
>>   plot(dat1$abond, col = "red")
>>   lines(dat1$freq, col= "blue")
>> John Kane
>> Kingston ON Canada
>> 
>> 
>>> -----Original Message-----
>>> From: gian.benucci at gmail.com
>>> Sent: Thu, 14 Mar 2013 11:05:40 +0100
>>> To: r-help at r-project.org
>>> Subject: [R] column and line graphs in R
>>> 
>>> Hi all,
>>> 
>>> I would love to plot my data with R. I have abundance and frequency of
>>> fungal
>>> taxonomic data that should be plotted in the same graph. In Microsoft
>>> Excel
>>> is that possible but the graphic result is, as always, very poor. Is
>>> there
>>> a function that may let me plot these data in R?
>>> I have a matrix made of two columns, on is the relative abundance and
>>> the
>>> other is the relative frequency for each of my sample.
>>> Thank you very much,
>>> 
>>> --
>>> Gian
>>> 
>>>       [[alternative HTML version deleted]]
>>> 
>>> ______________________________________________
>>> R-help at 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.
>> 
>> ____________________________________________________________
>> FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!
>> Check it out at http://www.inbox.com/earth
>> 
>> 
>> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at 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.

____________________________________________________________
FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!



More information about the R-help mailing list