How to specify different palettes and line size within one ggplot2 plot

I am showing four distributions inside the same graph ggplot2

with the following code (the data is downloaded there: https://www.dropbox.com/s/l5j7ckmm5s9lo8j/1.csv?dl=0 ):

require(reshape2)
library(ggplot2)
library(RColorBrewer)

fileName = "./1.csv" # downloadable there: https://www.dropbox.com/s/l5j7ckmm5s9lo8j/1.csv?dl=0

mydata = read.csv(fileName,sep=",", header=TRUE)

dataM = melt(mydata,c("bins"))

ggplot(data=dataM, aes(x=bins, y=value, colour=variable)) +
xlab("bins") + ylab("freq") + geom_line(size = .5, alpha = .9) +
scale_colour_brewer(type = "qual", palette = 7) +
geom_line(size = .5, alpha = .9) +
theme_bw() +
theme(plot.background = element_blank()
,panel.grid.minor = element_blank()
,axis.line = element_blank()
,legend.key = element_blank()
,legend.title = element_blank()) +
scale_y_continuous(expand=c(0,0)) + 
scale_x_continuous(expand=c(0,0))

      

enter image description here

How do I change this graph so that B, E and W are displayed according to a specific palette (say:) scale_colour_brewer

with a width of .5 and R appears in scale_colour_brewer(type = "qual", palette = 7)

with a width of 1?

+3


source to share


2 answers


You can call subsets of your data separately geom_line

:

ggplot() +
  geom_line(data=dataM[dataM$variable!="R",], aes(x=bins, y=value, colour=variable), size = .5, alpha = .9) +
  geom_line(data=dataM[dataM$variable=="R",], aes(x=bins, y=value, colour=variable), size = 1.5, alpha = .9) +
  scale_colour_brewer(type = "qual", palette = 7) +
  theme_bw() +
  theme(plot.background = element_blank(), panel.grid.minor = element_blank(), axis.line = element_blank(),
        legend.key = element_blank(), legend.title = element_blank()) +
  scale_y_continuous("freq", expand=c(0,0)) + 
  scale_x_continuous("bins", expand=c(0,0))

      

this gives: enter image description here




Another solution (as suggested by @baptiste) sets the scales size

and colour

manually:

ggplot(data=dataM, aes(x=bins, y=value, colour=variable, size = variable)) +
  geom_line(alpha = .9) +
  scale_colour_manual(breaks=c("B","E","W","R"), values=c("green","orange","blue","pink")) +
  scale_size_manual(breaks=c("B","E","W","R"), values=c(0.5,0.5,0.5,1.5)) +
  theme_bw() +
  theme(plot.background = element_blank(), panel.grid.minor = element_blank(), axis.line = element_blank(),
        legend.key = element_blank(), legend.title = element_blank()) +
  scale_y_continuous("freq", expand=c(0,0)) + 
  scale_x_continuous("bins", expand=c(0,0))

      

this gives more or less the same result: enter image description here

+3


source


ggplot(aes(x=bins, y=value, colour=variable)) +
  geom_line(data=dataM[dataM$variable!="R",]) +
  geom_line(data=dataM[dataM$variable=="R",])

      



0


source







All Articles