一组ggplot2缺少线型

时间:2017-08-20 06:38:30

标签: r ggplot2

我有以下数据:

date, name, bin,group, value
2017-08-19,a1,0,1,302
2017-08-19,a3,0,1,35
2017-08-19,a4,0,1,33
2017-08-19,a6,0,1,43
2017-08-19,p1,0,0,76
2017-08-19,i3,0,0,23
2017-08-19,cl,1,1,73
2017-08-19,c,1,0,2
2017-09-19,a1,0,1,302
2017-09-19,a3,0,1,35
2017-09-19,a4,0,1,33
2017-09-19,a6,0,1,43
2017-09-19,p1,0,1,76
2017-09-19,i3,0,1,23
2017-09-19,cl,1,1,73
2017-09-19,c,1,1,2

出于某种原因,我最终会得到一个不显示其中一个组的线型的情节。

这是我的代码:

p <- df %>%
  ggplot(aes(y=value,x=date,color=name))+
  geom_point(aes(shape=factor(bin)))+
  geom_line(aes(linetype=factor(group)))+
  geom_hline(aes(yintercept = 0))+
  theme_minimal()
p

您可以从下图中看到其中一种线型没有显示。

enter image description here

如何显示其他线型?

1 个答案:

答案 0 :(得分:1)

某些行没有显示的原因是,对于某些name,有两个group。结果,ggplot没有选择显示线条的那些,并且显然决定什么都没有。

一种可能的解决方案是将group - 值更改为每个name的第一个值,然后绘制:

df %>% 
  group_by(name) %>% 
  mutate(group = first(group)) %>% 
  ggplot(aes(x = date, y = value, color = name)) +
  geom_point(aes(shape = factor(bin))) +
  geom_line(aes(linetype = factor(group))) +
  geom_hline(aes(yintercept = 0)) +
  theme_minimal()

给出了以下情节:

enter image description here

相关问题