r - ggplot2: Recreating A plot of shrinkage from a book -
i trying recreate following plot computer age statistical inference.
i have following data
player,mle,truth,js 1,0.345,0.298,0.2848934967658405 2,0.333,0.346,0.2807859008379247 3,0.322,0.222,0.2770206045706685 4,0.311,0.276,0.2732553083034123 5,0.289,0.263,0.26572471576889994 6,0.289,0.273,0.26572471576889994 7,0.278,0.303,0.26195941950164375 8,0.255,0.27,0.25408652730647174 9,0.244,0.23,0.25032123103921555 10,0.233,0.264,0.2465559347719594 11,0.233,0.264,0.2465559347719594 12,0.222,0.21,0.2427906385047032 13,0.222,0.256,0.2427906385047032 14,0.222,0.269,0.2427906385047032 15,0.211,0.316,0.239025342237447 16,0.211,0.226,0.239025342237447 17,0.2,0.285,0.23526004597019082 18,0.145,0.2,0.2164335646339099
i gave try, seems points not connected lines correctly.
here code
js_player %>% gather(type,value,2:4) %>% ggplot(aes(x=value,y=type))+ geom_point()+ geom_line(aes(group=player),lty=2, alpha=1/4)+ theme_minimal()
from ?geom_line
:
geom_line()
connects [observations] in order of variable on x axis".
which not want. want them connected in order true - js - mle. therefore geom_path
useful here:
geom_path()
connects observations in order in appear in data
thus, need massage data accordingly. (1) convert "type" factor
levels
in desired order. (2) order data according "type" (arrange(type)
). could more explicit , order "player" well, since use group = player
, not needed. (3) replace geom_line
geom_path
.
df %>% gather(type, value, 2:4) %>% mutate(type = factor(type, levels = c("truth", "js", "mle"))) %>% arrange(type) %>% ggplot(aes(x = value, y = type)) + geom_point() + geom_path(aes(group = player), lty = 2, alpha = 1/4) + theme_minimal()
try on smaller toy data set (easier check if/how works):
df <- read.csv(text = "player,mle,truth,js 1,1,2,3 2,2,4,5 3,5,5,4 4,8,8,6")
alternatively, stick geom_line
, use coord_flip
:
df %>% gather(type, value, 2:4) %>% mutate(type = factor(type, levels = c("truth", "js", "mle"))) %>% ggplot(aes(x = type, y = value)) + geom_point() + geom_line(aes(group = player), lty = 2, alpha = 1/4) + coord_flip() + theme_minimal()
Comments
Post a Comment