4

I'm trying to make a grouped barplot in r, but there are some things I cannot figure out. This is what I have so far:

enter image description here

I would like:

  1. to create a matrix from the data.frame (.csv file, see below)
  2. the ablines to appear, but not in front of the bars
  3. labels for the grouped bars (November, December, January, ... ->see data below)
  4. for the plot layout to be as shown below. (I basically want the plot border)

Ideal Graph

I used the following code:

x<-matrix(nrow=3,ncol=7, data=c(200,227,196,210,279,319,220,126,111,230,196,123,240,106,94,250,154,233,260,226,218))
tiff("p_month_all.tiff", width=600, height=300)
par(mar=c(5,4,0.5,0.5))
a=c("November","December","January","February","March","April","May")
barplot(x, beside=TRUE, ylim=c(0,350),xlab="Month", axes=TRUE,axis.lty=1, ylab="Monthly Precipitation [mm]", col=c("darkblue","dodgerblue3","deepskyblue1"),panel.first= abline(h = c(50,100,150,200,250,300), col = "grey", lty = 2), xaxt="n", yaxt="n")
par(ps=12, cex =1, cex.main=2)
axis(2, c(0,350, c(50, 100, 150, 200, 250, 300)), las=1)
dev.off()

The data set (.csv file) looks like this:

Month      Hornberg   Strick   Huetten
November     120       278       234
December     279       156       145
January      328       300       299
February     267       259       234
March        190       201       187
April        150       199       177
May          147       156       160
2
  • It's unclear what you want for the first point ("create matrix from data.frame"). Are you having trouble with importing the data from CSV, or with converting a data frame to a matrix? The latter can be solved with as.matrix. The other questions are about drawing the plot, so I think this needs to be in a separate question. Commented Sep 5, 2013 at 10:00
  • Importing data from .csv isn't a problem but I wanted to create a matrix where the row- and column names are included for the labels of the plot. Commented Sep 7, 2013 at 7:00

2 Answers 2

5

I've rewritten your code for clarity so you can see more easily what the problem is.

You were suppressing the axes with xaxt = "n" and yaxt = "n". I removed those lines. Adding a call to box draws the box around the plot. Adding a call to grid draws gridlines in the plot. I've added row and column names to your data matrix so the plot know what to use in the axes. I've updated the plot margins. I also tidied a few bits like replacing month names with month.name and using seq.int rather than a hard-coded sequence.

x <- matrix(
  c(
    200, 227, 196, 
    210, 279, 319, 
    220, 126, 111,
    230, 196, 123,
    240, 106, 94,
    250, 154, 233,
    260, 226, 218
  ),
  nrow = 3,
  ncol = 7
)
colnames(x) <- month.name[c(11:12, 1:5)]
rownames(x) <- c("Hornberg", "Strick", "Huetten")


par(mar = c(5, 4, 1.5, 0.5), ps = 12, cex  = 1, cex.main = 2, las = 1)

barplot(
  x, 
  beside      = TRUE, 
  ylim        = c(0,350),
  xlab        = "Month", 
  axes        = TRUE,
  axis.lty    = 1, 
  ylab        = "Monthly Precipitation [mm]",
  col         = c("darkblue", "dodgerblue3", "deepskyblue1"),
  panel.first =  abline(
    h    =  seq.int(50, 300, 50), 
    col  =  "grey", 
    lty  =  2
  )
)
box()
grid()
Sign up to request clarification or add additional context in comments.

1 Comment

@samjam If you found the answer useful, you can click the up arrow on the left to upvote it. You can do this for more than one answer.
2

So, first of all, look through ggplot2 documentation, it's pretty good http://docs.ggplot2.org/0.9.3.1/index.html If you haven't found an answer for your question, never give up googling :)

Ok, about your question:

  1. Create data

help(read.csv) -> import your data to data.frame named x Prepare data for the plot:

Melt your data to use it for the plot

    x<-melt(x)
  1. Use Month variable as a factor and order by month:

    x$Month=factor(x$Month,level=month.name) 
    x<-x[order(x$Month),]
    
  2. Plot the graph using ggplot2 (as you tagged it here and it's straitforward in use)

        ggplot(x,aes(x=Month,y=value,fill=variable))+geom_bar(stat="bin",position="dodge")+theme_bw()+ylab("Monthly Precipitation [mm]")+xlab("Month")
    

For the colours, can use scale_fill_brewer() (great tutorials here:http://www.cookbook-r.com/Graphs/Colors_%28ggplot2%29/)

ggplot(x,aes(x=Month,y=value,fill=variable))+geom_bar(stat="bin",position="dodge")+theme_bw()+ylab("Monthly Precipitation [mm]")+xlab("Month")+scale_fill_brewer(palette="Blues")

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.