1

I get 2 different legends one for shape and one for color. I've read: Combine legends for color and shape into a single legend already but I have no clue how to combine a color/shape identity together.

My Data: It calls Vergleich2
My code:

Vergleich2 <- data.frame(
list(
RH = c(4.4, 70.81, 89.74, 98.21, 99.45, 100.3, 101.16, 101.83, 103.46, 103.65, 103.9, 33.37, 32.26, 50.39, 75.65, 81.54, 86.58, 91.88, 94.1, 96.41, 98.52, 99.93, 101.45, 77.09, 84.51, 92.15, 94.61, 96.22, 97.36, 98.85, 98.95, 98.74, 99.34, 100.07, 101.06, 102.45, 103.04), 
max = c(0, 0.0262005491707849, 
0.0960002914076637, 0.26123554979527, 0.299421329851762, 0.362190635901956, 0.452267730725373, 0.60803295055093, 0.958096790371026, 0.995440372287362, 1, 0.0191985206504361, 0, 0.0444427600652313, 0.0676200802520531, 0.0922989569990268, 0.112052964622176, 0.182215180712429, 0.248659241123121, 0.327097853193048, 0.496708233033155, 0.627302705113058, 1, 0.515522981617377, 0.585402158506993, 0.762678213109326, 0.920738889764711, 0.836214001953324, 0.871654063266438, 0.908503395902539, 0.825786584233689, 0.875522664077668, 0.831158954459146, 0.831533205795933, 0.893700430247523, 1.00803637031109, 
1), letzte = c(0, 0.0171373807524096, 0.0818334694345387, 0.239981280241844, 0.280068579568638, 0.345939316999413, 0.434432925347285, 0.611502937955804, 0.964279264750348, 1.00834862405373, 1, 0.00678220086610785, 0, -0.00307024455552525, 0.0255053593718935, 0.0748980985479396, 0.0890155980480638, 0.153017148428967, 0.187262260262659, 0.306449913424004, 
0.454599256084893, 0.614943073105356, 1, 0.527873986434174, 0.593334258062775, 0.768834444991388, 1.21440714508987, 0.847592976104216, 0.892496700707447, 0.917439391188656, 0.834935302471757, 0.840806889095709, 0.823590477107656, 
0.834511976098586, 0.912778381850167, 1.00642363306524, 1)))


Region1_plot <- ggplot()+
  geom_point(data=Vergleich2[c(1:11),], mapping=aes(x=RH,y=max,col="red",shape=19))+
  geom_point(data=Vergleich2[c(1:11),], mapping=aes(x=RH,y=letzte, col="blue",shape=19))+
  
  geom_point(data=Vergleich2[c(12:13),], mapping=aes(x=RH,y=max,col="red",shape=3))+
  geom_point(data=Vergleich2[c(12:13),], mapping=aes(x=RH,y=letzte, col="blue",shape=3))+
  
  geom_point(data=Vergleich2[c(14:23),], mapping=aes(x=RH,y=max,col="red",shape=6))+
  geom_point(data=Vergleich2[c(14:23),], mapping=aes(x=RH,y=letzte, col="blue",shape=6))+
  scale_color_identity("", guide="legend", breaks=c("red","blue"),labels=c("Incr.1","Decr.1"))+
  scale_shape_identity("", guide = "legend", breaks=c(19,3,6), labels=c("Incr.1","Decr.1", "Incr.2"))+
  labs(title = "Relative Wasseraufnahme Isopren SOA #10 (RH)", title.position="center")+
  ylab("Norm. Optical Density [-]")+
  xlab("RH [%]")+
  coord_cartesian(xlim = c(0, 100))+
  scale_x_continuous(breaks=c(seq(0,120, 4)))+
  theme(axis.text = element_text(size = 15),
        axis.title = element_text(size=15),
        plot.title = element_text(hjust = 0.5),
        legend.title = element_text(size=0),
        legend.text= element_text(size=15),
        legend.background = element_rect(),
        legend.position = c(0.095,0.9),
        title = element_text(size=20))
print(Region1_plot)

Thx for your help!!

Cosis94
  • 63
  • 7
  • 1
    Please wrap your Vergleich2 into dput() to give others something that can be put in R immediately. Example dput(iris[1:4, ]) # first four rows of the iris data set. see here: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610 – pbraeutigm Jun 29 '21 at 11:49
  • I did. I hope it works! Thank you! – Cosis94 Jun 29 '21 at 12:01
  • 3
    This can't work and not because of a programming issue. If you want to combine legends, the points must have identical labels for color and shape. That's not the case in your plot. – Roland Jun 29 '21 at 13:06

1 Answers1

0

Per my experience it makes to shape data so that you can keep ggplot calls as simple as possible. The various geom_points hint at a problem with your input data. Here's a proposal how to add a column that contains a combination of the attributes you want to show:

library(tidyverse)

Vergleich2 <- data.frame(
list(
RH = c(4.4, 70.81, 89.74, 98.21, 99.45, 100.3, 101.16, 101.83, 103.46, 103.65, 103.9, 33.37, 32.26, 50.39, 75.65, 81.54, 86.58, 91.88, 94.1, 96.41, 98.52, 99.93, 101.45, 77.09, 84.51, 92.15, 94.61, 96.22, 97.36, 98.85, 98.95, 98.74, 99.34, 100.07, 101.06, 102.45, 103.04), 
max = c(0, 0.0262005491707849, 
0.0960002914076637, 0.26123554979527, 0.299421329851762, 0.362190635901956, 0.452267730725373, 0.60803295055093, 0.958096790371026, 0.995440372287362, 1, 0.0191985206504361, 0, 0.0444427600652313, 0.0676200802520531, 0.0922989569990268, 0.112052964622176, 0.182215180712429, 0.248659241123121, 0.327097853193048, 0.496708233033155, 0.627302705113058, 1, 0.515522981617377, 0.585402158506993, 0.762678213109326, 0.920738889764711, 0.836214001953324, 0.871654063266438, 0.908503395902539, 0.825786584233689, 0.875522664077668, 0.831158954459146, 0.831533205795933, 0.893700430247523, 1.00803637031109, 
1), letzte = c(0, 0.0171373807524096, 0.0818334694345387, 0.239981280241844, 0.280068579568638, 0.345939316999413, 0.434432925347285, 0.611502937955804, 0.964279264750348, 1.00834862405373, 1, 0.00678220086610785, 0, -0.00307024455552525, 0.0255053593718935, 0.0748980985479396, 0.0890155980480638, 0.153017148428967, 0.187262260262659, 0.306449913424004, 
0.454599256084893, 0.614943073105356, 1, 0.527873986434174, 0.593334258062775, 0.768834444991388, 1.21440714508987, 0.847592976104216, 0.892496700707447, 0.917439391188656, 0.834935302471757, 0.840806889095709, 0.823590477107656, 
0.834511976098586, 0.912778381850167, 1.00642363306524, 1)))


plot_df <- Vergleich2[1:23,] ## above you plot a subset of the data - that's why I'm choosing columns 1:23

plot_df <- plot_df %>%
    mutate(shapes = c(rep("Incr.1", 11), rep("Decr.1", 2), rep("Incr.2", 10))) %>%  ## adding the attribute for shapes
    pivot_longer(cols = c("max", "letzte"), names_to = "colrs") %>%             ## tidying data (a format that is ggplot-friendly)
    mutate(combined = paste(shapes, colrs))                             ## and combining the columns so that I can use one column for both shape and colour

ggplot(plot_df, aes(x = RH, y = value, shape = combined, colour = combined))+
    geom_point() +
    scale_color_manual("", values = c("red", "blue", "red", "blue", "red", "blue"))+
    scale_shape_manual("", values = c(19, 3, 6, 19, 3, 6))+
    labs(title = "Relative Wasseraufnahme Isopren SOA #10 (RH)", title.position="center")+
    ylab("Norm. Optical Density [-]")+
    xlab("RH [%]")+
    coord_cartesian(xlim = c(0, 100))+
    scale_x_continuous(breaks=c(seq(0,120, 4)))+
    theme(axis.text = element_text(size = 15),
        axis.title = element_text(size=15),
        plot.title = element_text(hjust = 0.5),
        legend.title = element_text(size=0),
        legend.text= element_text(size=15),
        legend.background = element_rect(),
        legend.position = c(0.095,0.9),
        title = element_text(size=20))

See Hadley Wickham's book on "[tidy data][1]"

Wolfgang Arnold
  • 1,252
  • 8
  • 17
  • 1
    Hey thanks for your effort! I have some other data and plots which should be changed. Your suggestion was my last option but I have no other choice. Thank you for recommendation!!! – Cosis94 Jun 29 '21 at 14:33