在ggplot2中绘制多个组的行给出了一个奇怪的结果

时间:2018-03-24 20:39:43

标签: r plot ggplot2

我已经完成了物种积累曲线,并希望在同一ggplot中绘制不同底物大小类的SAC结果,y轴上的预期物种丰富度和x轴上的位点样品数量。数据包含每个大小类中的累积样本数量(列"站点"),预期的物种丰富度(列"丰富度")和基质大小等级10,20和30(列" SC"。)

GUID

我希望每个sizeclass都有唯一的线型。我在ggplot中使用了以下代码:

   sites  richness sc
1     1 0.6696915 10
2     2 1.2008513 10
3     3 1.6387310 10
4     4 2.0128472 10
5     5 2.3424933 10
6     6 2.6403239 10

     sites richness sc
2836     1 1.000000 20
2837     2 1.703442 20
2838     3 2.249188 20
2839     4 2.706618 20
2840     5 3.110651 20
2841     6 3.479173 20

然而,不是在不同的线型中得到三条整齐的线条,而是[这种混乱的东西比一群斑马更多的条纹] [1]:https://i.stack.imgur.com/iD75K.jpg。我确信解决方案相当简单,但对于我的生活,我无法弄明白。

//正如布鲁克斯所指出的那样,我应该添加一些可重现的数据,这里是我的dput数据的一个子集,其中包含10个大小等级10和20的第一次观察:

sac_kaikki<-ggplot(sac_data, aes(x=sites, y=richness,group=sc)) +
  geom_line(aes(linetype=sc))+
  coord_cartesian(xlim=c(0,100))+
  theme(axis.title.y = element_blank())+
  theme(axis.title.x = element_blank())

//没关系,无论出于何种原因,如果我只绘制两个尺寸类,情节就可以正常工作,但是包括第三个尺寸产生了荒谬的情节我发布了一张照片。

dput(head(subset(sac_data,sac_data$sc=="10"),10)) structure(list(sites = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), richness = c(0.669691470054462, 1.20085134466255, 1.63873100707468, 2.01284716414471, 2.34249332096243, 2.64032389106845, 2.91468283244696, 3.17111526890278, 3.41334794519086, 3.64392468817362), sc = c("10", "10", "10", "10", "10", "10", "10", "10", "10", "10")), .Names = c("sites", "richness", "sc" ), row.names = c(NA, 10L), class = "data.frame") dput(head(subset(sac_data,sac_data$sc=="20"),10)) structure(list(sites = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), richness = c(0.999999999999987, 1.70344155844158, 2.24918831168832, 2.70661814764865, 3.11065087175364, 3.47917264517669, 3.82165739030286, 4.14341144680334, 4.44765475554031, 4.73653870494466), sc = c("20", "20", "20", "20", "20", "20", "20", "20", "20", "20")), .Names = c("sites", "richness", "sc" ), row.names = 2836:2845, class = "data.frame")

1 个答案:

答案 0 :(得分:0)

使用您的样本数据对我来说很好:

a <- structure(list(sites = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), richness = c(0.669691470054462, 
                                                                      1.20085134466255, 1.63873100707468, 2.01284716414471, 2.34249332096243, 
                                                                      2.64032389106845, 2.91468283244696, 3.17111526890278, 3.41334794519086, 
                                                                      3.64392468817362), sc = c("10", "10", "10", "10", "10", "10", 
                                                                                                "10", "10", "10", "10")), .Names = c("sites", "richness", "sc"
                                                                                                ), row.names = c(NA, 10L), class = "data.frame")
b <- structure(list(sites = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), richness = c(0.999999999999987, 
                                                                           1.70344155844158, 2.24918831168832, 2.70661814764865, 3.11065087175364, 
                                                                           3.47917264517669, 3.82165739030286, 4.14341144680334, 4.44765475554031, 
                                                                           4.73653870494466), sc = c("20", "20", "20", "20", "20", "20", 
                                                                                                     "20", "20", "20", "20")), .Names = c("sites", "richness", "sc"
                                                                                                     ), row.names = 2836:2845, class = "data.frame")

c <- structure(list(sites = 1:10, richness = c(0.42857142857143, 0.838095238095238, 
                                               1.22932330827066, 1.60300751879699, 1.95989974937343, 2.30075187969924, 
                                               2.62631578947368, 2.93734335839598, 3.23458646616541, 3.5187969924812
), sc = c("30", "30", "30", "30", "30", "30", "30", "30", "30", 
          "30")), .Names = c("sites", "richness", "sc"), row.names = c(NA, 
                                                                       10L), class = "data.frame")

sac_data <- bind_rows(a, b, c)

绘图:

ggplot(sac_data, aes(sites, richness, group = sc)) + 
  geom_line(aes(linetype = sc))

enter image description here