0

I am very new to plotting data and I am trying to add a third factor to my bwplot.

My factors are:

  • Site with 6 levels (C0, C1, C2, C3, C4, C5)
  • Seasonal Flow with 2 levels (High, Low)
  • Land Use with 2 levels (Urban, Rural)

I have no trouble plotting the Site and Seasonal Flow with the script below:

C <- read.csv('Collie 3.csv')

library(lattice)

bwplot(TN.TP~ Site| Seasonal.Flow,data=C,main="Collie River TN:TP",ylab="ratio",xlab="Site + Flow regime", horizontal=FALSE)

Datalink: https://www.dropbox.com/s/6q8b1svld61pqsr/Collie%203.csv

My problem is that I would like to also include Land Use on the x-axis, where Sites C0 and C1 are Rural and Sites C2, C3, C4, C5 are Urban. I have looked at ggplot, but still haven't figured it out. Even just colouring C0 and C1 differently would help.

2 Answers2

0

Here's a solution with ggplot2.

library(ggplot2)
ggplot(data = C, aes(x = Site, y = TN.TP, colour = Land.Use)) +
  facet_wrap( ~ Seasonal.Flow, ncol = 1) +
  geom_boxplot() +
  scale_colour_hue("Land use") +
  ggtitle("Collie River TN:TP") +
  ylab("ratio") +
  xlab("Site + Flow regime")

enter image description here

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
  • Excellent, thank you for that. Is there a simple was to change the colours of the boxplot? For some reason I get and error with the ggtitle you supplied. Error in ggtitle("Collie River TN:TP") + ylab("ratio") : non-numeric argument to binary operator Thank you once again Sven – user2479514 Jun 13 '13 at 16:22
  • @user2479514 1) You can specify your own colours with `scale_color_manual`. This replaces `scale_color_hue`. For example, you can try `scale_color_manual("Land use", values = c("blue", "red"))`. 2) I forgot a `+` sign at the end of the line preceding the one with `ggtitle`. Now it should work. – Sven Hohenstein Jun 13 '13 at 17:22
0

Here is a lattice solution:

library("lattice")
C <- read.csv('Collie 3.csv')

bwplot(TN.TP~ Site| Seasonal.Flow,data=C,main="Collie River TN:TP",
   ylab="ratio",xlab="Site + Flow regime", horizontal=FALSE, 
   groups = Land.Use, auto.key=TRUE,
   panel = panel.superpose,
   panel.groups = panel.bwplot)

Using layout()allows you to specify the number of columns and rows:

bwplot(TN.TP~ Site| Seasonal.Flow,data=C,main="Collie River TN:TP",
   ylab="ratio",xlab="Site + Flow regime", horizontal=FALSE, 
   groups = Land.Use, auto.key=TRUE,
   panel = panel.superpose,
   panel.groups = panel.bwplot,
   layout = c(1,2))  # same layout as ggplot2 graph

Or, if you like to plot them truly separated, try something like this: (look the third factor is added to the "formula")

bwplot(TN.TP~ Site| Seasonal.Flow * Land.Use ,data=C,
main="Collie River TN:TP", ylab="ratio",xlab="Site + Flow regime",
horizontal=FALSE)
Konn
  • 108
  • 6