[問題] 繪圖時不直接使用變數名稱
[問題類型]:
程式諮詢(我想用R 做某件事情,但是我不知道要怎麼用R 寫出來)
[軟體熟悉度]:
使用者(已經有用R 做過不少作品)
[問題敘述]:
在ggplot2、plotly、leaflet等繪圖套件中,
很常直接用變數的名稱去做aesthetic mapping,
但是一旦碰到迴圈或其它programming需求,
它反而很難一般化。
像是下列的ggplot2繪圖範例就有些結果上的差別。
我知道ggplot2套件中有aes_string提供較好的解決方案,
但是其它繪圖套件(例如plotly或leaflet)有類似的function嗎?
還是它們用non-standard evaluation時不會有問題?
這個問題困擾我滿久了,有沒有人願意分享一下經驗?
謝謝!
[程式範例]:
# 最一般的做法
ggplot(mtcars, aes(mpg, qsec)) + geom_point() + facet_grid(. ~ am)
# 利用aes_string function
ggplot(mtcars, aes_string("mpg", "qsec")) + geom_point() + facet_grid(. ~ am)
# 以下結果是錯誤的圖
# 用[]指定column
ggplot(mtcars, aes(mtcars[,1], mtcars[,7])) + geom_point() + facet_grid(. ~ am)
# Non-standard evaluation
ggplot(mtcars, aes(eval(parse(text = "mpg"), envir = mtcars),
eval(parse(text = "qsec"), envir = mtcars))) +
geom_point() +
facet_grid(. ~ am)
Stack Overflow相關解答:https://goo.gl/onbTcz
[關鍵字]:
Non-standard evaluation, aesthetic mapping
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.112.121.113
※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1503486206.A.9C3.html
※ 編輯: clsmbstu (140.112.121.113), 08/23/2017 19:08:12
→
08/23 19:22, , 1F
08/23 19:22, 1F
→
08/23 19:23, , 2F
08/23 19:23, 2F
→
08/23 19:23, , 3F
08/23 19:23, 3F
→
08/23 19:24, , 4F
08/23 19:24, 4F
→
08/23 19:49, , 5F
08/23 19:49, 5F
→
08/23 19:51, , 6F
08/23 19:51, 6F
→
08/23 19:52, , 7F
08/23 19:52, 7F
→
08/23 19:53, , 8F
08/23 19:53, 8F
→
08/23 19:54, , 9F
08/23 19:54, 9F
→
08/23 19:55, , 10F
08/23 19:55, 10F
→
08/23 19:57, , 11F
08/23 19:57, 11F
→
08/23 19:57, , 12F
08/23 19:57, 12F
→
08/23 19:58, , 13F
08/23 19:58, 13F
→
08/23 19:58, , 14F
08/23 19:58, 14F
→
08/23 19:58, , 15F
08/23 19:58, 15F
→
08/23 19:59, , 16F
08/23 19:59, 16F
→
08/23 19:59, , 17F
08/23 19:59, 17F
→
08/23 19:59, , 18F
08/23 19:59, 18F
→
08/23 20:00, , 19F
08/23 20:00, 19F
→
08/23 20:00, , 20F
08/23 20:00, 20F
→
08/23 20:01, , 21F
08/23 20:01, 21F
→
08/23 20:02, , 22F
08/23 20:02, 22F
→
08/23 20:02, , 23F
08/23 20:02, 23F
→
08/23 20:03, , 24F
08/23 20:03, 24F
→
08/23 20:05, , 25F
08/23 20:05, 25F
→
08/23 20:05, , 26F
08/23 20:05, 26F
→
08/23 20:05, , 27F
08/23 20:05, 27F
→
08/23 20:06, , 28F
08/23 20:06, 28F
→
08/23 20:06, , 29F
08/23 20:06, 29F
→
08/23 20:06, , 30F
08/23 20:06, 30F
→
08/23 20:07, , 31F
08/23 20:07, 31F
→
08/23 20:50, , 32F
08/23 20:50, 32F
→
08/23 20:52, , 33F
08/23 20:52, 33F
→
08/23 20:52, , 34F
08/23 20:52, 34F
→
08/23 20:54, , 35F
08/23 20:54, 35F
→
08/23 20:55, , 36F
08/23 20:55, 36F
→
08/23 20:55, , 37F
08/23 20:55, 37F
→
08/23 20:56, , 38F
08/23 20:56, 38F
→
08/23 20:57, , 39F
08/23 20:57, 39F
→
08/23 20:58, , 40F
08/23 20:58, 40F
→
08/23 20:58, , 41F
08/23 20:58, 41F
→
08/23 20:58, , 42F
08/23 20:58, 42F
→
08/24 08:59, , 43F
08/24 08:59, 43F
→
08/24 09:00, , 44F
08/24 09:00, 44F
※ 編輯: clsmbstu (114.44.107.83), 08/24/2017 09:01:28
→
08/24 09:03, , 45F
08/24 09:03, 45F
→
08/24 09:05, , 46F
08/24 09:05, 46F
→
08/24 09:07, , 47F
08/24 09:07, 47F
→
08/24 09:08, , 48F
08/24 09:08, 48F
→
08/24 09:33, , 49F
08/24 09:33, 49F
→
08/24 09:33, , 50F
08/24 09:33, 50F
→
08/24 09:34, , 51F
08/24 09:34, 51F
→
08/24 09:36, , 52F
08/24 09:36, 52F
→
08/24 09:37, , 53F
08/24 09:37, 53F
→
08/24 09:38, , 54F
08/24 09:38, 54F
R_Language 近期熱門文章
PTT數位生活區 即時熱門文章