lgndrzzz
lgndrzzz

Reputation: 108

Lines in ggplot order

From library mgcv i get the points to plot with:

fsb <- fs.boundary(r0=0.1, r=1.1, l=2173)

if with standard graphic package i plot fsb and then i add lines i get :

 x11()
 plot(fsb)
lines(fsb$x,fsb$y)

enter image description here

I try now with ggplot (this is the line within a bigger code) :

tpdf <- data.frame(ts=fsb$x,ps=fsb$y)
ts=fsb$x
ps=fsb$y
geom_line(data=tpdf, aes(ts,ps), inherit.aes = FALSE)

i get a messy plot: enter image description here

I think that i'm failing the order in geom_line

Upvotes: 2

Views: 99

Answers (2)

missuse
missuse

Reputation: 19756

This can be solved by using geom_path:

ggplot(tpdf)+
  geom_point(aes(ts,ps)) +
  geom_path(aes(ts,ps))

enter image description here

You have a very odd way of using ggplot I recommend you to reexamine it.

data:

library(mgcv)
fsb <- fs.boundary(r0 = 0.1, r=2, l=13)
tpdf <- data.frame(ts=fsb$x,ps=fsb$y)

Upvotes: 4

erocoar
erocoar

Reputation: 5923

You'll have to specify the group parameter - for example, this

ggplot(tpdf) + 
  geom_point(aes(ts, ps)) +
  geom_line(aes(ts, ps, group = gl(4, 40)))

gives me a plot similar to the one in base R. enter image description here

Upvotes: 2

Related Questions