1

I'm having trouble creating grouped barplots. Have explored base graphics and lattice.

My data looks like

compound detection LUtype
a 50 ag
a 75 urban
a 34 mixed
b 89 ag
......

I'd like to create a plot with compounds on the y axis (horizontal bar plot) with the bars colored to represent the land use type and detection on the x axis. These data are stored in a data frame, which I tried converting to a matrix with as.matrix, but this doesn't work and from what I can tell, the matrix is only the row of compounds. This does not produce a plot.

bars<-data.frame(data6$compound,data6$detection,data6$LUtype)  
barsM<-as.matrix(data6$compound,data6$detection,data6$LUtype)  
barplot(barsM,horiz=TRUE,beside=TRUE)

I also tried to bypass the matrix by using lattice, by no plot here either.

library(lattice)  
require(lattice)  
barchart(data6$detection~data6$compound,groups=data6$LUtype,bars)

I'm reading this article plotting grouped bar charts in R, and I have basically the same set up, but these solutions aren't working for me.

Community
  • 1
  • 1
MeggSho
  • 11
  • 2
  • 1
    Please add data, and show (not with a link to an article) what code you tried and what did not work. See http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – dww May 26 '16 at 16:56
  • Ok, I usually input large datasets as .txt files but I created some data here for an example. `Data<-data.frame(pesticide=rep(c("a","b","c","d"),each=3),LU=rep(c("ag","urb","mixed"),4),detection=c(10,50,85,74,12,61,95,45,12,72,65,42)) write.table(Data,file="Data.txt",sep="\t",row.names=FALSE,quote=FALSE) pdf("testcode.pdf",width=11,height=8.5) require(lattice) barchart(detection~pesticide,groups=LU,Data,auto.key=list(columns=4)) dev.off()` I don't get an errors using this code but no plot is generated. I'm using the most recent version of R. – MeggSho May 27 '16 at 12:14
  • Below code made an example data pdf in my environment. `require(lattice) Data<-data.frame(pesticide=rep(c("a","b","c","d"),each=3), LU=rep(c("ag","urb","mixed"),4), detection=c(10,50,85,74,12,61,95,45,12,72,65,42)) write.table(Data,file="Data.txt",sep="\t",row.names=FALSE,quote=FALSE) d <- read.delim("Data.txt") pdf("testcode.pdf",width=11,height=8.5) barchart(pesticide ~ detection, groups=LU, d, auto.key=list(columns=3)) dev.off() ` – cuttlefish44 May 30 '16 at 20:12

0 Answers0