Home > Blockchain >  Change Legend Labels and Order in emmip plot
Change Legend Labels and Order in emmip plot

Time:11-28

I have a plot using emmip and I have been successful of modifying the labels of the panels and the x axis label but I have been unsuccessful changing the labels and the order of the legend, any suggestions?

It should be Phonemes by Cognate Status

and the label /k/ - Cognate, /k/ Noncognate, ....

This is the code for this plot.

enter image description here

###{r HS in CS interaction plot with pre switch syllable count vs phoneme }
(mylist <- list(
    Syl_Pre_Switch = seq(7,12,by=1),
    phoneTxtGrid=c("p","t","k"), 
    CogStatus=c("Cog", "Cag")))

emmip(modelHSCSwithDistance, phonemeTxtGrid*CogStatus ~ Syl_Pre_Switch,
      at=mylist, CIs = TRUE)    
    ggplot2::facet_grid(~factor(phonemeTxtGrid, 
                        levels=c('p', 't', 'k')))   
        xlab("Syllables After Cognate")   
        labs(fill = "Stops by Cognate Status")

I tried this suggestion from this post is there a way to change the Legend of graph in emmeans?

Adding

 scale_color_discrete()

but I have been unsuccessful in changing the label name.

CodePudding user response:

It may be possible to modify the plot, e.g., by adding more ggplot2 calls to it. But I think you are better off starting (almost) over. Start with:

plot.data <- emmip(<your emmip arguments>, plotit = FALSE)

This gives you a data frame with everything that goes into the plot (see ? emmip for details on naming). You can then rearrange this data frame, add columns, etc., then plot what you want using those data, using ggplot() or whatever else you may prefer.

CodePudding user response:

I was able to do it like this:

    (mylist222 <- list(Sylb_PostCog=seq(2,5,by = 1),phoneTxtGrid=c("p","t","k"), CogStatus=c("Cog", "Cag")))
emmip(modelHSvsL2CSwithDistancePostCogToSwitch, phonemeTxtGrid*CogStatus*SpeakGroup ~ Sylb_PostCog, at=mylist222, CIs = TRUE)    ggplot2::facet_grid(SpeakGroup~factor(phonemeTxtGrid, levels = c("p", "t", "k")))   labs(x = "Syllables Post Cognate", colour = "Stops by Cognate Status and Speaker Group")    scale_color_hue(labels = c("/k/ Cognate HS", "/p/ Cognate HS", "/t/ Cognate HS", "/k/ Noncognate HS", "/p/ Noncognate HS", "/t/ Noncognate HS", "/k/ Cognate L2", "/p/ Cognate L2", "/t/ Cognate L2", "/k/ Noncognate L2", "/p/ Noncognate L2", "/t/ Noncognate L2"))
  • Related