Re: [問題] ggplot2 畫function

作者: andrew43 (討厭有好心推文後刪文者)   2018-04-16 22:30:37
→ andrew43: 按ggplot設計的風格,你應該要先創造一個dataframe 04/16 22:18
→ andrew43: 包括了所有2 / (ap[k]) * x ^ 2結果以及多一個欄位來說 04/16 22:19
→ andrew43: 明k各別是多少... 所以會有三個欄位: x, fun(x), k 04/16 22:20
→ andrew43: 我上面的k是指ap中各元素 04/16 22:20
→ andrew43: 之後,就只要一行geom_line()並指定k為不同顏色即可 04/16 22:21
→ andrew43: 我回個文好了
請參考以下簡化的例子
重點就在於生成 dt
library(ggplot2)
x <- seq(0, 200, 10)
ap <- 1:10
dt <-
data.frame(
x.val = rep(x, length(ap)),
ap.val = rep(ap, each = length(x))
)
dt$y.val <- 2 / dt$ap.val * dt$x.val ^ 2
ggplot(dt, aes(x.val, y.val, color = as.factor(ap.val))) +
geom_line()
至於指定顏色等等細節先暫不談
※ 引述《Itok (浪裡白條)》之銘言:
: [問題類型]:
: 程式諮詢(我想用R 做某件事情,但是我不知道要怎麼用R 寫出來)
: [軟體熟悉度]:
: 使用者(已經有用R 做過不少作品)
: [問題敘述]:
: 我有用ggplot2畫function,想畫多條線在同一張圖,
: 用for 迴圈卻只能畫出最後一條,
: 想請教版上高手如何處理,謝謝。
: [程式範例]:
: library(ggplot2)
: ap<-c(10,20,30,40,50,60,70,80,90,100,150,200,250,300,350,400,450,500,550,600,650,700,750,800,850,900,950,1000)
: #initial plot p
: p<-ggplot(data.frame(x=c(0,200)), aes(x))+ylim(0,200)
: #畫出多條function
: myPlot<-ggplot(data.frame(x=c(0,200)), aes(x))+ylim(0,200)+
: stat_function(fun=function(x)2/(ap[1])*x^2, geom="line", aes(colour="010"))+
: stat_function(fun=function(x)2/(ap[2])*x^2, geom="line", aes(colour="020"))+
: stat_function(fun=function(x)2/(ap[3])*x^2, geom="line", aes(colour="030"))+
: stat_function(fun=function(x)2/(ap[4])*x^2, geom="line", aes(colour="040"))+
: stat_function(fun=function(x)2/(ap[5])*x^2, geom="line", aes(colour="050"))+
: stat_function(fun=function(x)2/(ap[6])*x^2, geom="line", aes(colour="060"))+
: stat_function(fun=function(x)2/(ap[7])*x^2, geom="line", aes(colour="070"))+
: stat_function(fun=function(x)2/(ap[8])*x^2, geom="line", aes(colour="080"))+
: stat_function(fun=function(x)2/(ap[9])*x^2, geom="line", aes(colour="090"))+
: stat_function(fun=function(x)2/(ap[10])*x^2, geom="line", aes(colour="100"))+
: stat_function(fun=function(x)2/(ap[11])*x^2, geom="line", aes(colour="150"))+
: stat_function(fun=function(x)2/(ap[12])*x^2, geom="line", aes(colour="200"))+
: stat_function(fun=function(x)2/(ap[13])*x^2, geom="line", aes(colour="250"))+
: stat_function(fun=function(x)2/(ap[14])*x^2, geom="line", aes(colour="300"))+
: stat_function(fun=function(x)2/(ap[15])*x^2, geom="line", aes(colour="350"))+
: stat_function(fun=function(x)2/(ap[16])*x^2, geom="line",aes(colour="400"))+
: stat_function(fun=function(x)2/(ap[17])*x^2, geom="line",aes(colour="450"))+
: stat_function(fun=function(x)2/(ap[18])*x^2, geom="line",aes(colour="500"))
: myPlot
: #用for迴圈畫僅能畫出最後一條
: for (i in c(1:18))
: {
: p<-p+stat_function(fun=function(x){2/(ap[i])*x^2}, geom="line", aes(colour=ap[i]))
: }
: p
: 請提供 sessionInfo() 的輸出結果,
: 裡面含有所有你使用的作業系統、R 的版本和套件版本資訊,
: 讓版友更容易找出錯誤
: R version 3.3.3 (2017-03-06)
: Platform: x86_64-w64-mingw32/x64 (64-bit)
: Running under: Windows >= 8 x64 (build 9200)
: [關鍵字]:
: ggplot2 function
作者: Itok (浪裡白條)   2018-04-18 18:47:00
謝謝你的回覆

Links booklink

Contact Us: admin [ a t ] ucptt.com