我想在ggplot調用中自動設置線型,我將在for循環中運行 。數據在var2變量中有大約20個值,我通過循環訪問Eurostoxx(索引)和另一個值來一次繪製圖表。ggplot in r:自動設置線型
這裏是第一行:
Date value var1 var2
2011-09-30 20.67 Return on Equity Eurostoxx
我想具有Eurostoxx如每一個曲線圖中的實線(它是混亂 讀者時它切換)。
我知道如何設置手動線型:
gp<- gp + scale_linetype_manual("",values=c("Eurostoxx"="dotted","Automobiles and Parts"="solid"))
,但我試圖在data.frame(規模$ output_breaks這
secnam<-unique(g$var2)[!unique(g$var2)%in%"Eurostoxx"]
secnam
#"Automobiles and Parts"
gp<- gp + scale_linetype_manual("",values=c("Eurostoxx"="dotted",secnam="solid"))
自動運行到錯誤
錯誤(),I(scale $ labels())): 行名包含缺失值
下面的代碼重現了這個問題。
gfull<-structure(list(Date = structure(c(15247, 15278, 15308, 15338,
15247, 15278, 15308, 15338, 15247, 15278, 15308, 15338, 15247,
15278, 15308, 15338, 15247, 15278, 15308, 15338, 15247, 15278,
15308, 15338), class = "Date"), value = c(20.67, 19.81, 19.81,
19.92, 1.2966, 1.4054, 1.3744, 1.3828, 16.36, 16.58, 16.86, 16.7,
0.8263, 0.9167, 0.8642, 0.8197, 13.32, 12.82, 12.59, 12.55, 1.1672,
1.1721, 1.1643, 1.1509), var1 = c("Return on Equity", "Return on Equity",
"Return on Equity", "Return on Equity", "Price to Book", "Price to Book",
"Price to Book", "Price to Book", "Return on Equity", "Return on Equity",
"Return on Equity", "Return on Equity", "Price to Book", "Price to Book",
"Price to Book", "Price to Book", "Return on Equity", "Return on Equity",
"Return on Equity", "Return on Equity", "Price to Book", "Price to Book",
"Price to Book", "Price to Book"), var2 = c("Eurostoxx", "Eurostoxx",
"Eurostoxx", "Eurostoxx", "Eurostoxx", "Eurostoxx", "Eurostoxx",
"Eurostoxx", "Automobiles and Parts", "Automobiles and Parts",
"Automobiles and Parts", "Automobiles and Parts", "Automobiles and Parts",
"Automobiles and Parts", "Automobiles and Parts", "Automobiles and Parts",
"Utilities", "Utilities", "Utilities", "Utilities", "Utilities",
"Utilities", "Utilities", "Utilities")), .Names = c("Date", "value",
"var1", "var2"), row.names = c(71L, 72L, 73L, 74L, 4511L, 4512L,
4513L, 4514L, 145L, 146L, 147L, 148L, 4585L, 4586L, 4587L, 4588L,
1477L, 1478L, 1479L, 1480L, 5917L, 5918L, 5919L, 5920L), class = "data.frame")
代碼:
g<-gfull
g<-subset(gfull, var2 %in% c("Eurostoxx","Automobiles and Parts"))
gp <- ggplot(g, aes(Date, value,group=var2,lty=var2))
gp <-gp + facet_grid(var1~., scales="free")
gp<- gp + geom_line()
#EUROSTOXX is dotted
g<-gfull
g<-subset(g,var2%in%c("Eurostoxx","Utilities"))
gp <- ggplot(g, aes(Date, value,group=var2,lty=var2))
gp <-gp + facet_grid(var1~., scales="free")
gp<- gp + geom_line()
#EUROSTOXX is solid
secnam<-unique(g$var2)[!unique(g$var2)%in%"Eurostoxx"]
gp<- gp + scale_linetype_manual("",values=c("Eurostoxx"="dotted",secnam="solid"))
gp
#Error in data.frame(scale$output_breaks(), I(scale$labels())) :
#row names contain missing values
你能使用代碼塊格式化你的代碼嗎? – 2012-01-16 18:07:27
您可以在包含您想要的線型值的'data.frame'中創建一個新變量,並使用'scale_linetype_identity'(未測試)。 – baptiste 2012-01-16 20:03:57
感謝您的評論傢伙。我將在下一次查找此代碼塊功能。巴蒂斯特,你的建議是對的,因爲喬蘭已經充實下面。 – Aidan 2012-01-17 08:47:33