2013-03-13 4 views
2

저는 geom_bar 플롯을 가지고 있으며, geom_hline의 길이를 설정하고 싶습니다.`geom_bar` 플롯에서`geom_hline`의 길이를 설정하십시오.

나는이 데이터를

,

set.seed(666) 
df <- data.frame(
    date = seq(Sys.Date(), len= 156, by="4 day")[sample(156, 26)], 
    IndoorOutdoor = rep(c(-1,1), 13), #Change so there are only 26 rows 
    Xmin = sample(90, 26, replace = T), 
    Ymin = sample(90, 26, replace = T), 
    Zmin = sample(90, 26, replace = T) 
) 

df$XYZmin <- rowSums(df[,c("Xmin", "Ymin", "Zmin")])*df$IndoorOutdoor 
df[,7:9] <- df[,3:5]*df[,2] #Adding the sign to each X/Y/Z 
names(df)[7:9] <- paste0(names(df)[3:5],"p") #to differentiate from your X/Y/Z 
require(ggplot2) 

df.m <- melt(df[,c(1:2,6:9)], measure.vars=c("Xminp", "Yminp", "Zminp")) 
df.m$pos <- c(as.Date("2013-04-15"), rep(Sys.Date(), (length(df.m$XYZmin)-1))) 
df.m$foo <- 0 

후 나는이 오류 메시지가 제공이

plot.alt <- ggplot(df.m, aes(date, value, fill=variable)) + 
    geom_bar(position="stack", stat="identity", width=0.5) + 
    geom_hline(aes(pos, foo), size = 0.8, colour = "green") 
plot.alt 

처럼

Warning message: 
Stacking not well defined when ymin != 0 

와 플롯,

그것을하려 시도

ddd 나는 as.numericpos와 함께 음모를 꾸미고하려고하면

df.m$pos <- as.numeric(df.m$pos) 

나는이 오류가 나는 녹색 라인의 길이를 설정하는 방법

Error: Invalid input: date_trans works with objects of class Date only 

?

+1

길이를 정밀하게 제어하려면 'geom_segment'를 사용하십시오. 그리고 경고는 오류가 아닙니다. 두 가지를 혼동하지 마십시오! – joran

+0

감사합니다. 좋은 지적입니다. 경고입니다. –

답변

4

geom_hline()geom_segment()으로 대체하고 x 값의 시작 및 끝 위치를 설정할 수 있습니다.

ggplot(df.m, aes(date, value, fill=variable)) + 
    geom_bar(position="stack", stat="identity", width=0.5) + 
    geom_segment(aes(x=min(pos),xend=max(pos),y=0,yend=0),colour="green") 
관련 문제