Reputation: 1
I have a graph in R using ggplot that displays the results of a distance based redundancy analysis.
The code for the redundancy analysis is as so:
#Create environment file of subsetted variables
env <- as.data.frame(cbind(metadata$ph, metadata$lat, metadata$long, metadata$slope, metadata$aspect, metadata$elevationcm, metadata$peatdepth, metadata$calluna, metadata$moss, metadata$sphag, metadata$sedge, metadata$grass, metadata$bare, metadata$doc, metadata$n, metadata$suva, metadata$hazen, metadata$moisturem, metadata$e4e6, metadata$dnamgg, metadata$abbymoisture, metadata$bdgcm3))
colnames(env)<-c("ph", "lat","long","slope","aspect","elevationcm","peatdepth", "calluna","moss","sphag","sedge","grass","bare","doc","n","suva","hazen","moisturem","e4e6","dnamgg","abbymoisture","bdgcm3")
# use the following on sites that dont have molinea
env <- as.data.frame(cbind(metadata$lat, metadata$long, metadata$slope, metadata$aspect, metadata$elevationcm, metadata$peatdepth, metadata$calluna, metadata$moss, metadata$sphag, metadata$sedge, metadata$bare, metadata$doc, metadata$n, metadata$suva, metadata$hazen, metadata$moisturem, metadata$e4e6, metadata$dnamgg, metadata$abbymoisture, metadata$bdgcm3, metadata$lightmolm2, metadata$rainfallmm, metadata$tair, metadata$tsoil, metadata$lightcol, metadata$rainfallcol, metadata$taircol, metadata$tsoilcol))
colnames(env)<-c("lat","long","slope","aspect","elevationcm","peatdepth","calluna","moss","sphag","sedge","bare","doc","n","suva","hazen","moisturem","e4e6","dnamgg","abbymoisture","bdgcm3", "lightmolm2", "rainfallmm", "tair", "tsoil", "lightcol", "rainfallcol","taircol", "tsoilcol")
#F
dbRDA1<-capscale(dist ~ lat+long+slope+aspect+elevationcm+peatdepth+ph+calluna+moss+sphag+grass+sedge+bare+doc+n+suva+hazen+moisturem+e4e6+bdgcm3, data=env, na.action = "na.omit")
summary(dbRDA1)
#Test the overall model
test1<-anova(dbRDA1, perm = 9999)## SIGNIFICANT
test1
#Test which variables are significant
test2<-anova(dbRDA1, by="terms", permu=9999)
test2
#Test which axes are significant
test3<-anova(dbRDA1, by="axis", permu=9999)
test3
plot(dbRDA1)
As you can see, the variables within have specific names. The following code uses ggplot to extract the significant variables, plot them on to the dbRDA, and then colour the RDA by a factor called "manage":
### Plot a dbRDA
metadata2<-na.omit(metadata) #remove row with missing variable
smry <- summary(dbRDA1)
df1 <- data.frame(smry$sites[,1:4]) # PC1 and PC2
df1$location<-metadata2$manage
df1 <- df1[order(df1$manage),]
find_hull <- function(df1) df1[chull(df1$CAP1, df1$CAP2), ]
library(plyr)
hulls <- ddply(df1, "manage", find_hull)
cent<-aggregate(cbind(df1$CAP1,df1$CAP2) ~ manage, data = df1, FUN = mean)
segs<-merge(df1, setNames(cent, c('manage', 'V1','V2')),
by = 'manage', sort = TRUE)
df1$seg1<-segs$V1
df1$seg2<-segs$V2
df2 <- data.frame(smry$biplot[,1:2]) # loadings for PC1 and PC2
#subset df2 with only significant variables
test2<-na.omit(test2)
df2$p<-as.numeric(test2$`Pr(>F)`)
df2<-subset(df2, p < 0.05)
#Plot with sig variables and no ellipses / hulls etc
rda.plot <- ggplot(df1, aes(x=CAP1, y=CAP2, color = metadata2$manage)) +
geom_point(size=3) +
scale_color_manual(values=c("red", "green", "orange","yellow", "blue", "pink",
"#AD6F3B", "purple"), name="Management/Condition",
breaks=c("F", "M", "U", "MH", "10Y", "5R", "D", "I"),
labels=c("Rotationally Burnt (Grouse Moor)","Mown (Grouse Moor)", "Unmanaged (uncut Grouse Moor)", "80y post burn (ex-Grouse Moor)",
"10y post restoration", "5y post restoration", "Degraded", "Intact")) +
geom_hline(yintercept=0, linetype="dotted") +
geom_vline(xintercept=0, linetype="dotted") +
theme_classic()
rda.biplot <- rda.plot +
geom_segment(data=df2, aes(x=0, xend=CAP1, y=0, yend=CAP2),
color="black", arrow=arrow(length=unit(0.01,"npc"))) +
geom_text(data=df2,
aes(x=CAP1,y=CAP2,label=rownames(df2),
hjust=0.5*(1-sign(CAP1)),vjust=0.5*(1-sign(CAP2))),
color="black", size=4.5)
rda.biplot
This produces a graph that looks like this: dbRDA plot
However, you'll see that the variables are named as they are in the original data, which I can't change (involves editing lots of other scripts for changed variable names). Instead I'd rather rename these in the plot, but I can't think of a way to do this because geom.text takes the labels from df2 - and the added bits I've put into geom-text don't seem to work.
Does anyone know how I can change the text labels on the graph, e.g from "sedge" to "Sedge abundance" TIA
Upvotes: 0
Views: 600
Reputation: 171
I assume your data frame looks like the following
df2 <- data.frame(CAP1 = c(0.5,0.3,0.2), CAP2 = c(0.2,0.3,0.5))
rownames(df2) <- c("sedge", "Soil.Moisture", "air.temp")
When you don't want to change the rownames of df2 directly, it should be possible to make a vector of the same length as the number of rows of df2 containing the names you wish to have and forward it to the label-argument of geom_text:
vec <- c("Sedge Abundance", "Soil Moisture", "air temperature")
rda.biplot <- rda.plot +
geom_segment(data=df2, aes(x=0, xend=CAP1, y=0, yend=CAP2),
color="black",
arrow=arrow(length=unit(0.01,"npc"))) +
geom_text(data=df2,
aes(x=CAP1,y=CAP2,label=vec,
hjust=0.5*(1-sign(CAP1)),
vjust=0.5*(1-sign(CAP2))),
color="black", size=4.5)
In general, providing a short reproducible example may help you to get correct answers to your question, e.g., it would help here to know what df2 looks like (see e.g., here).
Upvotes: 1