Drkcore

21 12 2010 R ggplot2 Tweet

Rで富士山関数

富士山をプロットする関数をみかけたので、Rで描いてみた。

fx <- function(x){x^4-x^2+6}
sfx <- function(x){12/(abs(x)+1)}
gx <- function(x){1/2*cos(6*x)+7/2}

plot(gx,-2,2,xlim=c(-7,7),ylim=c(0,7),xlab="",ylab="")
par(new=T)
plot(sfx,-7,-1,xlim=c(-7,7),ylim=c(0,7),axes=F,xlab="",ylab="")
par(new=T)
plot(sfx,1,7,xlim=c(-7,7),ylim=c(0,7),axes=F,xlab="",ylab="")
par(new=T)
plot(fx,-1,1,xlim=c(-7,7),ylim=c(0,7),axes=F,xlab="",ylab="")

fujisan plot

ggplotで関数だけ描く方法わからんなぁとつぶやいたら、教えてもらったのでggplot2でも描いた。

gx <- function(x){
x[-2>x | x>2] <-NA
1/2*cos(6*x)+7/2
}

fx <- function(x){
x[x<(-1)] <- NA
x[x>1] <- NA
x^4-x^2+6
}

fx <- function(x){
x[-1<x & x<1] <-NA
12/(abs(x)+1)
}

ggplot(data.frame(x=c(-7,7),y=0), aes(x,y))+stat_function(fun=gx) \
+stat_function(fun=fx)+stat_function(fun=sx)

fujisan plot

ggplot2だと線がうまくつながらなかった。

ProductName Ggplot2: Elegant Graphics for Data Analysis (Use R!)
Hadley Wickham
Springer-Verlag New York Inc (C) / ¥ 5,259 ( 2009-08-30 )


About

  • もう5年目(wishlistありマス♡)
  • 最近はPythonとDeepLearning
  • 日本酒自粛中
  • ドラムンベースからミニマルまで
  • ポケモンGOゆるめ

Tag

Python Deep Learning javascript chemoinformatics Emacs sake and more...

Ad

© kzfm 2003-2021