1

I'm using the following code to create a facet_grid plot:

load(url("http://ebib.com.br/dados.RData"))

library(ggplot2)
library(scales)

ggplot() + geom_step(data=subset(me, variable=="Umid. Relat."), aes(Data, value), color="blue") +
   geom_smooth(data=subset(me, variable=="Umid. Relat."), aes(Data, value), , color="blue") +
   geom_line(data=subset(me, variable=="Vel. Vento"), aes(Data, value), color="gray20") +
   geom_smooth(data=subset(me, variable=="Vel. Vento"), aes(Data, value), color="gray20") +
   geom_line(data=subset(me, variable==c("Temp. Max.", "Temp. Min.")), aes(Data, value, colour=variable)) +
   geom_smooth(data=subset(me, variable==c("Temp. Max.", "Temp. Min.")), aes(Data, value, colour=variable)) +
   geom_bar(stat="identity", data=subset(me, variable=="Precipitação"), aes(Data, value), fill="blue") +
   facet_grid(variable~., scales="free_y") +
   scale_x_date(breaks = date_breaks("months"), labels = date_format("%b")) +
   theme(legend.position="none")

Is there a way to plot the variables "Temp. Max." and "Temp. Min." in the same graph/facet?

Edit: Working code

load(url("http://ebib.com.br/dados.RData"))

library(ggplot2)
library(scales)

me$variable2 <- as.character(me$variable)
me$variable2[me$variable2 %in% c("Temp. Max.","Temp. Min.")] <- "Temp. Max./Min."

ggplot() + geom_line(data=subset(me, variable=="Umid. Relat."), aes(Data, value), color="blue") +
geom_smooth(data=subset(me, variable=="Umid. Relat."), aes(Data, value), , color="blue") +
geom_line(data=subset(me, variable=="Vel. Vento"), aes(Data, value), color="gray20") +
geom_smooth(data=subset(me, variable=="Vel. Vento"), aes(Data, value), color="gray20") +
geom_line(data=subset(me, variable==c("Temp. Max.", "Temp. Min.")), aes(Data, value, colour=variable)) +
geom_smooth(data=subset(me, variable==c("Temp. Max.", "Temp. Min.")), aes(Data, value, colour=variable)) +
geom_bar(stat="identity", data=subset(me, variable=="Precipitação"), aes(Data, value), fill="blue") +
facet_grid(variable2~., scales="free_y") +
scale_x_date(breaks = date_breaks("months"), labels = date_format("%b")) +
theme(legend.position="none")
Prradep
  • 5,506
  • 5
  • 43
  • 84
rfsaldanha
  • 493
  • 4
  • 7
  • 3
    Not a very good reproducible example. It would be better if you could give a sample of "me" dataset. See this http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Avinash Jan 03 '14 at 10:44
  • Yes, I was thinking about that. I'm editing the post right now. Thanks! – rfsaldanha Jan 03 '14 at 11:17

1 Answers1

4

You need a new variable for facetting.

The new column variable2 is almost identical to variable but there is a single value "Temp. Max./Min." for both "Temp. Max." and "Temp. Min.".

me$variable2 <- as.character(me$variable)
me$variable2[me$variable2 %in% c("Temp. Max.","Temp. Min.")] <- "Temp. Max./Min."

Use this variable as an argument for facet_grid:

facet_grid(variable2 ~., scales = "free_y")

This will produce the following plot:

enter image description here

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168