rg中的ggplot:自动设置线型

时间:2012-01-16 17:46:30

标签: r ggplot2

我正在尝试在ggplot调用中自动设置linetype,我会这样做 在for循环中运行。数据在var2变量中有大约20个值,我循环使用 Eurostoxx(指数)和另一个一次绘制的值。

这是第一行:

       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"))

但我尝试自动化

secnam<-unique(g$var2)[!unique(g$var2)%in%"Eurostoxx"]
secnam

#"Automobiles and Parts"

gp<- gp +  scale_linetype_manual("",values=c("Eurostoxx"="dotted",secnam="solid"))

遇到错误

data.frame出错(缩放$ output_breaks(),I(缩放$ 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

1 个答案:

答案 0 :(得分:1)

可能有更优雅的方式来完成你想要完成的任务,但为了解决你的具体问题,让我们来看看究竟是什么c("Eurostoxx"="dotted",secnam="solid")

> c("Eurostoxx"="dotted",secnam="solid")
Eurostoxx    secnam 
 "dotted"   "solid"

请注意,您尝试使用secnam中的将元素命名为“solid”,但R不起作用。 (至少,不是没有深入研究使用evalparsesubstitute等,但这比我们需要的更复杂。)

相反,只需创建线型矢量,然后使用names函数直接修改名称:

> l <- c("dotted","solid")
> names(l) <- c("Eurostoxx",unique(g$var2)[!unique(g$var2)%in%"Eurostoxx"])
> l
Eurostoxx Utilities 
 "dotted"   "solid"

然后在values参数中将向量l传递给scale_linetype_manual