指向我寫了下面的代碼:GGPLOT2 geom_line()應在規定值
library(ggplot2)
data <- structure(list(x = c(1L, 6L, 3L, 4L, 2L, 3L, 6L, 1L, 5L, 2L,
1L, 5L), y = c(1L, 7L, 5L, 6L, 3L, 4L, 6L, 2L, 5L, 6L, 5L, 2L
), year = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L), .Label = c("2010", "2011"), class = "factor"), matching = structure(c(1L,
2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L), .Label = c("person1",
"person2", "person3", "person4", "person5", "person6"), class = "factor")), .Names = c("x",
"y", "year", "matching"), row.names = c(NA, -12L), class = "data.frame")
data$year <- factor(data$year)
colors <- c("#4cb5ee", "#a0d099", "red")
p <- ggplot(data, aes(x=x, y=y)) +
geom_point(aes(colour=year), shape=16, size=6) +
geom_line(aes(group=matching), arrow=arrow(length=unit(0.15,"cm")), colour="black", size=1) +
xlab("x") + ylab("y") +
scale_colour_manual("year", values=colors) +
scale_x_continuous(limits=c(1,7), breaks=seq(1,7, by=1)) +
scale_y_continuous(limits=c(1,7), breaks=seq(1,7, by=1))
print(p)
它提供了以下的輸出:
但我想geom_line()做的是:總是指向年= 2011年的點。我無法弄清楚爲什麼這條線的箭頭有時候指向年份= 2010的點,有時也指向年份= 2011的點。
我發現了什麼是箭頭需要幾個參數:
arrow(angle = 30, length = unit(0.25, "inches"), ends = "last", type = "open")
所以,我可以說ends="first"
。但我不能一概而論ends
總是first
或總是last
。
我試圖添加一個列到我的data.frame,它有信息如果箭頭應該首先結束或最後結束,但它沒有給我我想要的輸出。
每個幫助都非常感謝:-)
在此先感謝!
這是一個更好的解決方案。我提出的方法可以爲更復雜的情況提供一些靈活性,但這更適合於手頭的任務。 – Chase 2011-04-29 13:09:31
打敗我吧。我想我需要滿足於我(非ggplot用戶)從聯機幫助中將它拼湊在一起。 – 2011-04-29 13:12:53
感謝您的解決方案,這非常簡單;-) – 2011-04-29 13:14:36