如何在多个情节中添加不同的图例?

时间:2019-06-24 13:39:18

标签: r plot model legend population

好的,所以我使用了par函数来组合多个图。 我添加了图例,这很有趣,也很有趣,直到某些情节需要与其他情节不同的图例,并且我无法弄清楚使其发挥作用所需的编程技巧。

我已经在“我需要帮助的部分”的名称下包含了我的传说的部分,如您所见,其中有四个,分别是N1,N2,K1和K2,并带有斜线和每个文本功能。

我试图列出串联等,但是它与我的Alpha并不能很好地工作,而且我花了很长时间研究如何使其起作用。

# establish number of groups, and size of each group
GROUP_SIZE = 15
GROUP_NUM = math.ceil(len(data) / group_size)
# make an empty list of groups to add each group to
groups = []
while len(groups) < GROUP_NUM and (len(males) > 0 and len(females) > 0):
    # calculate the proper gender ratio, to perfectly balance this group
    num_males = len(males) / len(data) * GROUP_SIZE
    num_females = GROUP_SIZE - num_males
    # select that many people from the previously-shuffled lists
    males_in_this_group = [males.pop(0) for n in range(num_males) if len(males) > 0]
    females_in_this_group = [males.pop(0) for n in range(num_females) if len(females) > 0]
    # put those two subsets together, shuffle to make it feel more random, and add this group
    this_group = males_in_this_group + females_in_this_group
    random.shuffle(this_group)
    groups.append(this_group)

Plot Output

我希望在第一张图上有N1和N2,在第二张和第四张上有K1,在第三张上有K2。谢谢大家的帮助!

山姆

1 个答案:

答案 0 :(得分:0)

请考虑根据 i 循环迭代器有条件地绘制所需的ablines。请参阅下面的“帮助”部分中的调整内容

  ### This is the part where I need help
  if(i == 1) {
    N1eq <- (R1[i]*alphs[2,2]-R2[i]*alphs[1,2])/
      (alphs[1,1]*alphs[2,2]-alphs[2,1]*alphs[1,2])
    abline(h = N1eq, lty = 3)
    text(0, N1eq, "n1*", adj = c(0, 0))

    N2eq <- (R2[i]*alphs[1,1]-R1[i]*alphs[2,1])/
      (alphs[1,1]*alphs[2,2]-alphs[2,1]*alphs[1,2])
    abline(h = N2eq, lty = 3)
    text(0, N2eq, "n2*", adj = c(0, 0))
  }

  if (i %in% c(2,4)) {
    K1 <- R1[i]/alphs[1,1]
    abline(h = K1, lty = 3)
    text(0, K1, "K1", adj = c(0, 0))
  }

  if (i == 3) {
    K2 <- R2[i]/alphs[2,2]
    abline(h = K2, lty = 3)
    text(0, K2, "K2", adj = c(0, 0))
  }

Plot Output