theomega theomega - 1 year ago 168
R Question

ggplot2: Add points to geom_line

I have a line-plot in ggplot2 and I want to add points (=shapes) for each data row to clearly identify it. I do not(!) need a shape/point at every data-point but instead some values would be sufficient. See the following example:

d=data.table(x=seq(0, 100, by=0.1), y=seq(0,1000)))
ggplot(d, aes(x=x, y=y))+geom_line()
ggplot(d, aes(x=x, y=y))+geom_line()+geom_point()

Line Only
With added points

Due to the huge number of samples, the shapes are not visible anymore but overdraw each other. I only need some of them, perhaps a equidistant spacing would look the best, but I'm open to any other solution.

Answer Source

You can also add some points, just thin the data with an index.

d=data.table(x=seq(0, 100, by=0.1), y=seq(0,1000))
ggplot(d, aes(x=x, y=y))+geom_line()
#Change the length parameter for fewer or more points
thinned <- floor(seq(from=1,to=dim(d)[1],length=70))
ggplot(d, aes(x=x, y=y))+geom_line()+geom_point(data=d[thinned,],aes(x=x,y=y))

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download