Mixing a line chart and point points in a base chart in R

I built a chart in R with two series, but I want to add a color bar at the bottom of the chart:

enter image description here

Data to be displayed

2013-01-01 12:35:00      0
2013-01-01 12:45:00      1
2013-01-01 13:00:00      1
....
2013-01-01 13:00:00      2

where 0 is green, 1 is orange, and 2 is red. Date and time match the X data in the source table.

This is the code for the chart (no color bar):

datos_tem <- dbGetQuery(connection, paste("SELECT temp_int,hum_int,datetime FROM datalog_v2 WHERE host_id=41 and datetime>='2014-02-01 00:00:00' and datetime<='2014-02-01 23:59:00';", sep=""))
dbDisconnect(connection)

datos_tem$datetime <- as.POSIXct(datos_tem$datetime)
datos_tem$temp_int <- as.numeric(datos_tem$temp_int)
datos_tem$hum_int <- as.numeric(datos_tem$hum_int)


#gg <- qplot(datos_tem$datetime, datos_tem$temp_int) + geom_line()        # first line
#gg <- gg + geom_line(aes( x=datos_tem$datetime, y=datos_tem$hum_int )) # add the second line!

png(file.path("/tmp/", paste("comp",".png",sep="_")))
Molten <- melt(datos_tem, id.vars = "datetime")
ggplot(Molten, aes(x = datetime, y = value, colour = variable)) + geom_line() +
  scale_y_continuous(limits=c(0, 100)) +
   xlab("Tiempo") +
   ylab("Temperatura --- (ºC) y Humedad (%)")+ 
  geom_line(size=1.9)+
 scale_color_manual(values=c("#FF0000", "#0000FF"), 
                       name="Medidas",
                       labels=c("Temperature", "Humidity"))

So, I want to add something like my example to my code. Is it possible?

Data for rows:

      temp_int  hum_int            datetime
      11.6      76.8     2014-02-01 00:00:00
      11.4      77.8     2014-02-01 00:15:00
      11.3      79.4     2014-02-01 00:30:00
      .....

And the data for the panel below:

datetime                DPV
2013-01-01 12:35:00      0
2013-01-01 12:45:00      1
2013-01-01 13:00:00      1
....
2013-01-01 13:00:00      2

It's better!! I changed my data and now I have:

datetime,temp_int,hum_int,dpv
"2014-02-15 00:00:00",67.2,13.6,"red"
"2014-02-15 00:15:00",63.4,13.8,"yellow"
"2014-02-15 00:30:00",61.2,14.2,"green"
"2014-02-15 00:45:00",60.4,14.5,"green"
....
+3
source share
1 answer

It's hard to answer without evidence, but there are some ideas to get you started.

, x, temp id , .

set.seed(1)
df<-data.frame(x=1:100,temp=runif(100,10,50),id=sample(1:3,100,replace=TRUE))

geom_tile() y 0 id fill=. , . , geom_tile() y.

ggplot(df,aes(x))+geom_line(aes(y=temp))+
  geom_tile(aes(y=0,fill=factor(id)))

- geom_bar() stat="identity" y , . width= , , .

ggplot(df,aes(x))+geom_line(aes(y=temp))+
  geom_bar(aes(y=4,fill=factor(id)),stat="identity",width=1)

enter image description here

UPDATE - OP

, .

df<-read.table(text="datetime,temp_int,hum_int,dpv
        2014-02-15 00:00:00,67.2,13.6,red
        2014-02-15 00:15:00,63.4,13.8,yellow
        2014-02-15 00:30:00,61.2,14.2,green
        2014-02-15 00:45:00,60.4,14.5,green",header=T,sep=",")

datetime POSIXct.

df$datetime <- as.POSIXct(df$datetime)

.

library(reshape2)
df.melt<-melt(df,id.vars=c("datetime","dpv"))

. colour= aes() geom_line(), ggplot(). geom_bar() dpv fill=, scale_fill_identity(), dpv . , , width=900. 900, 15 , 900 ( 1 ).

ggplot(df.melt, aes(x = datetime, y = value)) + 
  geom_line(aes(colour = variable),size=1.9) +
  geom_bar(aes(y=4,fill=dpv),stat="identity",width=900)+
  scale_fill_identity()

enter image description here

+3

All Articles