简体   繁体   中英

How to adjust both the line and point shape size in a legend to be different sizes?

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

图例中与数据点符号相交的线的图像 - 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))) 

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")
  

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM