2017-04-11 126 views
1

我有一個數據幀,看起來像這樣:R圖形與多條曲線,多個條件和多行

var1 var2 var3 year value 
AT EA 1 02 ... 
AT EA 1 03 ... 
AT XY 2 02 ... 
AT XY 2 03 ... 
BE EA 1 02 ... 
BE EA 1 03 ... 
BE XY 2 02 ... 
BE XY 2 03 ... 

值僅僅是一個數值變量。我想要做的是產生一個圖形,顯示y軸上的值和x軸上的年份。此外,我想:

  • 一個情節爲VAR1的每個級別(這將是在xyplot因素條件)

  • VAR2的每個級別應該有不同的顏色

  • 每個級別VAR3的應該是不同類型的線圖的(連續相對於虛線)

另外,能告訴我R鍵只是使用的VAR1和VAR2噸一定水平o製作圖表。例如。只使用那些var1爲「AT」或「FR」的觀察值,但不使用「BE」或「DE」的觀察值? var1和var2在data.frame中都有13個級別。

編輯:data.frame可以下載here。我試過以下內容:

library(lattice) 
xyplot(value~year | factor(report_ctry), data=EA17_flows_ex, groups=factor(indicator), type="l") 
+0

歡迎堆棧溢出!請閱讀有關的信息[如何提出一個很好的問題( http://stackoverflow.com/help/how-to-ask)以及如何給出一個[可重現的例子](http://stackoverflow.com/questions/5963269)。這會讓其他人更容易幫助你。 – zx8754

+1

我已經編輯了開放文章以包含data.frame和我試過的代碼。 – Laubsauger

回答

0

也許有些事情是沿着?代碼未經測試。您應該提供一個小例子,以及您嘗試過並且無法使用的最佳結果。

library(ggplot2) 

# will display only those in the list, AT and FR in this case 
subxy <- xy[xy$var %in% c("AT", "FR"), ] 

ggplot(subxy, aes(x = year, y = value, color = var2, group = as.factor(var3))) + 
    theme_bw() + 
    geom_point(position = "jitter", width = 0.25) + 
    facet_wrap(~ var1) 
0

您好我不知道你怎麼想var3linetype。但是,它的工作原理不同。只具有一定的水平var1var2剛子集ggplot內的數據框或單獨(例如subset(dataframe,var1 %in% [list with var1-values you want to look at] & var2 %in% [list with var2-values you want to look at]))

library("ggplot2") 

plot = ggplot(dataframe,aes(year,value))+ 
     geom_point(aes(colour=var2,size=var3))+facet_wrap(~var1) 

ggsave("plot.png",plot,width=200,height=200,unit="mm") 

enter image description here

+0

我想要一個顯示依賴於var1,var2,a的變量值的線圖nd var3。我已經添加了data.frame的鏈接以及我在開篇文章中嘗試的代碼。如果我使用「線型」而不是「尺寸」,您的解決方案就可以工作。謝謝。 :) – Laubsauger