【问题标题】:Grouping points in a ggplot graph在 ggplot 图中对点进行分组
【发布时间】:2020-07-05 20:22:20
【问题描述】:

这是我的数据

# Groups:   pot.code [63]
   hemiparasite host  leaf.species pot.code   pot type  Metal Value
   <fct>        <fct> <fct>        <fct>    <int> <fct> <chr> <dbl>
 1 CALE         ACMI  CALE         2B           2 B     K     0.829
 2 CALE         ACMI  CALE         2C           2 C     K     0.500
 3 CALE         ACMI  CALE         4B           4 B     K     0.610
 4 CALE         ACMI  CALE         4C           4 C     K     0.538
 5 CALE         ZEMA  CALE         12B         12 B     K     0.679
 6 CALE         ZEMA  CALE         12C         12 C     K     0.382
 7 CAFO         ACMI  CAFO         41B         41 B     K     0.638
 8 CAFO         ACMI  CAFO         41C         41 C     K     0.273
 9 CAFO         ACMI  CAFO         42B         42 B     K     0.518
10 CAFO         ACMI  CAFO         42C         42 C     K     0.329
# ... with 368 more rows

绘制图形时,我希望将同一锅中的“类型”B 和 C 用一条线连接起来,我之前在 ggplot2 中使用 group 完成了此操作,并且效果很好。然而,由于某种原因,这条线只是在所有数据中显示为一条直线。这是我的代码:

ggplot(leaf.graph, aes(x=leaf.species, y=Value, group=pot))+
  geom_line()+
  geom_point(aes(color=host, shape=type), position=position_dodge(width=0.4))+
  facet_wrap(.~Metal, scales="free", ncol=2)+
  theme_minimal()+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"))+
  scale_shape_discrete(name="Type", labels =c("Parasitism", "No parasitism"))+
  scale_color_discrete(name="Host", labels= c("ACMI", "ZEMA"))

感谢您的帮助!

编辑——我以前用分类轴做过这个——见下面的例子

数据:

 X        K       Na        P       Mg        Ca         S         drought pot species
1 1 19103.74 230.4304 3451.667 4657.260 19184.494 11700.592 normal watering   1    CALE
2 2 21286.39 282.5610 3639.559 3259.262  7514.861  2534.621 normal watering   1    ACMI
3 3 28356.04 182.4751 3227.498 4038.457 13496.755  8017.703 normal watering   3    CALE
4 4 23747.66 232.2271 3193.174 2558.794  5526.189  2009.643 normal watering   3    ACMI
5 5 32659.69 220.5023 2108.735 3467.477 14738.200 10490.562 normal watering   7    CALE
6 6 18798.06 410.5469 4354.962 2450.054  5913.416  3071.759 normal watering   7    ACMI
  leaves root.exclusion Leaf.Mass Leaf.Area castilleja.sp treatment unique_code shoot.ht
1     NA  no parasitism     0.515        NA  C. levisecta         1         1_1     19.6
2     47  no parasitism        NA        NA  C. levisecta         1         1_1     11.3
3     NA  no parasitism     0.761        NA  C. levisecta         1         1_3     18.4
4     47  no parasitism        NA     6.968  C. levisecta         1         1_3      9.2
5     NA  no parasitism     0.509        NA  C. levisecta         1         1_7     14.4
6     41  no parasitism        NA        NA  C. levisecta         1         1_7     16.4

然后我们有图表的代码,分组在其中起作用

Mg<-ggplot(courtney.CALE, aes(x=species, y=Mg, group=unique_code))+
  geom_line(color="grey")+
  geom_point(aes(color=drought))+
  facet_wrap(.~root.exclusion)+
  newtheme; Mg

感谢您的帮助

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    基本上一切正常。问题是你有一个分类轴。因此你得到一条直线。当您使用position_dodge时,这些点根本不在线上。

    为了实现所需的绘图,我的方法将分类变量leaf.species 转换为数字,即CAFO = 1CALE = 2,并将不同的x 值分配给不同的types。之后我申请scale_x_continuous 将类别作为标签取回。试试这个:

    library(dplyr)
    library(ggplot2)
    
    leaf.graph1 <- leaf.graph %>% 
      mutate(x = as.numeric(factor(leaf.species)))
    breaks <- unique(leaf.graph1$x)
    labels <- unique(leaf.graph1$leaf.species)
    
    ggplot(leaf.graph1, aes(x = ifelse(type == "B", x - 0.2, x + 0.2), y=Value, group=pot))+
      geom_line() +
      geom_point(aes(color=host, shape=type))+
      scale_x_continuous(breaks = breaks, labels = labels, limits = c(.5, 2.5)) +
      facet_wrap(.~Metal, scales="free", ncol=2)+
      theme_minimal()+
      theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
            panel.background = element_blank(), axis.line = element_line(colour = "black"))+
      scale_shape_discrete(name="Type", labels =c("Parasitism", "No parasitism"))+
      scale_color_discrete(name="Host", labels= c("ACMI", "ZEMA"))
    

    【讨论】:

    • 感谢您的帮助,但不幸的是这对我不起作用;请查看我提供的示例的编辑,其中我具有相同的轴和数据类别以及分组因子类别并且它有效。出于某种原因,您的解决方案没有给我所有的物种。
    • 嗨@Skotani1。好的,现在我明白了。问题不在于分类轴本身。问题是您只想连接一个类别的点,例如pot = 2 中的类型 A 和 B 都属于 CAFO 类别。因此它们共享相同的 x 轴值,您得到一条直线。在您编辑的数据中,分组跨越 x 轴,即类别CAFO 的一个值,ACMI 类别的一个值。因此,您有不同的 x 轴值并得到漂亮的线条。
    • 哦!我应该知道的。好的,非常感谢您的帮助@stefan。问题是如何在不改变轴的情况下让它工作,也许是不可能的
    猜你喜欢
    • 2011-11-17
    • 1970-01-01
    • 2021-07-15
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 2016-02-07
    • 2012-03-07
    • 1970-01-01
    相关资源
    最近更新 更多