Skip to content

column and line graphs in R

8 messages · Gian Maria Niccolò Benucci, John Kane, Marc Girondot +1 more

#
Le 14/03/13 11:05, Gian Maria Niccol? Benucci a ?crit :
You should be more precise about what sort of graph you want. A 
bivariate plot ?
For example, here is a bivariate from a matrix object:
fungal <- matrix(c(12, 54, 65, 76, .2, .6, .1, .7), nrow=4)
plot(fungal, xlab="Abondance", ylab="Frequency", bty="n", xlim=c(0,80))

(I don't understand the difference between relative abondance and 
frequency).
Sincerely,

Marc Girondot
#
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
____________________________________________________________
FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!
#
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=======================#
____________________________________________________________
FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!
#
When you send data, use dput() to send them. It is much more easy for 
people who want to help you.
Here is an example. I am not sure if it is what you want but you can 
play with the code.
Sincerely

Marc

fungal <- structure(list(rel.abund = c(0.003, 0.029, 0.033, 0.023, 0.009,
0.042, 0.069, 0.059, 0.034, 0.049, 0.084, 0.015, 0.059, 0.032,
0.142, 0.031, 0.034, 0.01, 0.011, 0.004, 0.034, 0.182), rel.freq = c(0.083,
0.167, 0.167, 0.083, 0.083, 0.25, 0.083, 0.167, 0.083, 0.083,
0.333, 0.083, 0.083, 0.167, 0.25, 0.083, 0.083, 0.083, 0.083,
0.083, 0.333, 0.417)), .Names = c("rel.abund", "rel.freq"),
class = "data.frame", row.names = c("MOTU2",
"MOTU4", "MOTU6", "MOTU7", "MOTU9", "MOTU11", "MOTU14", "MOTU16",
"MOTU17", "MOTU18", "MOTU19", "MOTU20", "MOTU21", "MOTU22", "MOTU23",
"MOTU24", "MOTU25", "MOTU29", "MOTU30", "MOTU33", "MOTU36", "MOTU34"
))

premar <- par("mar")
par(mar=c(5,4,4,4)+0.1)

plot(fungal[,1], type="h", lwd=20, lend=2, bty="n", xlab="", 
ylab="Relative abundance", xaxt="n", ylim=c(0,0.2))
par(xpd=TRUE)
segments(-2.5, 0.01, -2.5, 0.03, lwd=20, lend=2, col="black")

par(new=TRUE)
plot(fungal[,2], type="p", bty="n", pch=16, col="red", axes=FALSE, 
xlab="", ylab="", main="", ylim=c(0,0.5))
axis(1, at=1:length(rownames(fungal)), labels=rownames(fungal), las=2)
axis(4)
mtext("Relative frequency", side=4, line=3)
points(25.6, 0.1, pch=16, col="red")

par(mar=premar)


Le 14/03/13 15:40, Gian Maria Niccol? Benucci a ?crit :

  
    
#
On 03/15/2013 01:40 AM, Gian Maria Niccol? Benucci wrote:
Hi Gian,
You can do this in twoord.plot like this (data is named "nat_af" and the 
first column is labeled "label"):

twoord.plot(1:22-0.2,nat_af$rel.abund,1:22+0.2,nat_af$rel.freq,
  type=c("bar","bar"),lylim=c(0,0.19),rylim=c(0,0.43),halfwidth=0.2,
  main="Abundance and frequency",ylab="Abundance",rylab="Frequency",
  xticklab=rep("",22))
staxlab(1,at=1:22,labels=nat_af$label,cex=0.8,srt=45)

Jim