2014-04-13 5 views
1

줄거리를 몇 줄 추가하고 옆에 텍스트가 있습니다.ggplot에서 여러 줄 옆에 텍스트를 추가하는 방법은 무엇입니까?

Price <- seq(1, 20, by=1) 
MW <- seq(1, 200, by=10) 
fuel <- rep(c("Coal", "Gas", "Hydro", "Other"), each=5) 
Day <- rep(1, each=20) 
df1 <- data.frame(Price, MW,fuel, Day) 

Price<-seq(0, 19, by=1) 
MW <- seq(1, 100, by=5) 
Day <- rep(2, each=20) 
df2 <- data.frame(Price, MW, fuel, Day) 

df <- rbind(df1, df2) 
df <- df[with(df, order(Day, Price, fuel)), ] 

library(ggplot2) 



Lines<-data.frame(Level=c("mean", "Median"), vals=c(50, 100), lables=c("mean: 50", "Median: 100")) 
Lines$valy<-10 

ggplot(df, aes(x=MW, y=Price, group=Day))+ 
geom_point(aes(colour=fuel, shape=as.factor(Day)))+ 
geom_line(aes(colour=fuel))+ 
geom_vline(data=Lines, mapping=aes(xintercept=vals), color="blue") + 
geom_text(data = Lines, 
     aes(x=vals, y=valy, label=lables, colour="blue"), 
     hjust = 1) 

내가 오류를 받고 있어요 왜 어떤 생각 :

Error: Aesthetics must either be length one, or the same length as the dataProblems:vals, valy, lables 

가 나는 또한 오류 설정 내 주요 데이터에서이 작업을 수행하지만 얻기 위해 노력했다 :

object 'indexMO' not found 

내 코드가 있습니다 :

ggplot(tSub, aes(x=cumsum, y=Price, group=indexMO))+ 
geom_line(aes(colour=FuelType))+ 
geom_point(aes(colour=FuelType))+ 
scale_y_continuous("Price",breaks= seq(0,1000,by=50),labels = comma_format())+ 
scale_x_continuous("Dispatchable Energy (MW)",breaks= seq(0,12000,by=500),labels = comma_format())+ 
ggtitle(expression(atop("Minimum, Median, and Maximum Merit Orders for 2013", atop(italic("Based on Total Dispatchable MW"), "")))) + 
theme(axis.text.x = element_text(angle=-45, hjust=0, vjust=1), 
#plot.margin = unit(c(1.5, 1, 1, 1), "cm"), 
plot.title = element_text(size = 16, face = "bold", colour = "black", vjust = -1))+ 
theme(axis.text.x = element_text(colour = 'black', face = 'bold'))+ 
theme(axis.text.y = element_text(colour = 'black', face = 'bold'))+ 
geom_vline(data=Dispatched, mapping=aes(xintercept=vals), color="blue") + 
geom_text(data = Dispatched, 
     aes(x=vals, y=yval, label=LineLables, colour="blue"), 
     hjust = 1) 

하지만이 d oes는 위의 코드와 거의 같습니다.

+0

'dput (tSub)'할 수 있습니까? – hrbrmstr

답변

1

어때요?

enter image description here

당신이 & 미적 매핑 모두 데이터 소스와 일치 &을 혼합하고 있기 때문에

gg <- ggplot() 
gg <- gg + geom_point(data=df, mapping=aes(x=MW, y=Price, group=Day, colour=fuel, shape=as.factor(Day))) 
gg <- gg + geom_line(data=df, mapping=aes(x=MW, y=Price, group=Day, colour=fuel)) 
gg <- gg + geom_vline(data=Lines, mapping=aes(xintercept=vals), color="blue") 
gg <- gg + geom_text(data=Lines, mapping=aes(x=vals, y=valy, label=as.character(lables), colour="blue"), hjust = 1) 
gg 

, 나는 각 geom에 명시 적으로 언급하는 것을 선호합니다. 이것은 대개 R이 처리하기 쉽게 만듭니다.

+0

나는 이것이 q의 1 부분에 대해서만 대답한다는 것을 알지만,'tSub'는 q에있는 어떤 데이터 덤프에도 없습니다. 아마 2 개의 질문을 만드시겠습니까? – hrbrmstr

1

왜 그런 오류가 발생하는지 잘 모르겠지만, 문제는 글로벌 미학으로 group을 지정하는 것입니다. 이동하면 geom_line() 콜에서만 필요한 모든 것이 잘 작동합니다.

ggplot(df, aes(x=MW, y=Price))+ 
    geom_point(aes(colour=fuel, shape=as.factor(Day)))+ 
    geom_line(aes(colour=fuel, group = Day))+ 
    geom_vline(data=Lines, mapping=aes(xintercept=vals), color="blue") + 
    geom_text(data = Lines, 
       aes(x=vals, y=valy, label=lables), 
       color = "blue", 
       hjust = 1) 

아마도 tsub의 문제 일 수 있습니다.

관련 문제