簡體   English   中英

根據第三個變量更改線條樣式 - R.

[英]Change line style depending on third variable - R

在我的數據幀之后,使用dput():

structure(list(year = 1984:2007, ger.90.10.ratio = c(3.1, 3.09, 
2.98, 2.93, 2.98, 2.96, 2.95, 3.12, 3.16, 3.13, 3.24, 3.3, 3.28, 
3.24, 3.24, 3.17, 3.23, 3.31, 3.63, 3.69, 3.75, 3.91, 4.1, 3.99
), kanzler = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L), wahljahr = c(0L, 
0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 
0L, 1L, 0L, 0L, 1L, 0L, 0L), us.90.10.ratio = c(9.55, 9.69, 10.09, 
10.23, 10.21, 9.99, 10.12, 10.22, 10.34, 10.64, 10.57, 10.11, 
10.33, 10.6, 10.44, 10.42, 10.58, 10.63, 10.75, 11.22, 11.08, 
11.17, 11.08, 11.18), us.80.20.ratio = c(4.36, 4.38, 4.49, 4.48, 
4.45, 4.44, 4.42, 4.51, 4.6, 4.65, 4.68, 4.52, 4.61, 4.64, 4.65, 
4.62, 4.56, 4.65, 4.69, 4.83, 4.76, 4.78, 4.84, 4.93), ger.ratio.diff = c(NA, 
-0.0100000000000002, -0.11, -0.0499999999999998, 0.0499999999999998, 
-0.02, -0.00999999999999979, 0.17, 0.04, -0.0300000000000002, 
0.11, 0.0599999999999996, -0.02, -0.0399999999999996, 0, -0.0700000000000003, 
0.0600000000000001, 0.0800000000000001, 0.32, 0.0600000000000001, 
0.0600000000000001, 0.16, 0.19, -0.109999999999999), ger.ratio.change = c(0, 
-0.0100000000000002, -0.11, -0.0499999999999998, 0.0499999999999998, 
-0.02, -0.00999999999999979, 0.17, 0.04, -0.0300000000000002, 
0.11, 0.0599999999999996, -0.02, -0.0399999999999996, 0, -0.0700000000000003, 
0.0600000000000001, 0.0800000000000001, 0.32, 0.0600000000000001, 
0.0600000000000001, 0.16, 0.19, -0.109999999999999)), row.names = c(NA, 
-24L), .Names = c("year", "ger.90.10.ratio", "kanzler", "wahljahr", 
"us.90.10.ratio", "us.80.20.ratio", "ger.ratio.diff", "ger.ratio.change"
), class = "data.frame")

我想要做的是創建一個線圖,根據第三個變量的值改變線條樣式。 它應該如下圖所示 根據總統黨派關系繪制不同的線型

在單詞中:在上面的數據框中,我想根據年份繪制ger.90.10.ratio,並根據kanzler-column中的值更改行的樣式。

我到處搜索過(也許我的google-fu很糟糕?)但我真的很想知道這是否可能在R.

感謝您的幫助,非常感謝!

R的基本圖形不允許在中線更改線條屬性(類型/虛線圖案,顏色等),因此您必須拆分數據並在片段或segments()上使用lines() segments()

這不是一個完整的解決方案,但它是一個開始:

設置圖形參數(不必要,但我喜歡它):

par(las=1,bty="l")

生成用於拆分的變量(我們希望每個段區分):

dat <- transform(dat,k2=c(0,cumsum(diff(kanzler)!=0)))

拆分數據:

s <- split(dat,dat$k2)

設置一個空圖:

plot(ger.90.10.ratio~year,data=dat,type="n")

添加每個部分的行:

invisible(lapply(s,
     function(x) with(x,lines(year,ger.90.10.ratio,col=kanzler))))

在此輸入圖像描述

你可以在ggplot2稍微更緊湊(像往常一樣)這樣ggplot2 ,但我們設置的k2變量仍然有用:

library("ggplot2")
qplot(year,ger.90.10.ratio,data=dat,colour=factor(kanzler),
      group=k2,geom="line")

在此輸入圖像描述

這是一個使用lattice包的解決方案,以顯示它與基本圖形圖類似的程度。 我基本上使用@Ben分裂數據的想法。 我使用lattice面板來提供更compact解決方案。

在此輸入圖像描述

library(lattice)
dat <- transform(dat,k2=c(0,cumsum(diff(kanzler)!=0)))
s <- split(dat,dat$k2)
xyplot(ger.90.10.ratio~year,data=dat,type=c('p'),groups=kanzler,
       main = 'THE PARTISAL POLITICAL ECONOMY',
       auto.key=list(columns=2,cex=2,text=c('Democrats','Republicans')),
       panel=function(x,y,...) {
         panel.xyplot(x,y,...)
         lapply(s,function(x) 
          with(x,panel.lines(year,ger.90.10.ratio,col=kanzler)))
         panel.grid()
       })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM