【问题标题】:How do I color points separately when plotting multiple line graphs with a common x-axis?绘制具有公共 x 轴的多个折线图时,如何分别为点着色?
【发布时间】:2019-03-30 10:59:02
【问题描述】:

我想制作 3 个堆叠线图,L、F 和 P,它们都具有共同的 x 轴深度,并且点由 Depo 着色。我找到了多种堆叠图表的方法,但我正在努力整合我想要的点颜色编码。

这是一些示例数据 - 抱歉,由于某种原因,它不会保留为表格格式

depth     L     F     P Depo
67.48 1.003 1.063 1.066 Turb
67.63 1.004 1.020 1.024 Dri
67.73 1.011 1.017 1.028 Dri
67.83 1.006 1.007 1.014 Turb
67.92 1.003 1.029 1.032 Pro
68.06 1.004 1.007 1.011 Pro

我可以通过制作图表然后使用 grid.draw 堆叠它们来获得我想要的东西。但这会重复每个图形的 x 轴值。

Lin <- ggplot(MyData, aes(x=depth, y=L)) + geom_line() + geom_point(data = MyData, aes(x=depth, y=L, color = Depo))    
Fab <- ggplot(MyData, aes(x=depth, y=P)) + geom_path() + geom_point(data = MyData, aes(x=depth, y=P, color = Depo))    
Fol <- ggplot(MyData, aes(x=depth, y=F)) + geom_path() + geom_point(data = MyData, aes(x=depth, y=F, color = Depo))    

grid.draw(rbind(ggplotGrob(Fol), ggplotGrob(Lin), ggplotGrob(Fab), size = "last"))

以下作品可以在不重复 x 轴的情况下绘制图表,但我不知道如何通过 Depo 更改点。

mm <- melt(subset(MyData, select=c(depth, L, F,P)), id.var="depth")

ggplot(mm, aes(x = depth, y = value)) + geom_line(aes(color = variable)) +
  facet_grid(variable ~ ., scales = "free_y") + theme(legend.position = "none")

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    我建议不要使用melt。根据我的经验,它很难正确使用,而且很容易出错。我个人更喜欢tidyr动词gather()spread()

    这是我对您的图表的处理方法:

    MyData %>%
      gather(variable, value, L:P) %>%
      ggplot() + aes(depth, value) +
      geom_path() + geom_point(aes(color = Depo, shape = Depo), size = 3) +
      facet_wrap(~variable, ncol = 1, scales = "free_y")
    

    第一步是对 L、F 和 P 列进行gather 操作,保持深度和 Depo。我的图表代码与您已经尝试过的代码没有太大不同。

    【讨论】:

      【解决方案2】:

      您可以抑制上图的 x 轴:

      Lin <- ggplot(MyData, aes(x=depth, y=L)) + geom_line() + geom_point(data = MyData, aes(x=depth, y=L, color = Depo)) + theme(axis.title.x=element_blank(),
                                                                                                                                  axis.text.x=element_blank(),
                                                                                                                                  axis.ticks.x=element_blank())   
      Fab <- ggplot(MyData, aes(x=depth, y=P)) + geom_path() + geom_point(data = MyData, aes(x=depth, y=P, color = Depo)) 
      Fol <- ggplot(MyData, aes(x=depth, y=F)) + geom_path() + geom_point(data = MyData, aes(x=depth, y=F, color = Depo)) + theme(axis.title.x=element_blank(),
                                                                                                                                    axis.text.x=element_blank(),
                                                                                                                                    axis.ticks.x=element_blank())
      
      grid.draw(rbind(ggplotGrob(Fol), ggplotGrob(Lin), ggplotGrob(Fab), size = "last"))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-14
        • 1970-01-01
        • 2023-02-04
        • 1970-01-01
        相关资源
        最近更新 更多