[英]Similar seaborn plot in R
您的代碼每次循環都會創建一個新的繪圖。 使用lines
將lines
添加到現有圖。
plot(x, sin(x + 1 * .5)*(7 - 1), type="l")
for(i in seq(2,6)) {
lines(x, sin(x + i * .5)*(7 - i), col=i)
}
您也可以使用ggplot2
做到這ggplot2
:
library(tidyverse) # loads several related packages including ggplot2 and purrr, both of which we use below.
my_fun = function(x, i) {
sin(x + i * .5)*(7 - i)
}
ggplot(data.frame(x=x), aes(x)) +
map2(1:6, hcl(seq(15,375,length=7)[1:6],100,65), function(ii,cc) {
stat_function(fun=my_fun, args=list(i=ii), col=cc)
}) +
theme_classic()
使用ggplot2
x = seq(from = 0,to = 14,length.out = 100)
p <- ggplot(data=data.frame(x), aes(x = x,y = sin(x + 1 * .5)*(7 - 1) ))+geom_line()
for (i in 2:6) {
p <- p + geom_line(aes_string(y = sin(x + i * .5)*(7 - i)),col = i)+
theme_classic()+theme(axis.title.y = element_blank())
}
p
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.