2012-09-08 21 views
6

Ho una linea-trama in ggplot2 e voglio aggiungere punti (= forme) per ogni riga di dati per identificare chiaramente. Non ho (!) Bisogno di una forma/punto ad ogni punto di dati, ma invece alcuni valori sarebbero sufficienti. Vedere l'esempio seguente:ggplot2: aggiungere punti al 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

A causa del gran numero di campioni, le forme non sono visibili ma più overdraw vicenda. Ho solo bisogno di alcuni di loro, forse una spaziatura equidistante sarebbe guardare il meglio, ma sono aperto a qualsiasi altra soluzione.

+0

vedi [questa domanda] (http://stackoverflow.com/questions/6893959/r-how-do-i-draw-a-line -con-più-arrows-in-it/6904434 # 6904434) per dividere una linea in punti equi-spaziate – baptiste

+0

naturalmente la risposta dovrebbe dipendere dal fatto di avere una semplice linea retta o un percorso sinuoso – baptiste

risposta

8

È inoltre possibile aggiungere alcuni punti, appena sottile i dati con un indice.

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

È possibile tracciare punti in certi quantili con quantile. Ad esempio, la seguente sequenza genera decili.

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

Volevo solo aggiungere una soluzione data.table che può lavorare con dati raggruppati così:

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

Il trucco è l'uso di seq come con le soluzioni proposte di cui sopra, ma questa volta è fatto all'interno del gruppo (usando .SD). Si noti che attualmente .SD può avere cattive prestazioni, è possibile utilizzare il più prolisso dt[dt[, ..., by =shape]$V1] se questo è lento.

questo modo si crea il seguente output:

Weibull plots

Problemi correlati