ggplot2:重新创建一本书的收缩情节

时间:2017-08-20 14:23:18

标签: r ggplot2

我正在尝试从计算机时代统计推断中重新创建以下图表。

enter image description here

我有以下数据

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

我尝试了一下,但似乎我的点没有正确连接。

enter image description here

这是我的代码

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()

1 个答案:

答案 0 :(得分:1)

来自?geom_line

  

geom_line()按照x轴上的变量顺序连接[观察]。

这不是你想要的。您希望它们按顺序TRUE连接 - JS - MLE。因此geom_path在这里非常有用:

  

geom_path()按照数据

中出现的顺序连接观察结果

因此,您需要相应地按摩您的数据。 (1)按所需顺序将“type”转换为factor levels。 (2)根据“类型”(arrange(type))对数据进行排序。您可以更明确,也可以按“播放器”排序,但由于我们使用group = Player,因此不需要这样做。 (3)将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()

尝试使用较小的玩具数据集(更容易检查它是否/如何工作):

df <- read.csv(text = "Player,MLE,TRUTH,JS
1,1,2,3
               2,2,4,5
               3,5,5,4
               4,8,8,6") 

enter image description here

或者,坚持geom_line并使用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()