문제
사용 ggplot2
나는 보통 사용합니다 geom_text
그리고 같은 것 position=jitter
내 음모에 주석을 달다.
그러나 - 좋은 음모를 위해 나는 종종 수동으로 주석을 달 수있는 가치가 있습니다. 아래처럼 :
data2 <- structure(list(type = structure(c(5L, 1L, 2L, 4L, 3L, 5L, 1L,
2L, 4L, 3L, 5L, 1L, 2L, 4L, 3L, 5L, 1L, 2L, 4L, 3L), .Label = c("EDS",
"KIU", "LAK", "MVH", "NA*"), class = "factor"), value = c(0.9,
0.01, 0.01, 0.09, 0, 0.8, 0.05, 0, 0.15, 0, 0.41, 0.04, 0.03,
0.52, 0, 0.23, 0.11, 0.02, 0.64, 0.01), time = c(3L, 3L, 3L,
3L, 3L, 6L, 6L, 6L, 6L, 6L, 15L, 15L, 15L, 15L, 15L, 27L, 27L,
27L, 27L, 27L), year = c(2008L, 2008L, 2008L, 2008L, 2008L, 2007L,
2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L,
2006L, 2006L, 2006L, 2006L, 2006L)), .Names = c("type", "value",
"time", "year"), row.names = c(1L, 3L, 4L, 5L, 6L, 7L, 9L, 10L,
11L, 12L, 13L, 15L, 16L, 17L, 18L, 19L, 21L, 22L, 23L, 24L), class = "data.frame")
ggplot(data2, aes(x=time, y=value, group=type, col=type))+
geom_line()+
geom_point()+
theme_bw()+
annotate("text", x=6, y=0.9, label="this is a wrong color")+
annotate("text", x=15, y=0.6, label="this is a second annotation with a wrong color")
문제는 라인 색상과 일치하도록 텍스트 주석 색상을 얻을 수 없다는 것입니다. 수동 스케일로 이것을 고칠 수 있다고 생각하지만 더 나은 방법이 있기를 바랍니다.
해결책
Annotate () 대신 geom_text ()를 사용하는 경우 그룹 색상을 플롯에 전달할 수 있습니다.
ggplot(data2, aes(x=time, y=value, group=type, col=type))+
geom_line()+
geom_point()+
theme_bw() +
geom_text(aes(7, .9, label="correct color", color="NA*")) +
geom_text(aes(15, .6, label="another correct color!", color="MVH"))
따라서 Annotate ()를 사용하면 다음과 같습니다.Alt Text http://www.cerebralmastication.com/wp-content/uploads/2010/03/before.png
그런 다음 geom_text ()를 사용한 후 다음과 같습니다.Alt Text http://www.cerebralmastication.com/wp-content/uploads/2010/03/after.png
다른 팁
나는 비슷한 문제가 있었고 JD 긴 대답으로 해결했습니다. 그러나 결과로 ggplot2
버전 0.9.0으로 업데이트 geom_text()
전화는 음모에 다소 흐릿 해졌다.
감사합니다 코스케 나는이 코드를 발견했다
ggplot(data2, aes(x=time, y=value, group=type, col=type))+
geom_line()+
geom_point()+
theme_bw() +
geom_text(aes(7, .9, label="correct color", color="NA*")) +
geom_text(aes(15, .6, label="another correct color!", color="MVH"))
geom_text를 표시합니다 nrow(data2)
타임스!
GEOM_TEXT에 데이터를 공급하는 올바른 방법은 다른 데이터를 구축하는 것입니다. 프레임 보유 좌표, 레이블 및 색상을 플롯하려는 문자열의 색상 :
data2.labels <- data.frame(
time = c(7, 15),
value = c(.9, .6),
label = c("correct color", "another correct color!"),
type = c("NA*", "MVH")
)
ggplot(data2, aes(x=time, y=value, group=type, col=type))+
geom_line()+
geom_point()+
theme_bw() +
geom_text(data = data2.labels, aes(x = time, y = value, label = label))