Question

I want to plot a path and show where the datapoints are.

Combine Points with lines with ggplot2 uses geom_point() + geom_line() but I do not like that the dots are much thicker and the lines have a discontinuous look - x - x ----- x --- thus I decidet to

create my own dotted line:

mya <- data.frame(a=1:20)

ggplot() +
  geom_path(data=mya, aes(x=a, y=a, colour=2, size=1)) +
  geom_point(data=mya, aes(x=a, y=a, colour=1, size=1)) +
  theme_bw() +
  theme(text=element_text(size=11))

example of a nice line

I like that the dots and the line have the same size. I did not use the alpha channel because I fear trouble with the alpha channel when I include the files in other programs.

open problems:

  • R should not create those legends
  • can R calculate the "darker colour" itself? darker(FF0000) = AA0000
  • how can I manipulate the linethickness? The size= parameter did not work as expected in R 2.15
Was it helpful?

Solution

Aesthetics can be set or mapped within a ggplot call.

  • An aesthetic defined within aes(...) is mapped from the data, and a legend created.
  • An aesthetic may also be set to a single value, by defining it outside aes().

In your case it appears you want to set the size to a single value. You can also use scale_..._manual(values = ..., guide = 'none') to suppress the creation of a legend. This appears to be what you want with colour.

You can then use named colours such as lightblue and darkblue (see ?colors for more details)

ggplot() +
 geom_line(data=mya, aes(x=a, y=a, colour='light'), size = 2) +
 geom_point(data=mya, aes(x=a, y=a, colour='dark'), size = 2) + 
 scale_colour_manual(values = setNames(c('darkblue','lightblue'), 
                                       c('dark','light')), guide = 'none') +
 theme_bw()

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top