2012-09-08 63 views
6

Tengo un gráfico de líneas en ggplot2 y quiero agregar puntos (= formas) para cada fila de datos para identificarlo claramente. No necesito (!) Una forma/punto en cada punto de datos, pero en cambio, algunos valores serían suficientes. Véase el siguiente ejemplo:ggplot2: Agregar puntos a geom_line

library(ggplot2) 
library(data.table) 
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

Debido al gran número de muestras, las formas no son visibles pero más sobregirar entre sí. Solo necesito algunos de ellos, tal vez un espaciado equidistante se vea mejor, pero estoy abierto a cualquier otra solución.

+0

ver [esta cuestión] (http://stackoverflow.com/questions/6893959/r-how-do-i-draw-a-line -with-multiple-arrows-in-it/6904434 # 6904434) para dividir una línea en puntos equidistantes – baptiste

+0

por supuesto, la respuesta debería depender de si tiene una línea recta simple o una ruta con curvas – baptiste

Respuesta

8

También puede agregar algunos puntos, solo los datos delgada con un índice.

library(ggplot2) 
library(data.table) 
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

5

Puede trazar puntos en ciertos cuantiles con quantile. Por ejemplo, la siguiente secuencia genera deciles.

quantile(rnorm(100), probs = seq(0, 1, .1)) 
#   0%   10%   20%   30%   40%   50%   60%   70%   80%   90%  100% 
#-2.43934306 -1.17208001 -0.91497203 -0.69489868 -0.46306926 -0.24133438 -0.03434118 0.39989589 0.72331902 1.06402664 2.02892420 

library(ggplot2) 
library(data.table) 
d <- data.table(x = seq(0, 100, by=0.1), y = seq(0,1000)) 

ggplot(d, aes(x=x, y=y))+ 
geom_line()+ 
geom_point(aes(x = quantile(x, probs = seq(0, 1, .1)), 
       y = quantile(y, probs = seq(0, 1, .1)))) 

Plot with points at deciles

2

sólo quería añadir una solución data.table que puede trabajar con datos agrupados así:

library(ggplot2) 
library(data.table) 

# Creates data from the Weibull distribution 
weib_dt <- function(x = seq(0, 4.0, 0.01), w_shape = 1, w_scale = 1) { 
    y = dweibull(x, shape = w_shape, scale = w_scale) 
    data.table("shape" = as.factor(w_shape), "scale" = as.factor(w_scale), "x" = x, "y" = y) 
} 

dt_a <- weib_dt(w_shape = 0.5) 
dt_b <- weib_dt(w_shape = 1.0) 
dt_c <- weib_dt(w_shape = 2.0) 
# Bind multiple Weibull samples together, created from different parametrizations 
dt_merged <- rbindlist(list(dt_a, dt_b, dt_c)) 

# Create the plot, using all the points for the lines, and only 9 points per group for the points. 
ggplot(dt_merged, aes(x, y, group=shape, color=shape)) + 
    coord_cartesian(ylim = c(0, 1.5)) + 
    geom_line() + 
    geom_point(data=dt_merged[, .SD[floor(seq(1, .N, length=9))], by=shape], 
      aes(x, y, group = shape, color = shape, shape = shape)) 

El truco aquí es el uso de seq al igual que con las soluciones sugeridas anteriormente, pero esta vez se hace dentro del grupo (usando .SD). Tenga en cuenta que actualmente .SD puede tener un mal rendimiento, puede usar el más detallado dt[dt[, ..., by =shape]$V1] si esto es lento.

Esto creará el siguiente resultado:

Weibull plots

Cuestiones relacionadas