0

I have successfully edited the x and y axis titles, but I'm stuck on the legend title and labels.

Here is the code I have run so far:

m.emo.plot <- interact_plot(model = m.emo, pred = cemodiff, modx = onlychildcg, data = data)
m.emo.plot

m.emo.plot + labs(x ="Emotional Difficulty of Care", y = "Psychological Well-being", legend.title = "Sibling Structure")

which gives me this plot:

enter image description here

I want to edit the legend so that onlychildcg says "Sibling Structure," the 0 is relabeled as "1+ siblings" and the 1 is relabeled as "Only child."

Here are a few things I have tried but haven't worked:

#m.emo.plot2 <- m.emo.plot + scale_fill_discrete(name = "New Legend Title")

#m.emo.plot$labels$colour <- "New legend title"

#m.emo.plot + labs(x ="Emotional Difficulty of Care", y = "Psychological Well-being", legend.title = "Sibling Structure", color='NEW LEGEND TITLE')

#m.emo.plot + labs(x ="Emotional Difficulty of Care", y = "Psychological Well-being", legend.title = "Sibling Structure", fill='NEW LEGEND TITLE')

#m.emo.plot + guides(fill=guide_legend(title="New Legend Title"))
#adds a new legend but doesn't remove current one and has two solid lines, not one solid and one dashed

#m.emo.plot + labs(x ="Emotional Difficulty of Care", y = "Psychological Well-being", legend.title = "Sibling Structure") + guide_legend(title="my awesome title")
#Can't add `guide_legend(title = "my awesome title")` to a <ggplot> object.

Any advice would be very appreciated!!

1
  • 2
    Where does the function interact_plot come from? That's not a ggplot2 function. It's easier to help you if you include a simple reproducible example with sample input that can be used to test and verify possible solutions.
    – MrFlick
    Commented May 28 at 17:56

1 Answer 1

0

Although it's a ggplot, and it's possible to modify the legend title using the scale_color_discrete() function, the solution is easier.

You're using the package {interactions}, and the included plotting function comes with two arguments that will help you.

interact_plot(legend.main= "", modx.labels = "")

This should work:

library(interactions)  
  fitiris <- lm(Petal.Length ~ Petal.Width * Species, data = iris)
  interact_plot(fitiris, pred = Petal.Width, modx = Species,
                legend.main = "CUSTOM LEGEND TITLE", # Edit this for a custom title
                modx.labels = c("CUSTOM LEVEL LABEL 1", # Edit this for the levels of your variable
                                "CUSTOM LEVEL LABEL 2",
                                "CUSTOM LEVEL LABEL 13")
                )

enter image description here

Hope it helps.

P.S. Next time, make sure to use a more detailed and reproducible example in your questions so it's easier for others to jump in and help you more efficiently. You could edit this question to include the package you need help with and add a sample data set.

Not the answer you're looking for? Browse other questions tagged or ask your own question.