1

I have a graph here: Graphs of variables from sites 2-6

dframe1$row <- row.names(dframe1)
library(reshape2)
dframe2 <- melt(dframe1, id=c("Site", "row"))
ggplot(dframe2, aes(x=as.numeric(row), fill=Site, y=value)) + geom_bar(stat="identity") +       facet_wrap(~variable, scales="free")

Basically I would like to know how I can change the colours so that they are different for each site (represented in the legend), and if it is possible to label the x and y axes individually for each mini graph and to get rid of the as numeric line at the bottom.

Here is the dput() of my data:

structure(list(Site = c(2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 
4L, 4L, 4L, 5L, 5L, 6L, 6L, 6L), weight = c(0.193, 0.249, 0.263, 
0.262, 0.419, 0.204, 0.311, 0.481, 0.326, 0.657, 0.347, 0.239, 
0.416, 0.31, 0.314, 0.277, 0.302, 0.403), cell.count = c(2530000, 
729000, 336000, 436000, 292000, 0, 2e+05, 6450000, 2e+05, 18700000, 
7430000, 9920000, 22700000, 21600000, 227000, 169000, 5e+05, 
283000), eleocyte = c(1270000, 17, 7.3, 264000, 0, 0, 2e+05, 
0, 2e+05, 2270000, 0, 9920000, 22700000, 442, 153000, 169000, 
5e+05, 283000), lambda.max = c(459L, 459L, 459L, 459L, 462L, 
462L, 462L, 462L, 462L, 465L, 465L, 465L, 465L, 490L, 490L, 475L, 
475L, 475L), avepb.ppm = c(390.2, 373.3, 340.13, 403.2, 248.53, 
206.7, 238.5, 190.6, 597.2, 206.8, 174.4, 186.3, 138.5, 269.55, 
58.1, 5.225, 4.02, 6.85), aveworm.pb.ppm = c(9.59, 9.446, 4.193, 
21.9, 1.66, 7.415, 13.11, 3.01, 51.5, 5.985, 4.705, 26.38, 2.38, 
4.44, 4.67, 0.11, 0.085, 0.096), aveworm.g = c(0.09125, 0.264, 
14.699, 0.2425, 0.4793, 0.051, 0.0635, 0.0465, 0.2645, 0.0559, 
0.0795, 0.05765, 0.0846, 0.457, 0.0625, 0.0535, 0.1576, 0.16)), .Names = c("Site", 
"weight", "cell.count", "eleocyte", "lambda.max", "avepb.ppm", 
"aveworm.pb.ppm", "aveworm.g"), class = "data.frame", row.names = c(NA, 
-18L))
LJW
  • 795
  • 2
  • 13
  • 27
KB2
  • 101
  • 1
  • 1
  • 11
  • 1
    which column ist __row__ i get this error: `Error: id variables not found in data: row` – Rentrop Dec 08 '14 at 14:28
  • 1
    As Floo0 mentions your code doesn't quite run, but to get a different `fill` per site you need to define it as a `factor` ie`fill=factor(Site)`. Currently it is treated as continuous (an integer). – user20650 Dec 08 '14 at 14:35
  • 1
    Apologies. This code needs to go first dframe1$row <- row.names(dframe1) before library(reshape 2). I have included it in the edit. Sorry for that. – KB2 Dec 08 '14 at 14:43

1 Answers1

2

To change the colors you need to tell ggplot that Site is a factor (as @user20650 pointed out in the comments).

You already have individual axes for the mini graphs because you specified scales = "free" in facet_wrap().

To get rid of the as.numeric(row) label at the bottom of your graph you can specify that you do not want text there with the theme() statement. You could also achieve this by adding a blank label with p <- p + xlab("") instead of the theme() line.

This code produces the following figure:

library(ggplot2)
p <- ggplot(dframe2, aes(x = as.numeric(row), fill = as.factor(Site), y = value))
p <- p + geom_bar(stat = "identity") + facet_wrap(~variable, scales = "free")
p <- p + scale_fill_discrete("Site")
p <- p + theme(axis.title.x = element_blank())
p

enter image description here

To change the labels of the facets you can change the levels of the variables in your data frame. See this answer. Similar to:

levels(dframe2$variable) <- c("Weight", "Cell Count", "Eleocyte", "Lambda",
                              "Average PPM", "Average PB PPM", "Average G")

If you use facet_grid instead of facet_wrap you can use a labeller function (in case you do not want to change your data frame). See also the discussion here.

The Cookbook for R site is also a good resource for faceting questions.

Community
  • 1
  • 1
LJW
  • 795
  • 2
  • 13
  • 27
  • Thank you very much for the answer. I mean (with regards to the mini axes), how I could add individual labels to them? I.e. so that I could specify units etc. Also I would like to change the header titles in R, I have tried using colnames() but it does not work.. – KB2 Dec 10 '14 at 22:54
  • @KB2 I updated the answer to include how to change the labels of the facets. I am not sure about the first question and do not have an easy answer. Good luck! – LJW Dec 11 '14 at 00:57