2017-07-19 1 views
0

Ich versuche, Linien zu einem vorhandenen Grundstück hinzuzufügen. Ignorieren Sie einfach die Daten, die nicht geplottet werden. geom_abline lässt mich Zeilen hinzufügen, aber sie gehen durch den gesamten Graphen. Wie kann ich die Linien zuschneiden, dass sie nur z. zwischen (0,0) und (10, -10)?ggplot - Linien zum Plotten hinzufügen - zwischen Punkten, nicht durch Punkte

library(ggplot2) 
    x <- c(2,4,6,4,7,5,3) 
y <- c(4,5,6,7,8,6,4) 
data <- data.frame(cbind(x,y)) 
ggplot(data, aes(x= x, y= y)) + 
    expand_limits(y = c(25, -60), x = c(20,-5)) + 
    geom_abline(intercept = 0, slope = -1) + 
    geom_abline(intercept = 0, slope = -.5, linetype="dotted") 

Antwort

2

Vielleicht so:

library(ggplot2) 
x <- c(2,4,6,4,7,5,3) 
y <- c(4,5,6,7,8,6,4) 
data <- data.frame(cbind(x,y)) 
ggplot(data, aes(x= x, y= y)) + 
    expand_limits(y = c(25, -60), x = c(20,-5)) + 
    geom_segment(aes(x = 0, xend = 10, y = 0, yend = 0 - 1*10)) + 
    geom_segment(aes(x = 0, xend = 10, y = 0, yend = 0 - 0.5*10), 
       linetype = 2) 
Verwandte Themen