有没有一种方法可以为一组个人添加标签,而不是为每个个人添加标签?

时间:2019-11-06 02:25:10

标签: r ggplot2

我正在使用R清理STRUCTURE图(Wilson和Rannala,2003)。该程序生成的绘图本身是可以的,但是我正在生成多个绘图,并希望保持多个因素一致,以便于读者阅读。原始地块的标签和组是按总体而不是按个人划分的,例如:(https://i.imgur.com/MAwJoEP.png

这些是我正在使用的数据:

Group   Ind pop_est K
UD  MAUD13  0.003   P1
UD  MAUD14  0.004   P1
UD  MAUD15  0.003   P1
UD  MAUD16  0.005   P1
UD  MAUD17  0.025   P1
UD  MAUD18  0.007   P1
UD  MAUD19  0.01    P1
UD  MAUD21  0.003   P1
UD  MAUD23  0.013   P1
MD  MAMD1   0.013   P1
MD  MAMD2   0.015   P1
MD  MAMD3   0.007   P1
MD  MAMD4   0.009   P1
MD  MAMD5   0.004   P1
MD  MAMD6   0.004   P1
MD  MAMD7   0.003   P1
LD  MALD1   0.015   P1
LD  MALD2   0.003   P1
LD  MALD3   0.007   P1
LD  MALD4   0.01    P1
LD  MALD5   0.004   P1
LD  MALD6   0.004   P1
LD  MALD7   0.026   P1
LD  MALD8   0.009   P1
LD  MALD9   0.006   P1
LD  MALD10  0.003   P1
LD  MALD12  0.005   P1
LD  MALD13  0.008   P1
LD  MALD14  0.004   P1
LD  MALD15  0.01    P1
LD  MALD16  0.004   P1
LD  MALD17  0.028   P1
LD  MALD18  0.004   P1
LD  MALD19  0.008   P1
LD  MALD20  0.003   P1
LD  MALD21  0.004   P1
LD  MALD23  0.018   P1
LD  MALD24  0.006   P1
LD  MALD25  0.003   P1
LD  MALD26  0.006   P1
LD  MALD27  0.006   P1
LD  MALD28  0.006   P1
LD  MALD29  0.004   P1
LD  MALD30  0.005   P1
UM  MAUM1   0.991   P1
UM  MAUM2   0.987   P1
UM  MAUM3   0.988   P1
UM  MAUM4   0.986   P1
UM  MAUM5   0.983   P1
UM  MAUM6   0.993   P1
UM  MAUM7   0.979   P1
UM  MAUM8   0.991   P1
UM  MAUM9   0.991   P1
UM  MAUM10  0.988   P1
UM  MAUM11  0.993   P1
UM  MAUM12  0.985   P1
UM  MAUM13  0.99    P1
UM  MAUM14  0.992   P1
UM  MAUM15  0.994   P1
UM  MAUM16  0.978   P1
UM  MAUM17  0.991   P1
UM  MAUM18  0.99    P1
UM  MAUM19  0.991   P1
UM  MAUM20  0.99    P1
MM  MAMM1   0.986   P1
MM  MAMM2   0.991   P1
MM  MAMM3   0.993   P1
MM  MAMM4   0.959   P1
MM  MAMM5   0.987   P1
MM  MAMM6   0.982   P1
MM  MAMM7   0.993   P1
MM  MAMM8   0.993   P1
MM  MAMM9   0.993   P1
MM  MAMM10  0.991   P1
MM  MAMM11  0.993   P1
MM  MAMM12  0.982   P1
MM  MAMM13  0.989   P1
MM  MAMM14  0.991   P1
MM  MAMM15  0.993   P1
MM  MAMM16  0.993   P1
MM  MAMM17  0.989   P1
MM  MAMM18  0.992   P1
MM  MAMM19  0.992   P1
MM  MAMM20  0.974   P1
LM  MALM1   0.004   P1
LM  MALM2   0.003   P1
LM  MALM3   0.006   P1
LM  MALM4   0.003   P1
LM  MALM5   0.004   P1
LM  MALM6   0.004   P1
LM  MALM7   0.003   P1
LM  MALM8   0.003   P1
LM  MALM9   0.003   P1
LM  MALM10  0.003   P1
LM  MALM11  0.004   P1
LM  MALM12  0.004   P1
LM  MALM13  0.02    P1
LM  MALM14  0.003   P1
LM  MALM15  0.003   P1
LM  MALM16  0.003   P1
LM  MALM17  0.004   P1
LM  MALM18  0.005   P1
LM  MALM19  0.01    P1
LM  MALM20  0.004   P1
UK  MAUK1   0.003   P1
UK  MAUK2   0.005   P1
UK  MAUK3   0.003   P1
UK  MAUK4   0.003   P1
UK  MAUK5   0.004   P1
UK  MAUK6   0.004   P1
UK  MAUK7   0.004   P1
UK  MAUK8   0.044   P1
UK  MAUK9   0.005   P1
UK  MAUK10  0.003   P1
UK  MAUK11  0.007   P1
UK  MAUK12  0.004   P1
UK  MAUK13  0.004   P1
UK  MAUK14  0.004   P1
UK  MAUK15  0.003   P1
UK  MAUK16  0.005   P1
UK  MAUK17  0.017   P1
UK  MAUK18  0.004   P1
UK  MAUK19  0.007   P1
UK  MAUK20  0.007   P1
MK  MAMK1   0.006   P1
MK  MAMK2   0.005   P1
MK  MAMK3   0.023   P1
MK  MAMK4   0.009   P1
MK  MAMK5   0.007   P1
MK  MAMK6   0.008   P1
MK  MAMK7   0.004   P1
MK  MAMK8   0.015   P1
MK  MAMK9   0.005   P1
MK  MAMK10  0.011   P1
MK  MAMK11  0.004   P1
MK  MAMK12  0.004   P1
MK  MAMK13  0.019   P1
MK  MAMK14  0.004   P1
MK  MAMK15  0.008   P1
MK  MAMK16  0.003   P1
MK  MAMK17  0.003   P1
MK  MAMK18  0.012   P1
MK  MAMK19  0.006   P1
MK  MAMK20  0.006   P1
LK  MALK1   0.009   P1
LK  MALK2   0.011   P1
LK  MALK3   0.004   P1
LK  MALK4   0.01    P1
LK  MALK5   0.022   P1
LK  MALK6   0.063   P1
LK  MALK7   0.081   P1
LK  MALK8   0.003   P1
LK  MALK9   0.005   P1
LK  MALK10  0.068   P1
LK  MALK11  0.003   P1
LK  MALK12  0.004   P1
LK  MALK13  0.004   P1
LK  MALK14  0.008   P1
LK  MALK15  0.006   P1
LK  MALK16  0.007   P1
LK  MALK17  0.011   P1
LK  MALK18  0.005   P1
LK  MALK19  0.007   P1
LK  MALK21  0.004   P1
UD  MAUD13  0.991   P2
UD  MAUD14  0.991   P2
UD  MAUD15  0.991   P2
UD  MAUD16  0.987   P2
UD  MAUD17  0.967   P2
UD  MAUD18  0.945   P2
UD  MAUD19  0.949   P2
UD  MAUD21  0.991   P2
UD  MAUD23  0.971   P2
MD  MAMD1   0.976   P2
MD  MAMD2   0.961   P2
MD  MAMD3   0.988   P2
MD  MAMD4   0.968   P2
MD  MAMD5   0.97    P2
MD  MAMD6   0.991   P2
MD  MAMD7   0.985   P2
LD  MALD1   0.967   P2
LD  MALD2   0.992   P2
LD  MALD3   0.984   P2
LD  MALD4   0.975   P2
LD  MALD5   0.97    P2
LD  MALD6   0.982   P2
LD  MALD7   0.969   P2
LD  MALD8   0.94    P2
LD  MALD9   0.981   P2
LD  MALD10  0.99    P2
LD  MALD12  0.951   P2
LD  MALD13  0.987   P2
LD  MALD14  0.986   P2
LD  MALD15  0.981   P2
LD  MALD16  0.991   P2
LD  MALD17  0.922   P2
LD  MALD18  0.991   P2
LD  MALD19  0.984   P2
LD  MALD20  0.992   P2
LD  MALD21  0.986   P2
LD  MALD23  0.979   P2
LD  MALD24  0.987   P2
LD  MALD25  0.989   P2
LD  MALD26  0.982   P2
LD  MALD27  0.988   P2
LD  MALD28  0.99    P2
LD  MALD29  0.992   P2
LD  MALD30  0.986   P2
UM  MAUM1   0.006   P2
UM  MAUM2   0.005   P2
UM  MAUM3   0.003   P2
UM  MAUM4   0.004   P2
UM  MAUM5   0.01    P2
UM  MAUM6   0.004   P2
UM  MAUM7   0.014   P2
UM  MAUM8   0.004   P2
UM  MAUM9   0.005   P2
UM  MAUM10  0.004   P2
UM  MAUM11  0.004   P2
UM  MAUM12  0.008   P2
UM  MAUM13  0.005   P2
UM  MAUM14  0.003   P2
UM  MAUM15  0.003   P2
UM  MAUM16  0.018   P2
UM  MAUM17  0.004   P2
UM  MAUM18  0.005   P2
UM  MAUM19  0.004   P2
UM  MAUM20  0.004   P2
MM  MAMM1   0.005   P2
MM  MAMM2   0.005   P2
MM  MAMM3   0.003   P2
MM  MAMM4   0.031   P2
MM  MAMM5   0.007   P2
MM  MAMM6   0.009   P2
MM  MAMM7   0.004   P2
MM  MAMM8   0.004   P2
MM  MAMM9   0.004   P2
MM  MAMM10  0.004   P2
MM  MAMM11  0.003   P2
MM  MAMM12  0.013   P2
MM  MAMM13  0.004   P2
MM  MAMM14  0.004   P2
MM  MAMM15  0.003   P2
MM  MAMM16  0.003   P2
MM  MAMM17  0.006   P2
MM  MAMM18  0.006   P2
MM  MAMM19  0.003   P2
MM  MAMM20  0.02    P2
LM  MALM1   0.003   P2
LM  MALM2   0.006   P2
LM  MALM3   0.009   P2
LM  MALM4   0.004   P2
LM  MALM5   0.006   P2
LM  MALM6   0.004   P2
LM  MALM7   0.004   P2
LM  MALM8   0.004   P2
LM  MALM9   0.003   P2
LM  MALM10  0.005   P2
LM  MALM11  0.009   P2
LM  MALM12  0.004   P2
LM  MALM13  0.008   P2
LM  MALM14  0.008   P2
LM  MALM15  0.012   P2
LM  MALM16  0.004   P2
LM  MALM17  0.009   P2
LM  MALM18  0.007   P2
LM  MALM19  0.024   P2
LM  MALM20  0.004   P2
UK  MAUK1   0.991   P2
UK  MAUK2   0.99    P2
UK  MAUK3   0.976   P2
UK  MAUK4   0.992   P2
UK  MAUK5   0.986   P2
UK  MAUK6   0.987   P2
UK  MAUK7   0.984   P2
UK  MAUK8   0.931   P2
UK  MAUK9   0.99    P2
UK  MAUK10  0.991   P2
UK  MAUK11  0.967   P2
UK  MAUK12  0.992   P2
UK  MAUK13  0.989   P2
UK  MAUK14  0.984   P2
UK  MAUK15  0.99    P2
UK  MAUK16  0.99    P2
UK  MAUK17  0.924   P2
UK  MAUK18  0.991   P2
UK  MAUK19  0.985   P2
UK  MAUK20  0.979   P2
MK  MAMK1   0.99    P2
MK  MAMK2   0.991   P2
MK  MAMK3   0.97    P2
MK  MAMK4   0.979   P2
MK  MAMK5   0.99    P2
MK  MAMK6   0.986   P2
MK  MAMK7   0.994   P2
MK  MAMK8   0.981   P2
MK  MAMK9   0.99    P2
MK  MAMK10  0.983   P2
MK  MAMK11  0.993   P2
MK  MAMK12  0.989   P2
MK  MAMK13  0.976   P2
MK  MAMK14  0.993   P2
MK  MAMK15  0.987   P2
MK  MAMK16  0.993   P2
MK  MAMK17  0.99    P2
MK  MAMK18  0.985   P2
MK  MAMK19  0.986   P2
MK  MAMK20  0.986   P2
LK  MALK1   0.968   P2
LK  MALK2   0.943   P2
LK  MALK3   0.977   P2
LK  MALK4   0.977   P2
LK  MALK5   0.812   P2
LK  MALK6   0.929   P2
LK  MALK7   0.879   P2
LK  MALK8   0.992   P2
LK  MALK9   0.988   P2
LK  MALK10  0.704   P2
LK  MALK11  0.992   P2
LK  MALK12  0.992   P2
LK  MALK13  0.985   P2
LK  MALK14  0.983   P2
LK  MALK15  0.987   P2
LK  MALK16  0.986   P2
LK  MALK17  0.985   P2
LK  MALK18  0.988   P2
LK  MALK19  0.981   P2
LK  MALK21  0.989   P2
UD  MAUD13  0.005   P3
UD  MAUD14  0.005   P3
UD  MAUD15  0.006   P3
UD  MAUD16  0.008   P3
UD  MAUD17  0.007   P3
UD  MAUD18  0.048   P3
UD  MAUD19  0.041   P3
UD  MAUD21  0.006   P3
UD  MAUD23  0.016   P3
MD  MAMD1   0.011   P3
MD  MAMD2   0.024   P3
MD  MAMD3   0.004   P3
MD  MAMD4   0.023   P3
MD  MAMD5   0.026   P3
MD  MAMD6   0.004   P3
MD  MAMD7   0.011   P3
LD  MALD1   0.017   P3
LD  MALD2   0.005   P3
LD  MALD3   0.009   P3
LD  MALD4   0.014   P3
LD  MALD5   0.026   P3
LD  MALD6   0.014   P3
LD  MALD7   0.005   P3
LD  MALD8   0.051   P3
LD  MALD9   0.013   P3
LD  MALD10  0.007   P3
LD  MALD12  0.044   P3
LD  MALD13  0.005   P3
LD  MALD14  0.01    P3
LD  MALD15  0.009   P3
LD  MALD16  0.005   P3
LD  MALD17  0.05    P3
LD  MALD18  0.005   P3
LD  MALD19  0.007   P3
LD  MALD20  0.006   P3
LD  MALD21  0.009   P3
LD  MALD23  0.003   P3
LD  MALD24  0.007   P3
LD  MALD25  0.008   P3
LD  MALD26  0.012   P3
LD  MALD27  0.006   P3
LD  MALD28  0.004   P3
LD  MALD29  0.004   P3
LD  MALD30  0.008   P3
UM  MAUM1   0.003   P3
UM  MAUM2   0.007   P3
UM  MAUM3   0.009   P3
UM  MAUM4   0.01    P3
UM  MAUM5   0.007   P3
UM  MAUM6   0.003   P3
UM  MAUM7   0.007   P3
UM  MAUM8   0.005   P3
UM  MAUM9   0.003   P3
UM  MAUM10  0.008   P3
UM  MAUM11  0.003   P3
UM  MAUM12  0.007   P3
UM  MAUM13  0.005   P3
UM  MAUM14  0.004   P3
UM  MAUM15  0.003   P3
UM  MAUM16  0.004   P3
UM  MAUM17  0.004   P3
UM  MAUM18  0.005   P3
UM  MAUM19  0.005   P3
UM  MAUM20  0.006   P3
MM  MAMM1   0.009   P3
MM  MAMM2   0.004   P3
MM  MAMM3   0.004   P3
MM  MAMM4   0.009   P3
MM  MAMM5   0.006   P3
MM  MAMM6   0.008   P3
MM  MAMM7   0.004   P3
MM  MAMM8   0.003   P3
MM  MAMM9   0.003   P3
MM  MAMM10  0.005   P3
MM  MAMM11  0.003   P3
MM  MAMM12  0.005   P3
MM  MAMM13  0.007   P3
MM  MAMM14  0.005   P3
MM  MAMM15  0.004   P3
MM  MAMM16  0.004   P3
MM  MAMM17  0.005   P3
MM  MAMM18  0.003   P3
MM  MAMM19  0.006   P3
MM  MAMM20  0.006   P3
LM  MALM1   0.993   P3
LM  MALM2   0.991   P3
LM  MALM3   0.985   P3
LM  MALM4   0.992   P3
LM  MALM5   0.99    P3
LM  MALM6   0.992   P3
LM  MALM7   0.993   P3
LM  MALM8   0.993   P3
LM  MALM9   0.994   P3
LM  MALM10  0.992   P3
LM  MALM11  0.987   P3
LM  MALM12  0.993   P3
LM  MALM13  0.972   P3
LM  MALM14  0.989   P3
LM  MALM15  0.986   P3
LM  MALM16  0.993   P3
LM  MALM17  0.986   P3
LM  MALM18  0.988   P3
LM  MALM19  0.966   P3
LM  MALM20  0.992   P3
UK  MAUK1   0.006   P3
UK  MAUK2   0.005   P3
UK  MAUK3   0.02    P3
UK  MAUK4   0.005   P3
UK  MAUK5   0.01    P3
UK  MAUK6   0.009   P3
UK  MAUK7   0.012   P3
UK  MAUK8   0.025   P3
UK  MAUK9   0.004   P3
UK  MAUK10  0.006   P3
UK  MAUK11  0.026   P3
UK  MAUK12  0.004   P3
UK  MAUK13  0.007   P3
UK  MAUK14  0.012   P3
UK  MAUK15  0.007   P3
UK  MAUK16  0.005   P3
UK  MAUK17  0.059   P3
UK  MAUK18  0.005   P3
UK  MAUK19  0.008   P3
UK  MAUK20  0.014   P3
MK  MAMK1   0.004   P3
MK  MAMK2   0.004   P3
MK  MAMK3   0.007   P3
MK  MAMK4   0.012   P3
MK  MAMK5   0.003   P3
MK  MAMK6   0.006   P3
MK  MAMK7   0.003   P3
MK  MAMK8   0.004   P3
MK  MAMK9   0.005   P3
MK  MAMK10  0.006   P3
MK  MAMK11  0.004   P3
MK  MAMK12  0.007   P3
MK  MAMK13  0.005   P3
MK  MAMK14  0.004   P3
MK  MAMK15  0.005   P3
MK  MAMK16  0.003   P3
MK  MAMK17  0.006   P3
MK  MAMK18  0.003   P3
MK  MAMK19  0.008   P3
MK  MAMK20  0.008   P3
LK  MALK1   0.024   P3
LK  MALK2   0.047   P3
LK  MALK3   0.02    P3
LK  MALK4   0.013   P3
LK  MALK5   0.166   P3
LK  MALK6   0.008   P3
LK  MALK7   0.04    P3
LK  MALK8   0.005   P3
LK  MALK9   0.007   P3
LK  MALK10  0.228   P3
LK  MALK11  0.005   P3
LK  MALK12  0.004   P3
LK  MALK13  0.011   P3
LK  MALK14  0.009   P3
LK  MALK15  0.007   P3
LK  MALK16  0.007   P3
LK  MALK17  0.004   P3
LK  MALK18  0.007   P3
LK  MALK19  0.012   P3
LK  MALK21  0.007   P3

使用代码:

pop <- read.csv("2019Nov5_StructurePlotV2_MA1.csv")

ggplot(data=pop, aes(x=Ind, y=pop_est, fill=K)) +
  geom_bar(stat="identity") +
  xlab("Population") + ylab("Membership Estimate")

我生成了以下图:(https://i.imgur.com/5crF4X3.png

这看起来像是我所追求的目标的开始,但是我需要x轴标签来反映人口群体(来自“群体”列)而不是每个个体(这显然很混乱)。

我是ggplot2的新手(总体来说R还是很绿色),所以我希望我有所了解。

我到处找了一些其他人,他们可能已经使用ggplot2来操纵STRUCTURE图,因为它是一种广泛使用的群体遗传工具,并且认为可能已经编写了一些管道。令人惊讶的是,情况似乎并非如此,因此,如果有人知道我在这方面可能被忽略的任何事情,请告诉我。

干杯!

1 个答案:

答案 0 :(得分:0)

这与我的示例情节差不多。主要技巧是使用刻面作为x轴标签。假设zz是您提供但引用的数据。

pop <- read.table(text = zz, header = T)

ggplot(data=pop, aes(x=Ind, y=pop_est, fill=K)) +
  # Friendly reminder, geom_col is shorthand for geom_bar(stat = "identity")
  geom_col(width = 1) +
  # Facet over the groups
  facet_grid(~ Group, switch = "x", scales = "free", space = "free") +
  # Use these expand arguments to get rid of padding around the axes
  scale_x_discrete(expand = c(0,0), name = "Population") +
  scale_y_continuous(expand = c(0,0), name = "Membership Estimate") +
  # We're hiding most of the x-axis and the spacing between panels
  theme(panel.spacing.x = grid::unit(0, "cm"),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        strip.background = element_blank(),
        panel.border = element_rect(fill = NA, colour = "black"))

enter image description here