0

I have to use a rather complicated color-coding scheme to be able to differentiate several lines/points. For instance, I use open symbols where I can differentiate between fill and color. The following graph is plotted:

https://portal.idiv.de/nextcloud/index.php/s/LEJ9gnpN3fB6kAK

However, on top of this, I would like to plot one single line of what is the mean of all the lines. But as I already specified the aesthetics above with fill and color I cannot undo this, but this would be needed....

Further, is there a chance to have one legend combining shapes, lines and colors? The problem is, that for one line I cannot plot a regression line as there is just one x-value...

The code:

#dataset
data.red <- read_csv("https://www.idiv.de/fileadmin/content/Files_EIE/Teaching/data.red.csv")

#color coding etc
experiment <- factor(data.red1$experiment, levels= c("SATAKUNTA","BANGOR",  "BIOTREE", "FORBIO","IDENT_AUCLAIR", "IDENT_MN", "KREINITZ", "ORPHEE", "BEFChina","SABAH", "SARDINILLA"))
color.experiment <-c("#2b83ba","#78c588", "#3f6d49","#018571","#123f1b","#78c588","#3f6d49","#018571","#fdae61","#d7191c","#d7191c")  
fill.experiment <- c("#2b83ba", "#ffffff","#ffffff","#ffffff","#ffffff","#78c588","#3f6d49","#018571","#fdae61","#000000","#000000")                    
line.experiment <- c("solid", "solid", "dashed", "dotted", "dotdash", "longdash", "twodash", "dashed", "solid", "solid", "dashed")

#plot
ggplot(data.red, aes(x=log.div.level , y=bas, color=experiment, shape=experiment, fill=experiment)) + 
    labs(x="SR", y="value") +
  geom_point(stroke = 1.3)+
    geom_smooth(method=lm, se=F, na.rm=F, aes(linetype=experiment))+
     scale_shape_manual(name  = "Experiment", breaks=c("SATAKUNTA","BANGOR",  "BIOTREE", "FORBIO","IDENT_AUCLAIR", "IDENT_MN", "KREINITZ", "ORPHEE", "BEFChina","SABAH", "SARDINILLA"), values=c(15,24,24,24,24,25,25,25,16,18,5))+
  scale_colour_manual(name = "Experiment", breaks=c("SATAKUNTA","BANGOR",  "BIOTREE", "FORBIO","IDENT_AUCLAIR", "IDENT_MN", "KREINITZ", "ORPHEE", "BEFChina","SABAH", "SARDINILLA"), values=color.experiment)+
  scale_fill_manual(name = "Experiment", breaks=c("SATAKUNTA","BANGOR",  "BIOTREE", "FORBIO","IDENT_AUCLAIR", "IDENT_MN", "KREINITZ", "ORPHEE", "BEFChina","SABAH", "SARDINILLA"), values=fill.experiment)+
  scale_linetype_manual(name = "Experiment", breaks=c("SATAKUNTA","BANGOR",  "BIOTREE", "FORBIO","IDENT_AUCLAIR", "IDENT_MN", "KREINITZ", "ORPHEE", "BEFChina","SABAH", "SARDINILLA"), values=line.experiment)
  • Can you provide sample data? so we can recreate the plot – Santiago I. Hurtado Mar 05 '21 at 13:36
  • You should add a small portion of your dataset or some example dataset so people can actually reproduce what you want and try to fix it. Check it how to do it here: [link](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – rodrigarco Mar 05 '21 at 13:39

1 Answers1

0

You have to simple put the data and mapping argument inside each geom, like this:

ggplot() + 
  geom_point(data = data.red,
             mapping =  aes(x=log.div.level ,
                            y=bas, color=experiment,
                            shape=experiment, 
                            fill=experiment),
             stroke = 1.3)+
  geom_smooth(data = data.red,
              mapping =  aes(x=log.div.level , y=bas,
                             color=experiment, linetype=experiment),
              method=lm, se=F, na.rm=F)+
  scale_shape_manual(name  = "Experiment", breaks=c("SATAKUNTA","BANGOR",  "BIOTREE", "FORBIO","IDENT_AUCLAIR", "IDENT_MN", "KREINITZ", "ORPHEE", "BEFChina","SABAH", "SARDINILLA"), values=c(15,24,24,24,24,25,25,25,16,18,5))+
  scale_colour_manual(name = "Experiment", breaks=c("SATAKUNTA","BANGOR",  "BIOTREE", "FORBIO","IDENT_AUCLAIR", "IDENT_MN", "KREINITZ", "ORPHEE", "BEFChina","SABAH", "SARDINILLA"), values=color.experiment)+
  scale_fill_manual(name = "Experiment", breaks=c("SATAKUNTA","BANGOR",  "BIOTREE", "FORBIO","IDENT_AUCLAIR", "IDENT_MN", "KREINITZ", "ORPHEE", "BEFChina","SABAH", "SARDINILLA"), values=fill.experiment)+
  scale_linetype_manual(name = "Experiment", breaks=c("SATAKUNTA","BANGOR",  "BIOTREE", "FORBIO","IDENT_AUCLAIR", "IDENT_MN", "KREINITZ", "ORPHEE", "BEFChina","SABAH", "SARDINILLA"), values=line.experiment) +
  labs(x="SR", y="value") +
  geom_smooth(data = data.red, method=lm,
              mapping = aes(x=log.div.level ,
                            y=bas),
              se=F, na.rm=F, col = 'black' ) 

This code adds a black horizontal line with what you ask for.

Santiago I. Hurtado
  • 1,113
  • 1
  • 10
  • 23