jfq3 / ggordiplots

Make ggplot Versions of Vegan's Ordiplots

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Changing line type and color using ggordiplot

gcuster1991 opened this issue · comments

Hi @jfq3 ,
I am trying to color and format (type) the lines on a plot based on different columns of metadata from which I make the plot. I make the attached plot using the following code:

library(ggordiplots)
m <- gg_ordiplot(ord, sd_ps$Infestation_Stage_Soil, choices = c(1, 2), kind = c("se"), conf = 0.95, show.groups = "all", ellipse = TRUE, label = TRUE, hull = FALSE, spiders = FALSE, plot = TRUE, pt.size = 1.5) 
m<-m$plot + theme_bw() 
m <-m + annotate("text", x = -1, y = -1, label = "Soil Origin: p > 0.15")
m <-m + annotate("text", x = -1, y = -1.15, label = "Infestation Stage: p < 0.001")
m <-m + annotate("text", x = -1, y = -1.3, label = "K=3, Stress=0.179")
m <- m + ggtitle("NMDS of Infestation Stage and Soil Origin", ) +
  theme(plot.title = element_text(hjust = 0.5, size = 22)) 
m <- m + guides(color=guide_legend("Infestation Stage and Soil Origin"))
m

image

What I would like to do is have a dashed line and a solid line for the two Soil Origins (bulk and rhizosphere) and a then use only three colors for the three infestation stages (healthy (green), infested (red) and dead (grey)).

The data for these two factors are stored in my metadata (sd_ps$Soil and sd_ps$Infestation_Stage)

I have tried the following argument but i get an error stating :
additional line of code:

m<-m+ geom_line(data = sd_ps, aes(linetype = Soil))

Error:
Error in order(data$PANEL, data$group, data$x) : argument 3 is not a vector

I would like to use this package because of the "se" option for the ellipses. I have not been able to incorporate this option into any other ggplot option.

Any ideas?

Thanks,
Gordon

commented

Whether they are added to the plot or not, the ggordiplot functions silently return data frames for all of the objects they could add to the plot. So the approach I would take would be to make the plot with ellipse = FALSE, kind="se", conf = 0.95 and then add the ellipses from the data frame df_ellipse. The ellipses could be added one at a time as I have done in the code below, or you could get fancier and add columns to df_ellipse to control color and line type. Line type can be specified with digits: 1 is for solid, 2 is for dashed, etc. You may find that lines other than solid do not show up well unless you increase the width a bit.
If you do plot the ellipses in only three colors, I think you should also plot your points in only three colors, and add shapes to distinguish soil origin. I did not write gg_ordiplot to handle more than one grouping variable, so to do this you would have to use a modified version of df_ord to make your base plot.

library(ggordiplots)
data("dune")
data("dune.env")
dune.hel <- decostand(dune, method = "hellinger")
ord <- rda(dune.hel)
plt <- gg_ordiplot(ord, groups = dune.env$Management, label = TRUE, ellipse = FALSE, kind = "se", conf = 0.95)

df_ellipse <- plt$df_ellipse

BF_ellipse <- subset(df_ellipse, Group == "BF", droplevels= TRUE)
HF_ellipse <- subset(df_ellipse, Group == "HF", droplevels= TRUE)
NM_ellipse <- subset(df_ellipse, Group == "NM", droplevels= TRUE)
SF_ellipse <- subset(df_ellipse, Group == "SF", droplevels= TRUE)

plt$plot +
  geom_path(data=BF_ellipse, aes(x=x, y=y), color="Red", linetype="solid") +
  geom_path(data=HF_ellipse, aes(x=x, y=y), color="Green", linetype="dashed", size=1.25) +
  geom_path(data=NM_ellipse, aes(x=x, y=y), color="Blue", linetype="dotdash", size=1.25) +
  geom_path(data=SF_ellipse, aes(x=x, y=y), color="Purple", linetype="dotted", size=1.25)

rplot

@jfq3 , Thank you for the help. I was able to get the lines and change the shape of the points. I am now stuck on how to produce a legend for the line color and style. I have ran the code below and produced the following graph, but it only includes the two legends ,one for the point shape and one for point color. Any suggestions or ideas as to how to approach this problem?

library(ggordiplots)
d <- gg_ordiplot(ord, sd_ps$Infestation_Stage_Soil, choices = c(1, 2), kind = c("se"), conf = 0.95, show.groups = "all", ellipse = FALSE, label = FALSE, hull = FALSE, spiders = FALSE, plot = TRUE, pt.size =1) 
df_ellipse <- d$df_ellipse
dR_ellipse <- subset(df_ellipse, Group == "Dead_Rhizosphere", droplevels= TRUE)
dB_ellipse <- subset(df_ellipse, Group == "Dead_Bulk", droplevels= TRUE)
iR_ellipse <- subset(df_ellipse, Group == "Infested_Rhizosphere", droplevels= TRUE)
iB_ellipse <- subset(df_ellipse, Group == "Infested_Bulk", droplevels= TRUE)
hR_ellipse <- subset(df_ellipse, Group == "Healthy_Rhizosphere", droplevels= TRUE)
hB_ellipse <- subset(df_ellipse, Group == "Healthy_Bulk", droplevels= TRUE)

test<-d$df_ord
test$Infestation_Stage<-substr(rownames(test), start = 1, stop = 1)
test$Soil_Origin<-substr(rownames(test), start = 4, stop = 4)
index <- c("H", "I", "D")
values <- c("Healthy", "Infested", "Dead")
test$Infestation_Stage<- values[match(test$Infestation_Stage, index)]
index <- c("A", "B")
values <- c("Rhizosphere", "Bulk")
test$Soil_Origin<- values[match(test$Soil_Origin, index)]

test$Infestation_Stage <- factor(test$Infestation_Stage, 
                              levels = c("Healthy", "Infested", "Dead"), ordered = TRUE)


plot_test<-ggplot(test, aes(x=test$x, y=test$y), color=Infestation_Stage) + geom_point(aes(color = Infestation_Stage, shape=Soil_Origin))

plot_test<-plot_test +
  geom_path(data=dR_ellipse, aes(x=x, y=y), color="Gray", linetype="solid") +
  geom_path(data=dB_ellipse, aes(x=x, y=y), color="Gray", linetype="dashed", size=1.25) +
  geom_path(data=iR_ellipse, aes(x=x, y=y), color="Red", linetype="solid", size=1.25) +
  geom_path(data=iB_ellipse, aes(x=x, y=y), color="Red", linetype="dashed", size=1.25) +
  geom_path(data=hR_ellipse, aes(x=x, y=y), color="Forest green", linetype="solid", size=1.25) +
  geom_path(data=hB_ellipse, aes(x=x, y=y), color="Forest green", linetype="dashed", size=1.25) 

plot_test<- plot_test + scale_color_manual(values=c(colorspalette)) + theme_bw()
m<-plot_test
m <-m + annotate("text", x = -1, y = -1, label = "Soil Origin: p > 0.15")
m <-m + annotate("text", x = -1, y = -1.15, label = "Infestation Stage: p < 0.001")
m <-m + annotate("text", x = -1, y = -1.3, label = "K=3, Stress=0.179")
m <- m + ggtitle("NMDS of Infestation Stage and Soil Origin", ) +
  theme(plot.title = element_text(hjust = 0.5, size = 22)) + labs(shape="Soil Origin", colour="Infestation Stage")
m<- m + labs(x= "NMDS1", y="NMDS2")

image

commented

This is not really a ggordiplots question, but .....
You could try something like this:

library(ggordiplots)
data("dune")
data("dune.env")
dune.hel <- decostand(dune, method = "hellinger")
ord <- rda(dune.hel)
plt <- gg_ordiplot(ord, groups = dune.env$Management, label = TRUE, ellipse = FALSE, kind = "se", conf = 0.95)

df_ellipse <- plt$df_ellipse

BF_ellipse <- subset(df_ellipse, Group == "BF", droplevels= TRUE)
HF_ellipse <- subset(df_ellipse, Group == "HF", droplevels= TRUE)
NM_ellipse <- subset(df_ellipse, Group == "NM", droplevels= TRUE)
SF_ellipse <- subset(df_ellipse, Group == "SF", droplevels= TRUE)

plt$plot +
  geom_path(data=BF_ellipse, aes(x=x, y=y, linetype="solid"), color="Red") +
  geom_path(data=HF_ellipse, aes(x=x, y=y, linetype="solid"), color="Green") +
  geom_path(data=NM_ellipse, aes(x=x, y=y, linetype="dotted"), color="Gray", size=1.25) +
  geom_path(data=SF_ellipse, aes(x=x, y=y, linetype="dotted"), color="Gray" , size=1.25) +
  scale_linetype_manual(values = c("dotted" = 3, "solid" = 1), labels = c("Dead", "Live"), name="Condition")

image

I chose a dotted line because a dashed line does not show up well in the legend. I don't think you really need to have line color in the legend. In your case color maps to infestation stage. The ellipses are of the same color, and the ellipse line type will distiguish soil origins.

Thanks for the help.

commented