Line chart with gray and black dotted lines.

With the ggplot2 R package, I made a line graph with 6 color lines (refers to 6 factor levels), and I would like to change it in black and white by making 3 BLACK solid, dashed and dashed lines plus 3 GRAY solid, dashed and dashed lines . I am trying to use scale_linetype_manual () and scale_color_grey (), but I cannot mix gray and black dotted lines.

Here's the gray code:

ggplot() + 
  geom_line(data = f[!is.na(f$fr),], aes(x=date2, y=fr, colour=locality, group=locality), 
            size = 1.0) + 
  scale_color_grey(start = 0, end = 0.9, guide="legend", name="Locality", 
                   labels=c("a","b","c","d","e","f")) + 
  xlab("") + 
  ylab("") + 
  theme_bw() +
  theme(legend.position = "top", panel.background = element_rect(fill = "white"), 
        panel.grid.major = element_line(colour = "white"), 
        axis.text.x=element_text(angle=90,hjust=1,vjust=0.5)) + 
  facet_grid(.~year)    

enter image description here

whereas this is a code with dashed lines:

ggplot() + 
  geom_line(data = f[!is.na(f$fr),], aes(x=date2, y=fr, linetype=locality, group=locality), 
            size = 1.0) + 
  scale_linetype_manual(name="Locality", 
                        values=c("solid", "dashed", "dotted", "dotdash", "longdash", "twodash"), 
                        labels=c("a","b","c","d","e","f")) +
  xlab("") + 
  ylab("") + 
  theme_bw()+ 
  theme(legend.position = "top", panel.background = element_rect(fill = "white"), 
        panel.grid.major = element_line(colour = "white"), 
        axis.text.x=element_text(angle=90,hjust=1,vjust=0.5)) +
  facet_grid(.~year)    

enter image description here

therefore .. Someone can help me create the same storyline with one solid black line, one dashed black line, one dashed black line, one solid gray line, etc.

+4
2

colour, linetype, .

d <- data.frame(locality = rep(letters[1:6], each = 2),
            x = 1:2,
            y = rep(1:6, each = 2))

ggplot(d, aes(x = x, y = y,colour = locality, linetype = locality)) + 
  geom_line() +
  theme_bw() +
  scale_color_manual(name = "Locality",
                     values = c('black', 'black', 'black', 'grey', 'grey', 'grey'),
                     labels = c("a","b","c","d","e","f")) +
  scale_linetype_manual(name = "Locality", 
                        values = c("solid", "dashed", "dotted", "solid", "dashed", "dotted"), 
                        labels = c("a","b","c","d","e","f"))

enter image description here

+5

MLavoie, , . , , , - :

vectCol=c("a"="black", "b"="black", "c"="black", "d"="grey","e"="grey", "f"="grey")
vectTyp=c("a"="solid", "b"="dashed", "c"="dotted", "d"="solid","e"="dashed", "f"="dotted")

" " ggplot, scale_manual .

ggplot(data = f[!is.na(f$fr),], aes(x=date2, y=fr, colour=locality,group=locality, linetype=locality)) + 
geom_line(size = 1.0) + 
scale_color_manual(values=vectCol, guide="legend", name="Locality", labels=c("a","b","c","d","e","f"))+ 
scale_linetype_manual(values=vectTyp, guide="legend", name="Locality", labels=c("a","b","c","d","e","f"))+
xlab("") + ylab("") + theme_bw() + 
theme(legend.position = "top", panel.background = element_rect(fill ="white"), panel.grid.major = element_line(colour = "white"), 
    axis.text.x=element_text(angle=90,hjust=1,vjust=0.5)) + facet_grid(.~year) 
+2

Source: https://habr.com/ru/post/1618116/


All Articles