简体   繁体   中英

Changing point size and colour in GGplot2 legend

I have a plot that looks like this绘图示例

I would like to modify the legend so that the points on the lines are the same colour as the points on the plot, but also larger, so that they are visible against the line, like this图例示例 .

As you can see, if the legend points are larger (size 5 here), they become visible, but don't match the plot point colours (maybe the red one does, I'm not sure, but the blue point on the legend is obviously different than the turquoise points on the plot). I was able to increase the legend point size only by changing the size of the plot points, which I definitely do not want.

I am not able to share the full dataset, but here is some fully-reproducible code using a dummy data set (as the points are so small and few, and so transparent, you cannot really see them, but the plot should load nevertheless):

Alla<-c(40,45,50,60,70,42,44,53,61,68)
Allb<-c(40,38,36,32,30,32,30,29,24,23)
All<-data.frame(Alla,Allb)
All<-All%>%rename(a=Alla,b=Allb)
Male<-All[1:5,]
Female<-All[6:10,]

library(ggplot2)
x<-ggplot()+
  geom_point(data=Male,aes(x=a,y=b),color="Turquoise",position=position_jitter(w=0.2,h=1.0),alpha=0.1,size=0.1,shape=16,show.legend=TRUE)+
  geom_point(data=Female,aes(x=a,y=b),color="#FF9999",position=position_jitter(w=0.2,h=1.0),alpha=0.1,size=0.1,shape=16,show.legend=TRUE)+
  theme_classic(base_size=15)+
  theme(legend.position="top",legend.direction="horizontal")+
  labs(x='A',y='B')
x<-x+geom_smooth(data=Male,aes(x=a,y=b,alpha="Men"),method="lm",formula=y~x+I(x^2),colour="Blue",linetype=1,se=F)+
  geom_smooth(data=Female,aes(x=a,y=b,alpha="Women"),method="lm",formula=y~x+I(x^2),colour="Dark Red",linetype=5,se=F)+
  geom_smooth(data=All,aes(x=a,y=b,alpha="All"),method="lm",formula=y~x+I(x^2),colour="Black",linetype=3,se=F)+
  scale_fill_discrete(name="Key",labels=c("Men","Women","All"))+
  scale_colour_discrete(name="Plot Colour",labels=c("Men","Women","All"))+
  scale_alpha_manual(name="Sex",
                     values=c(1,1,1),
                     breaks=c("Men","Women","All"),
                     guide=guide_legend(override.aes=list(linetype=c(1,5,3),name="Key",
                                                          shape=c(16,16,NA),
                                                          color=c("Blue","Dark Red","Black"),
                                                          fill=c("Turquoise","#FF9999",NA))))
### Print plot
x

The code is largely inherited from someone else, and I am still struggling a little with the aes() mapping approach of ggplot , which is presumably where the problem lies. Could someone please tell me which parts of the code need to be edited so that I can make the legend points size=c(5,5,NA) and colour=c("turquoise","#FF9999",NA) , while keeping the plot points small?

Thank you very much.

Colors of the points, lines and the legends are handled by ggplot 2 automatically. You can apply manual color mapping using scale_color_manual . Here is a more minimal example:

library(tidyverse)

data <- tibble(
  a = c(40, 45, 50, 60, 70, 42, 44, 53, 61, 68),
  b = c(40, 38, 36, 32, 30, 32, 30, 29, 24, 23),
  Sex = c(rep("Male", 6), rep("Female", 4))
)

sex_colors <- c("Female" = "darkred", "Male" = "Turquoise")

data %>%
  ggplot(aes(a, b, color = Sex, fill = Sex)) +
  geom_point() +
  scale_fill_manual(values = sex_colors) +
  scale_color_manual(values = sex_colors) +
  geom_smooth()

Created on 2022-03-02 by the reprex package (v2.0.0)

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