R、 ggplot2,条形图上的唯一标签(基于系数)

R、 ggplot2,条形图上的唯一标签(基于系数),r,ggplot2,label,R,Ggplot2,Label,因此,我想为我的条添加唯一的标签,以显示哪个颜色描述哪个级别。我尝试过这里建议的聚合解决方案,但没有成功。 这是从我的数据中提取的一个小dput: testdata2<-structure(list(time = c(4L, 6L, 6L, 5L, 4L, 5L, 5L, 6L, 4L, 6L, 5L, 5L, 6L, 6L, 6L, 4L, 4L, 4L, 5L, 4L, 5L, 4L, 5L, 4L, 5L, 6L, 6L, 5L, 4L, 4L, 6L, 6L, 4L,

因此,我想为我的条添加唯一的标签,以显示哪个颜色描述哪个级别。我尝试过这里建议的聚合解决方案,但没有成功。 这是从我的数据中提取的一个小dput:

    testdata2<-structure(list(time = c(4L, 6L, 6L, 5L, 4L, 5L, 5L, 6L, 4L, 6L, 
5L, 5L, 6L, 6L, 6L, 4L, 4L, 4L, 5L, 4L, 5L, 4L, 5L, 4L, 5L, 6L, 
6L, 5L, 4L, 4L, 6L, 6L, 4L, 4L, 5L, 4L, 4L, 4L, 4L, 4L, 4L, 6L, 
4L, 4L, 4L, 6L, 4L, 4L, 4L, 4L, 5L, 5L, 4L, 4L, 4L, 6L, 4L, 6L, 
4L, 5L, 4L, 6L, 5L, 6L, 6L, 5L, 4L, 4L, 4L, 5L, 4L, 4L, 4L, 4L, 
6L, 6L, 6L, 4L, 4L, 4L, 4L, 5L, 5L, 6L, 4L, 4L, 5L, 4L, 4L, 5L, 
4L, 4L, 4L, 5L, 5L, 4L, 5L, 4L, 4L, 4L, 4L, 6L, 4L, 4L, 6L, 6L, 
4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 6L, 4L, 4L, 6L, 6L, 4L, 4L, 4L, 
4L, 5L, 6L, 6L, 4L, 4L, 6L, 5L, 4L, 4L, 6L, 6L, 6L, 5L, 5L, 5L, 
6L, 6L, 6L, 5L, 6L, 4L, 5L, 4L, 4L, 4L, 4L, 6L, 6L, 4L, 4L, 4L, 
4L), days = c(44L, 69L, 69L, 62L, 44L, 62L, 62L, 69L, 44L, 69L, 
62L, 62L, 69L, 69L, 69L, 44L, 44L, 44L, 62L, 44L, 62L, 44L, 62L, 
44L, 62L, 69L, 69L, 62L, 44L, 44L, 69L, 69L, 44L, 44L, 62L, 44L, 
44L, 44L, 44L, 44L, 44L, 69L, 44L, 44L, 44L, 69L, 44L, 44L, 44L, 
44L, 62L, 62L, 44L, 44L, 44L, 69L, 44L, 69L, 44L, 62L, 44L, 69L, 
62L, 69L, 69L, 62L, 44L, 44L, 44L, 62L, 44L, 44L, 44L, 44L, 69L, 
69L, 69L, 44L, 44L, 44L, 44L, 62L, 62L, 69L, 44L, 44L, 62L, 44L, 
44L, 62L, 44L, 44L, 44L, 62L, 62L, 44L, 62L, 44L, 44L, 44L, 44L, 
69L, 44L, 44L, 69L, 69L, 44L, 44L, 44L, 44L, 44L, 44L, 62L, 62L, 
69L, 44L, 44L, 69L, 69L, 44L, 44L, 44L, 44L, 62L, 69L, 69L, 44L, 
44L, 69L, 62L, 44L, 44L, 69L, 69L, 69L, 62L, 62L, 62L, 69L, 69L, 
69L, 62L, 69L, 44L, 62L, 44L, 44L, 44L, 44L, 69L, 69L, 44L, 44L, 
44L, 44L), value = c(15428463.1, 8275663.387, 6001381.183, 5851384.572, 
5504593.565, 5473207.122, 5433888.106, 3555472.492, 3311864.32, 
2878210.281, 2235464.82, 2043159.083, 2028397.199, 1977864.446, 
1700550.537, 1528101.872, 1460917.685, 1370737.581, 1312540.266, 
965378.0136, 862158.8019, 823795.2483, 534023.7474, 505910.3843, 
499708.9577, 455411.069, 359275.5795, 311692.5686, 311121.3589, 
308866.8563, 307202.181, 294260.8732, 291732.6364, 285420.0293, 
255216.1535, 250249.7888, 246642.5843, 246191.6839, 234919.1708, 
223646.658, 197494.4277, 195660.3706, 167734.9935, 120841.3395, 
117685.04, 109693.0586, 77103.9889, 75751.28739, 74849.48627, 
68987.77952, 66484.88279, 65769.99157, 65380.57539, 64027.87389, 
59518.86864, 53305.89636, 50951.75888, 44062.09922, 43737.35027, 
43608.36398, 41031.94726, 36667.06157, 35029.66932, 33277.66957, 
32969.54289, 30740.32225, 29759.4345, 29308.53376, 27504.93187, 
26450.97492, 26152.23002, 24348.62813, 23446.82701, 22995.92662, 
21568.85988, 21260.73321, 20952.60682, 19839.62288, 18486.92138, 
17134.21987, 17134.21987, 16442.49783, 16442.49783, 16330.70811, 
16232.41875, 15781.51837, 15727.60661, 14428.81651, 13977.91613, 
13582.93295, 13527.01574, 13076.11501, 13076.11501, 12868.04173, 
12153.1505, 11723.4135, 11438.25928, 11272.51312, 10821.61238, 
10821.61238, 10821.61238, 10476.30327, 10370.712, 10370.712, 
9860.0502, 9551.923524, 9468.91088, 9468.91088, 9468.91088, 9468.91088, 
9468.91088, 9468.91088, 9293.585873, 9293.585873, 9243.797134, 
9018.010496, 9018.010496, 8935.670458, 8935.670458, 7665.308992, 
7665.308992, 7214.408256, 7214.408256, 7148.91221, 6778.784584, 
6778.784584, 6763.507872, 6763.507872, 6470.657908, 6434.020989, 
6312.607136, 6312.607136, 6162.531518, 5854.404842, 5854.404842, 
5719.129768, 5719.129768, 5719.129768, 5546.278166, 5546.278166, 
5546.278166, 5004.238547, 4930.0251, 4509.005248, 4289.347326, 
4058.104512, 4058.104512, 4058.104512, 4058.104512, 4005.645358, 
3697.518968, 3607.204128, 3607.204128, 3607.204128, 3607.204128
), genus = structure(c(78L, 22L, 28L, 64L, 60L, 22L, 60L, 60L, 
64L, 64L, 28L, 78L, 12L, 39L, 78L, 78L, 61L, 39L, 39L, 72L, 41L, 
41L, 12L, 28L, 78L, 72L, 41L, 72L, 22L, 11L, 16L, 63L, 26L, 1L, 
16L, 16L, 5L, 30L, 57L, 23L, 78L, 78L, 36L, 66L, 12L, 35L, 49L, 
69L, 50L, 40L, 26L, 63L, 53L, 24L, 18L, 1L, 15L, 59L, 31L, 18L, 
1L, 47L, 35L, 78L, 5L, 59L, 76L, 58L, 62L, 5L, 8L, 55L, 4L, 77L, 
15L, 2L, 40L, 65L, 1L, 6L, 78L, 11L, 40L, 26L, 78L, 52L, 78L, 
33L, 32L, 47L, 14L, 29L, 63L, 1L, 74L, 54L, 25L, 21L, 42L, 46L, 
79L, 30L, 9L, 13L, 75L, 11L, 1L, 7L, 20L, 37L, 67L, 68L, 2L, 
4L, 29L, 2L, 19L, 25L, 74L, 3L, 27L, 1L, 44L, 27L, 10L, 18L, 
45L, 51L, 27L, 36L, 43L, 70L, 20L, 4L, 73L, 20L, 29L, 77L, 1L, 
43L, 71L, 75L, 13L, 38L, 71L, 17L, 25L, 78L, 78L, 44L, 1L, 1L, 
34L, 48L, 56L), .Label = c("", "Achromobacter", "Acinetobacter", 
"Aeromonas", "Ahrensia", "Algoriphagus", "Aquabacterium", "Arcobacter", 
"Arthrobacter", "Aureimonas", "BAL58-marine-group", "Blastomonas", 
"Bradyrhizobium", "Brevundimonas", "Caulobacter", "Cupriavidus", 
"Defluviicoccus", "Defluviimonas", "Diplosphaera", "Dokdonella", 
"Dyadobacter", "Ferrovibrio", "Flavobacterium", "Fluviicola", 
"Gemmobacter", "GKS98-freshwater-group", "Haematobacter", "Hoeflea", 
"Hyphomonas", "Kerstersia", "Ketogulonicigenium", "Kordiimonas", 
"Lentibacter", "Limnobacter", "Limnohabitans", "Loktanella", 
"Marinomonas", "Marivita", "Massilia", "Mesorhizobium", "Methylotenera", 
"Nisaea", "Nitratireductor", "Novosphingobium", "Oceanibaculum", 
"Oceanisphaera", "Ochrobactrum", "OM43-clade", "Opitutus", "Owenweeksia", 
"Paracocccus", "Parapusillimonas", "Pedobacter", "Peredibacter", 
"Perlucidibaca", "Phyllobacterium", "Polaribacter", "Pseudoalteromonas", 
"Pseudolabrys", "Pseudomonas", "Pseudorhodobacter", "Pseudospirillum", 
"Rhizobium", "Rhodobacter", "Roseicitreum", "Roseivirga", "Roseobacter-clade-CHAB-I-5-lineage", 
"SAR92-clade", "Seohaeicola", "Shinella", "Sphingomonas", "Sphingopyxis", 
"Sphingorhabdus", "Stappia", "Terrimonas", "Thalassobaculum", 
"Thalassospira", "uncultured", "Undibacterium"), class = "factor"), 
    family = structure(c(33L, 34L, 28L, 33L, 31L, 34L, 31L, 31L, 
    33L, 33L, 28L, 34L, 37L, 27L, 34L, 34L, 33L, 27L, 27L, 37L, 
    20L, 20L, 37L, 28L, 33L, 37L, 20L, 37L, 34L, 12L, 8L, 32L, 
    3L, 23L, 8L, 8L, 28L, 3L, 17L, 17L, 10L, 33L, 33L, 16L, 37L, 
    12L, 26L, 33L, 13L, 28L, 3L, 32L, 36L, 13L, 33L, 1L, 10L, 
    39L, 33L, 33L, 33L, 7L, 12L, 10L, 28L, 39L, 34L, 30L, 24L, 
    28L, 9L, 22L, 2L, 34L, 10L, 3L, 28L, 33L, 38L, 14L, 13L, 
    12L, 28L, 3L, 12L, 3L, 10L, 33L, 19L, 7L, 10L, 18L, 32L, 
    1L, 33L, 5L, 33L, 15L, 34L, 2L, 27L, 3L, 21L, 6L, 11L, 12L, 
    25L, 12L, 40L, 24L, 33L, 29L, 3L, 2L, 18L, 3L, 26L, 33L, 
    33L, 22L, 33L, 35L, 37L, 33L, 4L, 33L, 34L, 33L, 33L, 33L, 
    28L, 32L, 40L, 2L, 37L, 40L, 18L, 34L, 1L, 28L, 37L, 11L, 
    6L, 33L, 37L, 34L, 33L, 3L, 18L, 37L, 38L, 1L, 8L, 20L, 28L
    ), .Label = c("", "Aeromonadaceae", "Alcaligenaceae", "Aurantimonadaceae", 
    "Bacteriovoracaceae", "Bradyrhizobiaceae", "Brucellaceae", 
    "Burkholderiaceae", "Campylobacteraceae", "Caulobacteraceae", 
    "Chitinophagaceae", "Comamonadaceae", "Cryomorphaceae", "Cyclobacteriaceae", 
    "Cytophagaceae", "Flammeovirgaceae", "Flavobacteriaceae", 
    "Hyphomonadaceae", "Kordiimonadaceae", "Methylophilaceae", 
    "Micrococcaceae", "Moraxellaceae", "NS11-12-marine-group", 
    "Oceanospirillaceae", "OPB56", "Opitutaceae", "Oxalobacteraceae", 
    "Phyllobacteriaceae", "Porticoccaceae", "Pseudoalteromonadaceae", 
    "Pseudomonadaceae", "Rhizobiaceae", "Rhodobacteraceae", "Rhodospirillaceae", 
    "SHWN-night2", "Sphingobacteriaceae", "Sphingomonadaceae", 
    "uncultured", "Xanthobacteraceae", "Xanthomonadaceae"), class = "factor"), 
    order = structure(c(22L, 23L, 21L, 22L, 20L, 23L, 20L, 20L, 
    22L, 22L, 21L, 23L, 26L, 5L, 23L, 23L, 22L, 5L, 5L, 26L, 
    15L, 15L, 26L, 21L, 22L, 26L, 15L, 26L, 23L, 5L, 5L, 21L, 
    5L, 25L, 5L, 5L, 21L, 5L, 11L, 11L, 7L, 22L, 22L, 10L, 26L, 
    5L, 19L, 22L, 11L, 21L, 5L, 21L, 25L, 11L, 22L, 13L, 7L, 
    21L, 22L, 22L, 22L, 21L, 5L, 7L, 21L, 21L, 23L, 3L, 18L, 
    21L, 6L, 20L, 2L, 23L, 7L, 5L, 21L, 22L, 12L, 10L, 11L, 5L, 
    21L, 5L, 5L, 5L, 7L, 22L, 14L, 21L, 7L, 7L, 21L, 1L, 22L, 
    4L, 22L, 10L, 23L, 2L, 5L, 5L, 16L, 21L, 25L, 5L, 9L, 5L, 
    27L, 18L, 22L, 8L, 5L, 2L, 7L, 5L, 19L, 22L, 22L, 20L, 22L, 
    24L, 26L, 22L, 21L, 22L, 23L, 22L, 22L, 22L, 21L, 21L, 27L, 
    2L, 26L, 27L, 7L, 23L, 1L, 21L, 26L, 25L, 21L, 22L, 26L, 
    23L, 22L, 5L, 7L, 26L, 12L, 17L, 5L, 15L, 21L), .Label = c("", 
    "Aeromonadales", "Alteromonadales", "Bdellovibrionales", 
    "Burkholderiales", "Campylobacterales", "Caulobacterales", 
    "Cellvibrionales", "Chlorobiales", "Cytophagales", "Flavobacteriales", 
    "Gaiellales", "Hot-Creek-32", "Kordiimonadales", "Methylophilales", 
    "Micrococcales", "oca12", "Oceanospirillales", "Opitutales", 
    "Pseudomonadales", "Rhizobiales", "Rhodobacterales", "Rhodospirillales", 
    "Rickettsiales", "Sphingobacteriales", "Sphingomonadales", 
    "Xanthomonadales"), class = "factor"), class = structure(c(2L, 
    2L, 2L, 2L, 10L, 2L, 10L, 10L, 2L, 2L, 2L, 2L, 2L, 4L, 2L, 
    2L, 2L, 4L, 4L, 2L, 4L, 4L, 2L, 2L, 2L, 2L, 4L, 2L, 2L, 4L, 
    4L, 2L, 4L, 12L, 4L, 4L, 2L, 4L, 9L, 9L, 2L, 2L, 2L, 6L, 
    2L, 4L, 11L, 2L, 9L, 2L, 4L, 2L, 12L, 9L, 2L, 4L, 2L, 2L, 
    2L, 2L, 2L, 2L, 4L, 2L, 2L, 2L, 2L, 10L, 10L, 2L, 8L, 10L, 
    10L, 2L, 2L, 4L, 2L, 2L, 13L, 6L, 9L, 4L, 2L, 4L, 4L, 4L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 7L, 2L, 6L, 2L, 10L, 
    4L, 4L, 1L, 2L, 12L, 4L, 5L, 4L, 10L, 10L, 2L, 10L, 4L, 10L, 
    2L, 4L, 11L, 2L, 2L, 10L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 10L, 10L, 2L, 10L, 2L, 2L, 3L, 2L, 2L, 12L, 
    2L, 2L, 2L, 2L, 2L, 4L, 2L, 2L, 13L, 4L, 4L, 4L, 2L), .Label = c("Actinobacteria", 
    "Alphaproteobacteria", "ARKICE-90", "Betaproteobacteria", 
    "Chlorobia", "Cytophagia", "Deltaproteobacteria", "Epsilonproteobacteria", 
    "Flavobacteriia", "Gammaproteobacteria", "Opitutae", "Sphingobacteriia", 
    "Thermoleophilia"), class = "factor"), phylum = structure(c(4L, 
    4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
    4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
    4L, 4L, 2L, 4L, 4L, 4L, 4L, 2L, 2L, 4L, 4L, 4L, 2L, 4L, 4L, 
    5L, 4L, 2L, 4L, 4L, 4L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
    4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
    4L, 4L, 1L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
    4L, 4L, 4L, 4L, 4L, 4L, 2L, 4L, 4L, 4L, 4L, 1L, 4L, 2L, 4L, 
    3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 4L, 4L, 4L, 4L, 
    4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
    4L, 4L, 4L, 4L, 4L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 
    4L, 4L, 4L, 4L), .Label = c("Actinobacteria", "Bacteroidetes", 
    "Chlorobi", "Proteobacteria", "Verrucomicrobia"), class = "factor")), .Names = c("time", 
"days", "value", "genus", "family", "order", "class", "phylum"
), class = "data.frame", row.names = c(NA, -155L))

testdata2这里有一个清理问题的方法

对订单和天数的值求和-这将删除订单的重复

过滤掉较低的值,以删除标记的内容,这些内容非常微小,以至于在图形上几乎看不到填充

library(tidiverse)
testdata2 %>%
  group_by(order, days) %>%
  summarise(val_sum = sum(value)) %>%
  mutate(days = as.factor(days)) %>%
  filter(val_sum > 500000) %>%
  ggplot(aes(x = days, y = val_sum, fill=order))+       
  geom_bar(stat = "identity")+
  geom_text(aes(label=order, hjust = 0.5, vjust = 1), position="stack",size=3)+
  theme(legend.position='none')

不进行过滤:

testdata2 %>%
  group_by(order, days) %>%
  summarise(val_sum = sum(value)) %>%
  mutate(days = as.factor(days)) %>%
  ggplot(aes(x = days, y = val_sum, fill=order))+       
  geom_bar(stat = "identity")+
  geom_text(aes(label=order, hjust = 0.5, vjust = 1), position="stack",size=3)+
  theme(legend.position='none')

这里有一种清理方法

对订单和天数的值求和-这将删除订单的重复

过滤掉较低的值,以删除标记的内容,这些内容非常微小,以至于在图形上几乎看不到填充

library(tidiverse)
testdata2 %>%
  group_by(order, days) %>%
  summarise(val_sum = sum(value)) %>%
  mutate(days = as.factor(days)) %>%
  filter(val_sum > 500000) %>%
  ggplot(aes(x = days, y = val_sum, fill=order))+       
  geom_bar(stat = "identity")+
  geom_text(aes(label=order, hjust = 0.5, vjust = 1), position="stack",size=3)+
  theme(legend.position='none')

不进行过滤:

testdata2 %>%
  group_by(order, days) %>%
  summarise(val_sum = sum(value)) %>%
  mutate(days = as.factor(days)) %>%
  ggplot(aes(x = days, y = val_sum, fill=order))+       
  geom_bar(stat = "identity")+
  geom_text(aes(label=order, hjust = 0.5, vjust = 1), position="stack",size=3)+
  theme(legend.position='none')

如果每行有多个
顺序
s,您希望将标签绘制在哪个y值?@ZachLipp:hm这不是很容易回答的问题。它可能位于missuse在其回答中显示的顶部,也可能位于barIf指定部分的中心,每行有多个
顺序
s,您希望将标签绘制在哪个y值?@ZachLipp:hm这不是很容易回答的问题。它可能位于missuse在回答中所示的顶部,也可能位于barThanks指定部分的中心,看起来很棒!%>%的意思是将数据“从左手边”推送到ggplot调用中,对吗?%>%称为管道。它来自于软件包,它提供了一种从左到右编程的方法,而不是在R中从内到外编程。它需要一些时间来适应,但它产生了更干净的代码(更易于阅读和调试),并最终大大加快了编码速度。它在
tidyverse
中被大量使用,这是一套受欢迎的数据争用软件包。谢谢,看起来很棒!%>%的意思是将数据“从左手边”推送到ggplot调用中,对吗?%>%称为管道。它来自于软件包,它提供了一种从左到右编程的方法,而不是在R中从内到外编程。它需要一些时间来适应,但它产生了更干净的代码(更易于阅读和调试),并最终大大加快了编码速度。它在
tidyverse
中大量使用,tidyverse是一组流行的数据争用包。