2016-04-23 51 views
1

我想在下面的圖中添加其他樣條線,但是如果我這樣做,geom_point()將僅顯示第一個樣條線上的點。與ggplot2在同一圖上的多個樣條曲線?

n <- 10 
d <- data.frame(x = 1:n, y = rnorm(n)) 
ggplot(d,aes(x,y)) + geom_point() + 
    geom_line(data=data.frame(spline(d, n=n*10))) 

enter image description here

我怎樣才能在第二行顯示的點嗎?

+0

你可以添加另一個geom_line與新的樣條,或者先把所有的樣條線放在data.frame上 –

+0

我在geom_line中添加了另一條線,除了只顯示點第一個樣條。第二個樣條是「裸體」。 – Lanza

+0

啊 - 我誤解你在做什麼。請你可以更新你的例子來顯示你想要的東西 –

回答

2

假設你最初在兩個data.frames中有你想要的數據,這段代碼將起作用。它使用ggplot之前使樣條。

n <- 10 
d <- data.frame(x = 1:n, y = rnorm(n)) 
d2 <- data.frame(x = 1:n, y = rnorm(n)) 
dd <- rbind(cbind(d, case = "d"), cbind(d2, case = "d2")) 
ddsmooth <- plyr::ddply(dd, .(case), function(k) as.data.frame(spline(k, n = n * 10))) 
ggplot(dd,aes(x, y, group = case)) + geom_point() + 
    geom_line(aes(x, y, group = case), data = ddsmooth) 

如果您的數據位於data.frame的不同列中,請使用reshape2 :: melt來處理它。

相關問題