2015-04-22 33 views
2

我具有類似於這兩個數據幀:在一個情節結合geom_point和geom_line

date <- c("2014-07-06", "2014-07-06","2014-07-06","2014-07-07", "2014-07-07","2014-07-07","2014-07-08","2014-07-08","2014-07-08") 
TIME <- c("01:01:01", "10:02:02", "18:03:03","01:01:01", "10:02:02", "18:03:03","01:01:01", "10:02:02", "18:03:03") 
depth <- c(12, 23, 4, 15, 22, 34, 22, 12, 5) 
temp <- c(14, 10, 16, 13, 10, 9, 10, 14, 16) 
depth.temp <- data.frame(date, TIME, depth, temp) 
depth.temp$asDate<-as.Date(depth.temp$date) 

date <- c("2014-07-06", "2014-07-07","2014-07-08") 
meandepth <- c(13, 16, 9) 
cv <- c(25, 9, 20) 
depth.cv <- data.frame(date, meandepth, cv) 
depth.cv$asDate<-as.Date(depth.cv$date) 

從第一個我已經創建以下情節:

library(ggplot2) 
p1 <- qplot(asDate, depth, data=depth.temp, colour=temp, size = I(5), alpha = I(0.3))+ scale_y_reverse() 
p1 + scale_colour_gradientn(colours = rev(rainbow(12))) 

而從第二個該曲線:

p2 <- ggplot(depth.cv, aes(x=asDate, y=meandepth))+ scale_y_reverse() 
p2 + geom_line(aes(size = cv)) 

我想將兩個圖合併成一個,在後面的點和前面的行中,任何含義stions?請注意,點和線不是從相同的數據而是從兩個不同的數據幀中派生出來的。

+0

可能重複的[如何將2個圖(ggplot)合併爲一個圖?](http://stackoverflow.com/questions/21192002/how-to-combine-2-plots-ggplot-into-one-plot) – figurine

+0

它是相似的但是在這個問題中,點和線都是從同一個數據框中創建的,而我使用兩個單獨的數據框作爲點和線 –

+0

將代碼排列成與鏈接答案類似的結構,並確保定義哪個數據框用於每個情節,它會正常工作。 – figurine

回答

4

您可以將data添加到任何geom_,與您在主要ggplot調用中使用的值無關。對於這一點,我會跳過任何數據或美學映射分配在主ggplot呼叫,做他們都在各自的每個geom_

library(scales) 

gg <- ggplot() 
gg <- gg + geom_point(data=depth.temp, aes(x=asDate, y=depth, color=temp), size=5, alpha=0.3) 
gg <- gg + geom_line(data=depth.cv, aes(x=asDate, y=meandepth, size=cv)) 
gg <- gg + scale_color_gradientn(colours=rev(rainbow(12))) 
gg <- gg + scale_x_date(labels=date_format("%Y-%m-%d")) 
gg <- gg + scale_y_reverse() 
gg <- gg + labs(x=NULL, y="Depth") 
gg <- gg + theme_bw() 
gg 

enter image description here

+0

這正是我一直在尋找的,非常感謝! –

1

在我的情況下,對於線圖顯示我必須設置AES等:

AES(X = asDate,Y = meandepth,基團= 1)

在geom_line情節