簡體   English   中英

在 ggplot 圖中對點進行分組

[英]Grouping points in a ggplot graph

這是我的數據

# 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 中使用組完成了此操作,並且效果很好。 然而,由於某種原因,這條線只是在所有數據中顯示為一條直線。 這是我的代碼:

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

謝謝您的幫助

基本上一切正常。 問題是你有一個分類軸。 因此你得到一條直線。 當您使用position_dodge時,這些點根本不在線上。

為了實現所需的 plot,我的方法將分類變量leaf.species轉換為數字,即CAFO = 1CALE = 2 ,並將不同的 x 值分配給不同的type 之后我應用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"))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM