2

私はこれらのデータを持っています:

dt1 <- structure(list(yr = 2004:2010, X = c(0.637, 0.9701, 0.701, 0.4535, 0.5058, 0.4698, 0.6228), lower = c(0.4254, 0.6442, 0.4699, 0.2929, 0.3311, 0.3213, 0.4276), upper = c(0.8614, 1.32, 0.955, 0.6261, 0.6901, 0.6276, 0.8385)), .Names = c("yr", "X", "lower", "upper"), row.names = 50:56, class = "data.frame")

dt2 <- structure(list(yr = 2004:2010, X = c(0.1753, 0.2872, 0.3038, 0.1994, 0.2486, 0.235, 0.2604), lower = c(0.1059, 0.1747, 0.1879, 0.1174, 0.1542, 0.1507, 0.1704), upper = c(0.2554, 0.4121, 0.4319, 0.2876, 0.3542, 0.3222, 0.3588)), .Names = c("yr", "X", "lower", "upper"), row.names = 8:14, class = "data.frame")

そして、私はこのようにggplotを使用できます:

ggplot(dt1, aes(x=yr, y=X, group=1, ymin = lower, ymax = upper)) +
    geom_ribbon(alpha = 0.2) +
    geom_line() +
    geom_point(shape=21, size=3, fill="blue") +
    theme_gray(12) +
    opts(panel.background = theme_rect(fill='grey80')) +
    ylim(0,1.7)

ここに画像の説明を入力してください

...そして同様にdt2の場合: ここに画像の説明を入力してください

ここで、両方のプロットを同じグラフに表示したいと思います。どうやってやるの ?

4

2 に答える 2

4

(元のデータフレームの名前に基づいて)両方のデータフレームに新しい変数( "dt")を追加してから、それらを新しいデータフレーム(dt3)に変換します。

dt1[,"dt"]<-"dt1"  
dt2[,"dt"]<-"dt2"  
dt3<-rbind(dt1,dt2)  
dt3  
  >   yr      X  lower  upper  dt  
50 2004 0.6370 0.4254 0.8614 dt1  
51 2005 0.9701 0.6442 1.3200 dt1  
52 2006 0.7010 0.4699 0.9550 dt1  
53 2007 0.4535 0.2929 0.6261 dt1  
54 2008 0.5058 0.3311 0.6901 dt1  
55 2009 0.4698 0.3213 0.6276 dt1  
56 2010 0.6228 0.4276 0.8385 dt1  
8  2004 0.1753 0.1059 0.2554 dt2  
9  2005 0.2872 0.1747 0.4121 dt2  
10 2006 0.3038 0.1879 0.4319 dt2  
11 2007 0.1994 0.1174 0.2876 dt2  
12 2008 0.2486 0.1542 0.3542 dt2  
13 2009 0.2350 0.1507 0.3222 dt2  
14 2010 0.2604 0.1704 0.3588 dt2  

このdt3データフレームの場合、コードは新しいvariabelの名前に変更されたグループでうまく機能します

ggplot(dt3, aes(x=yr, y=X, group=dt, ymin = lower, ymax = upper)) +  
     geom_ribbon(alpha = 0.2) +  
     geom_line() +  
     geom_point(shape=21, size=3, fill="blue") +  
     theme_gray(12) +  
     opts(panel.background = theme_rect(fill='grey80')) +  
     ylim(0,1.7)  

ポイントとラインの色を変更するには(追加:colour = dt、linetype = 0、変更:fill = "blue"の代わりに:aes(fill = dt))。最後の2行は、色をカスタマイズするためのものです。

myplot<-ggplot(dt3, aes(x=yr, y=X, group=dt, colour=dt,ymin = lower, ymax = upper)) +  
geom_ribbon(alpha = 0.2, linetype=0)+ geom_line() +  
geom_point(shape=21, size=3, aes(fill=dt)) +  
theme_gray(12) +  
opts(panel.background = theme_rect(fill='grey80')) +  
ylim(0,1.7)  
myplot<-myplot+ scale_color_manual(values=c("red", "blue"))  
myplot<-myplot+ scale_fill_manual(values=c("red", "blue"))  
myplot  

ここに画像の説明を入力してください

于 2012-07-22T18:49:03.710 に答える
3

私は試行錯誤でこの答えを見つけました:

ggplot(dt1, aes(x=yr, y=X, group=1, ymin = lower, ymax = upper)) +
    geom_ribbon(alpha = 0.2) +
    geom_line() +
    geom_point(shape=21, size=3, fill="blue") +
    theme_gray(12) +
    opts(panel.background = theme_rect(fill='grey80')) +
    ylim(0,1.7) +
    geom_path(aes(x=yr, y=X, group=1, ymin = lower, ymax = upper), data=dt2) +
    geom_ribbon(alpha = 0.2, data=dt_inh) +
    geom_point(shape=21, size=3, fill="red", data=dt2)

ここに画像の説明を入力してください

これはそれを行うための良い方法ですか?

于 2012-07-22T18:55:17.533 に答える