ggplot이 있고 geom_text
플롯에 열 값을 붙여 넣어야 합니다. 실제로 내 플롯에 여러 열을 붙여 넣었습니다. 색상에 따라 컬럼을 구분할 수 있습니까?
df <- data.frame(YearMonth = c(200606,200606,200608,200701,200703,200605),
person1 = c('Alice','Bob','Alice','Alice','Bob','Alice'),
person2 = c('Bob','Alice','Bob','Bob','Alice','Bob'),
Event = c('event1','event2','event3','event3','event2','event4')
)
df$YM <- as.Date(paste0("01",df$YearMonth), format="%d%Y%m")
rangeYM <- range(df$YM)
ypts <- rep_len(c(-1,1), length.out=nrow(df))
아래는 플로팅에 사용한 코드입니다.
ggplot()+geom_blank(aes(x= rangeYM, y = c(-1,1))) + labs(x = "", y = "") +
theme(axis.ticks = element_blank()) +
geom_hline(yintercept = 0, col = 'maroon') +
geom_segment(aes(x = df$YM, y = 0, xend = df$YM, yend = ypts), arrow = arrow(length = unit(0.2,"cm"))) +
scale_x_date(date_labels = '%b-%y', date_breaks = "month", minor_breaks = NULL) +
scale_y_continuous(minor_breaks = NULL) +
geom_text(aes(x = df$YM, y = 0, label = paste(format(df$YM, "%b-%y")), vjust = 1.5), colour = "#5B7FA3", size = 3, fontface = "bold") +
geom_text(aes(x = df$YM, y = ypts, label = paste(df$person1,df$person2,df$Event,sep="\n")))
그것이 dataframe로부터 알 수있는 바와 같이 person1
, person2
, Event
열이며 동일 색으로 표현된다. 나는 포함하여 다른 색상을 지정 시도 colour
에서 aes
의 geom_text
다음과 같습니다.
geom_text(aes(x = df$YM, y = c(-1,1), label = paste(df$person1,df$person2,df$Event,sep="\n"), colour = factor(df$person1,df$person2,df$Event)))
그러나 이것은 나에게 오류를 준다. 요소에 하나의 열을 포함하면 즉 colour = factor(df$Person1)
, Person1 열의 값에 다른 색상을 할당합니다. 하지만 플롯에 여러 열을 붙여 넣을 때 열을 기준으로 색상을 구분하고 싶습니다.
그렇게 할 수있는 옵션이 있으면 도와주세요.
이 경우에는 별도의 기하학을 사용하는 것이 가장 쉬운 것 같습니다.
또한 data
전역 환경에서 개체를 사용하거나 .NET Framework에서 $
표기법 을 사용하는 대신 data.frames를 인수에 전달하는 것이 좋습니다 aes
. 내 경험상, 이것은 어느 시점에서 당신을 뒤에서 물고 나쁜 습관입니다.
ggplot()+geom_blank(aes(x= rangeYM, y = c(-1,1))) + labs(x = "", y = "") +
theme(axis.ticks = element_blank()) +
geom_hline(yintercept = 0, col = 'maroon') +
geom_segment(aes(x = df$YM, y = 0, xend = df$YM, yend = ypts), arrow = arrow(length = unit(0.2,"cm"))) +
scale_x_date(date_labels = '%b-%y', date_breaks = "month", minor_breaks = NULL) +
scale_y_continuous(minor_breaks = NULL) +
geom_text(aes(x = df$YM, y = 0, label = paste(format(df$YM, "%b-%y")), vjust = 1.5), colour = "#5B7FA3", size = 3, fontface = "bold") +
geom_text(aes(x = YM, y = ypts, label = person1), df, col = 'red', nudge_y = .1) +
geom_text(aes(x = YM, y = ypts, label = person2), df, col = 'darkgreen') +
geom_text(aes(x = YM, y = ypts, label = Event), df, col = 'blue', nudge_y = -.1)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다