Overplotting with different colour palettes in ggp

2019-08-16 00:43发布

问题:

Dear stack overflow readers,

I am having a dataset for which I have "raw" data and for which I additionally compute some kind of smoothing function for. I then want to plot the raw data as points, and the smoothing as a line. This works with the following:

Loc <- seq(from=1,to=50)
Loc <- append(Loc,Loc)
x <- seq(from=0,to=45,by=0.01)
Val <- sample(x,100)
labels <- NULL
labels[1:50] <- 'one'
labels[51:100] <- 'two'

x2 <- seq(from=12,to=32,by=0.01)
Val2 <- sample(x2,100)
raw <- data.frame(loc=Loc,value=Val,lab=labels)
smooth <- data.frame(loc=Loc,value=Val2,lab=labels)

pl <- ggplot(raw,aes(loc,value,colour=lab)) + geom_point() + geom_line(data=smooth)
print(pl)

The result looks like this:

The problem is that my actual data contains so many data points that using the same colour palette is going to be very confusing (the difference between point and line will be almost indistinguishable). Preferably, I would make the geom_lines() slightly darker. I've tried with scale_colour_hue(l=40), but that results in the the geom_points() to be darker as well.

Thanks for any suggestions!

回答1:

I don't have a perfect answer for you, but I do have one that works. You can individually control colors of your lines by splitting them into separate geometries, like so:

ggplot(raw,aes(loc,value)) + 
geom_point(aes(col=lab)) + 
geom_line(data=smooth[smooth$lab=="one",], colour="blue", size=1) + 
geom_line(data=smooth[smooth$lab=="two",], colour="black", size=1)

The disadvantage of this is that you have to manually specify the row selection, but the advantage is that you can manually specify the visual properties of each line.

The other option is to use the same palette but to make the lines bigger and partially transparent, like so:

ggplot(raw,aes(loc,value,colour=lab)) + 
geom_point() + 
geom_line(data=smooth, size=2, alpha=0.5)

You should be able to customize things from here to suit your needs.



回答2:

Instead of making lines darker, you can try to make points lighter by setting alpha= value in geom_point().

pl <- ggplot(raw,aes(loc,value,colour=lab)) + geom_point(alpha=0.5) + 
      geom_line(data=smooth)
print(pl)



标签: r ggplot2