0

I am building a graph with ggplot2, but I wan a specific set of data to be black. However, by default R gives them another colour, and when I force it to be black, then it disappear from the legend:

Plot with colours chosen by R

ggplot(binodalbetla, aes(x=w.sal,y=w.tpc,colour=Category), show.legend=FALSE)+
  geom_line(data=subset(binodalbetla,Category == "TL 1"),linewidth=1.7)+
  geom_line(data=subset(binodalbetla,Category == "TL 2"),linewidth=1.7)+
  geom_line(data=subset(binodalbetla,Category == "TL 3"),linewidth=1.7)+
  geom_line(data=subset(binodalbetla,Category == "Model"),linewidth=1.7)+
  geom_point(data=subset(binodalbetla,Category == "Exp"),size=3.2)+
  labs(
    x = expression(w[Salt]),
    y = expression(w[TPC]),
    title = bquote(Bet~":"~2~LA~"/"~K[3]~PO[4])) +
  scale_color_discrete(name="") + 
  theme(panel.background = element_rect(fill = "white", colour = "grey50"), axis.text=element_text(size=34), title=element_text(size=38,face="bold"), legend.title=element_text(size=24, face="bold"),legend.text=element_text(size=34))

Plot with "Exp" data in black, but not shown in the legend

ggplot(binodalbetla, aes(x=w.sal,y=w.tpc,colour=Category), show.legend=FALSE)+
  geom_line(data=subset(binodalbetla,Category == "TL 1"),linewidth=1.7)+
  geom_line(data=subset(binodalbetla,Category == "TL 2"),linewidth=1.7)+
  geom_line(data=subset(binodalbetla,Category == "TL 3"),linewidth=1.7)+
  geom_line(data=subset(binodalbetla,Category == "Model"),linewidth=1.7)+
  geom_point(data=subset(binodalbetla,Category == "Exp"),size=3.2, colour="black")+
  labs(
    x = expression(w[Salt]),
    y = expression(w[TPC]),
    title = bquote(Bet~":"~2~LA~"/"~K[3]~PO[4])) +
  scale_color_discrete(name="") + 
  theme(panel.background = element_rect(fill = "white", colour = "grey50"), axis.text=element_text(size=34), title=element_text(size=38,face="bold"), legend.title=element_text(size=24, face="bold"),legend.text=element_text(size=34))
stefan
  • 90,330
  • 6
  • 25
  • 51
  • To help us to help you would you mind sharing [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data shared via `dput()`. – stefan Mar 09 '23 at 10:27

0 Answers0