2

I'm trying to adjust the point and line size independently in my legend. I'm wanting to be able to discern between the dashed and solid line while also having my point shapes be distinctive enough overtop of the line in the legend. Right now, I can't seem to figure out how to make the line smaller - I've figured out how to adjust the point size though. Any help/thoughts are all appreciated; definitely still a newbie. Thanks!

I'll post an image and code below:

Image of figure with points enhanced, but still can't get line to size correctly in the legend

Image of line intersecting with data point symbol in legend- Chase

ggplot(df, aes(x = Psych.Distress.Sum, y = Likelihood.Max, color = Feedback)) +
  geom_smooth(method = "lm", se = T, aes(linetype = Feedback)) +
  geom_jitter(aes(shape = Feedback)) +
  labs(x = "Psychological Distress", y = "Endorsement of Max's Feedback Strategy") +
  apatheme +
  theme(axis.title = element_text(face="bold")) +
  theme(legend.text = element_text(face="bold")) +
  theme(legend.position = c(0.87, 0.13)) +
  scale_color_grey(end = .5) +
  theme(legend.key.height= unit(.5, 'cm'),
        legend.key.width= unit(1, 'cm')) +
  guides(colour = guide_legend(override.aes = list(size= 3, linetype=0))) 

1 Answers1

0

This is tricky. Here's a hacky way using two plots that are overlaid on top of each other using patchwork. To prove that the data are aligned, I made the 2nd plot's text be semi-transparent red, but we could make it totally transparent with color #FF000000. This method is a little brittle, since the plots will come out of alignment if they have different ranges or different formats. But if we adjust for that, they line up perfectly with no extra fuss.

Your question didn't include any sample data so I used the mtcars data set.

library(patchwork)
library(ggplot2)


# This layer has the `geom_smooth` and black axis text
 (a <- ggplot(mtcars, aes(x = wt, y = mpg, color = as.factor(am))) +
  geom_smooth(method = "lm", se = T, aes(linetype = as.factor(am))) +
  scale_color_grey(end = .5) +
  guides(linetype = guide_legend(override.aes = list(size = 1.2))) +
  labs(x = "Psychological Distress", 
       y = "Endorsement of Max's Feedback Strategy",
       linetype = "Line legend", color = "Line legend") +
    coord_cartesian(ylim = c(10, 35)) +
  theme_classic() +
  theme(axis.title = element_text(face="bold")) +
  theme(legend.text = element_text(face="bold")) +
  theme(legend.position = c(0.7, 0.8)))
  
# This layer has the `geom_jitter` and red semi-clear axis text
(b <- ggplot(mtcars, aes(x = wt, y = mpg, color = as.factor(am))) +
  geom_jitter(aes(shape =  as.factor(am))) +
  scale_color_grey(end = .5) +
  guides(shape = guide_legend(override.aes = list(size = 3))) +
    coord_cartesian(ylim = c(10, 35)) +
    labs(x = "", y = "",
         color = "Point legend", shape = "Point legend") +
  theme_classic() +
  theme(plot.background = element_blank(),
        panel.background = element_blank(),
        axis.text = element_text(color = "#FF000055")) +
  theme(legend.position = c(0.7, 0.55)))

a + inset_element(b, 0, 0, 1, 1, align_to = "full")
  

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53
  • Thanks so much for this! I'm still trying to find a way to keep it all in one legend - the lines would intersect the points. By default my original data does that, but the points are too small to be able to discern what they represent. I'll try to add an additional image to illustrate what I'm talking about. Still figuring out how to supply actual data in these questions. – RforDummies Jun 20 '22 at 23:59
  • 1
    Ohhhhh - I misunderstood that you wanted it in one legend. I'll give that a shot. – Jon Spring Jun 21 '22 at 05:17