geom_path未正确连接有序因子的点

时间:2019-03-19 23:30:27

标签: r ggplot2

我试图在x轴上的8个有序因子上绘制四种不同“类型”的100次仿真结果。 Geom_path似乎正在忽略因子和连接点的顺序。最终,我希望有色线对应于“类型”因数,将按字母顺序从A到I的每个点连接起来。任何见识将不胜感激。

我的数据格式较长。 L1是字母从A到I的有序因子,(A << I)

> head(h.melt)
         value L1   type log_value
1 2.738509e-07  A   d up -6.562486
2 0.000000e+00  A d down      -Inf
3 8.293517e-09  A   e up -8.081261
4 0.000000e+00  A e down      -Inf
5 0.000000e+00  B   d up      -Inf
6 5.323505e-09  B d down -8.273802
> 

这是我的绘图代码:

library(ggplot2)
pd <- position_dodge(0.4) #jitters postion if called
p <- ggplot(h.melt, aes(x = L1 , y = value, color=factor(type), alpha=5, group = type))
p <- p + geom_point()
p <- p + geom_path()
#p <- p + geom_path(aes(group = factor(type)), size = 0.7, linetype = 6, alpha =0.5)
p <- p + scale_x_discrete(limits = c("A","B","C","D","E","F","G","H","I"),labels=c("500","1000","1500","2000","2500","3000","3500","4000", "4500"))
p <- p + scale_fill_manual(values=c("#526EFF", "#FFCD00", "#56B4E9", "#FF9900"))
p <- p + theme(axis.text.x = element_text(angle = 45, vjust = 1, 
                                          size = 12, hjust = 1),
               axis.text.y = element_text(angle = 0, vjust = 1, 
                                          size = 12, hjust = 1) )
p <- p + theme_bw()
cols <-c("#526EFF", "#FFCD00", "#56B4E9", "#FF9900")
p <- p + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
p <- p +
  theme(plot.background = element_rect(fill = "transparent"),
        panel.background = element_rect(fill = "transparent")) # all rectangles
p <- p + scale_color_manual(values = cols)
p <- p + theme(axis.text=element_text(size=16),
               axis.title=element_text(size=18))

plot(p)

输出看起来像这样: enter image description here

0 个答案:

没有答案