Overplotting with different colour palettes in ggp

2019-08-16 00:22发布

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:

enter image description here

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!

标签: r ggplot2
2条回答
我只想做你的唯一
2楼-- · 2019-08-16 00:52

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)

enter image description here

查看更多
Rolldiameter
3楼-- · 2019-08-16 01:01

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)

chart 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)

chart 2

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

查看更多
登录 后发表回答