Open mr2raccoon opened 3 years ago
@mr2raccoon Instead of fill
, you need to use color
, so you can use one of the following to change legend title:
# example dataset
data(GlobalPatterns)
ord <- ordinate(GlobalPatterns, "NMDS", "bray")
# using guides
plot_ordination(GlobalPatterns, ord, type = "samples", color = "SampleType") +
guides(color = guide_legend(title = "New Legend Title")) +
geom_point(size=5) + ggtitle("Bray-Curtis-Index")
# using labs
plot_ordination(GlobalPatterns, ord, type = "samples", color = "SampleType") +
geom_point(size = 5) + labs(title = "Bray-Curtis-Index", color = "New Legend Title")
Thanks I could change the title- But how to change the annotation for each point inside the legend? I tried p1 + guides(color=guide_legend(title="Probe", values = c("Illumina MB5", "Nanopore MB5", "Illumina MB6","Nanopore MB6" )))
and p1 + guides(color=guide_legend(title="Probe", labels = c("Illumina MB5", "Nanopore MB5", "Illumina MB6","Nanopore MB6" )))
@mr2raccoon I would suggest you rename the labels in the Sample Data, it saves you the trouble to change it again and again in all the rest of the plots. Using the example data:
# Check your Sample Data structure
> sample_data(GlobalPatterns)[1:10]
Sample Data: [10 samples by 7 sample variables]:
X.SampleID Primer Final_Barcode Barcode_truncated_plus_T Barcode_full_length SampleType Description
CL3 CL3 ILBC_01 AACGCA TGCGTT CTAGCGTGCGT Soil Calhoun South Carolina Pine soil, pH 4.9
CC1 CC1 ILBC_02 AACTCG CGAGTT CATCGACGAGT Soil Cedar Creek Minnesota, grassland, pH 6.1
SV1 SV1 ILBC_03 AACTGT ACAGTT GTACGCACAGT Soil Sevilleta new Mexico, desert scrub, pH 8.3
M31Fcsw M31Fcsw ILBC_04 AAGAGA TCTCTT TCGACATCTCT Feces M3, Day 1, fecal swab, whole body study
M11Fcsw M11Fcsw ILBC_05 AAGCTG CAGCTT CGACTGCAGCT Feces M1, Day 1, fecal swab, whole body study
M31Plmr M31Plmr ILBC_07 AATCGT ACGATT CGAGTCACGAT Skin M3, Day 1, right palm, whole body study
M11Plmr M11Plmr ILBC_08 ACACAC GTGTGT GCCATAGTGTG Skin M1, Day 1, right palm, whole body study
F21Plmr F21Plmr ILBC_09 ACACAT ATGTGT GTAGACATGTG Skin F1, Day 1, right palm, whole body study
M31Tong M31Tong ILBC_10 ACACGA TCGTGT TGTGGCTCGTG Tongue M3, Day 1, tongue, whole body study
M11Tong M11Tong ILBC_11 ACACGG CCGTGT TAGACACCGTG Tongue M1, Day 1, tongue, whole body study
# Check if SampleType is a factor variable
> is.factor(sample_data(GlobalPatterns)$SampleType)
[1] TRUE
# Given SampleType is a factor variable, check the levels
> levels(sample_data(GlobalPatterns)$SampleType)
[1] "Feces" "Freshwater" "Freshwater (creek)" "Mock" "Ocean" "Sediment (estuary)" "Skin"
[8] "Soil" "Tongue"
# Change labels
levels(sample_data(GlobalPatterns)$SampleType) = c("Human 1", "Env 1", "Env 2", "Mock", "Env 3", "Env 4", "Human 2", "Env 5", "Human 3")
# SampleType labels is now updated
> sample_data(GlobalPatterns)[1:10]
Sample Data: [10 samples by 7 sample variables]:
X.SampleID Primer Final_Barcode Barcode_truncated_plus_T Barcode_full_length SampleType Description
CL3 CL3 ILBC_01 AACGCA TGCGTT CTAGCGTGCGT Env 5 Calhoun South Carolina Pine soil, pH 4.9
CC1 CC1 ILBC_02 AACTCG CGAGTT CATCGACGAGT Env 5 Cedar Creek Minnesota, grassland, pH 6.1
SV1 SV1 ILBC_03 AACTGT ACAGTT GTACGCACAGT Env 5 Sevilleta new Mexico, desert scrub, pH 8.3
M31Fcsw M31Fcsw ILBC_04 AAGAGA TCTCTT TCGACATCTCT Human 1 M3, Day 1, fecal swab, whole body study
M11Fcsw M11Fcsw ILBC_05 AAGCTG CAGCTT CGACTGCAGCT Human 1 M1, Day 1, fecal swab, whole body study
M31Plmr M31Plmr ILBC_07 AATCGT ACGATT CGAGTCACGAT Human 2 M3, Day 1, right palm, whole body study
M11Plmr M11Plmr ILBC_08 ACACAC GTGTGT GCCATAGTGTG Human 2 M1, Day 1, right palm, whole body study
F21Plmr F21Plmr ILBC_09 ACACAT ATGTGT GTAGACATGTG Human 2 F1, Day 1, right palm, whole body study
M31Tong M31Tong ILBC_10 ACACGA TCGTGT TGTGGCTCGTG Human 3 M3, Day 1, tongue, whole body study
M11Tong M11Tong ILBC_11 ACACGG CCGTGT TAGACACCGTG Human 3 M1, Day 1, tongue, whole body study
If you want to keep the original labels, you can create a new column to store the new labels you wish to use, and use that in all your plots. Using the example data:
# Create a new column NewSampleType with the same labels as SampleType
sample_data(GlobalPatterns)$NewSampleType = sample_data(GlobalPatterns)$SampleType
# Change the labels
levels(sample_data(GlobalPatterns)$NewSampleType) = c("Human 1", "Env 1", "Env 2", "Mock", "Env 3", "Env 4", "Human 2", "Env 5", "Human 3")
# Plot
plot_ordination(GlobalPatterns, ord, type = "samples", color = "NewSampleType") +
guides(color = guide_legend(title = "New Legend Title")) +
geom_point(size = 5) + ggtitle("Bray-Curtis-Index")
Hey,
I drew a Bray-curtis ordination plot but I cannot change any specifications in the legend.
All usual ggplot arguments fail: like
p1 <- p1 + guides(fill=guide_legend(title="New Legend Title"))
orlabs("title")
Did anyone experience the same problem and knows a way around?