首页 > 解决方案 > 在 ggplot 图中对点进行分组

问题描述

这是我的数据

# 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

谢谢您的帮助

标签: rggplot2

解决方案


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

为了实现所需的绘图,我的方法将分类变量转换leaf.species为数字,即CAFO = 1并将CALE = 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"))


推荐阅读