web-dev-qa-db-ja.com

複数のプロットを積み重ね、垂直方向に同じx軸で、RのY軸が異なる

Date:timeベクトルに対して複数の時系列ベクトルを持つdata.frameがあります。 X軸は同じで、Y軸が一意である別々のグラフに垂直に積み重ねられた、関連するすべてのベクトルをプロットしたいと思います。これに似たグラフ: enter image description here

私のデータは次のようになります:

 dt <- structure(list(DEPTH = c(156, 156.5, 157.4, 158.15, 158.8, 159.2, 
159.75, 160.35, 160.85, 161.1, 161.6, 162.05, 162.5, 162.65, 
163.15, 163.45, 163.55, 163.8, 163.65, 163.75, 163.8, 163.8, 
163.75, 164.45, 164.8, 165.35, 165.65, 165.75, 166.1, 166.75, 
167, 167.2, 167.65, 168, 168.8, 169.3, 169.7, 170.2, 170.65, 
170.9, 171.45, 171.65, 172, 172.1, 172.25, 173, 173.4, 173.9, 
174.2, 174.6, 175, 175.25, 175.45, 175.9, 176.25, 176.7, 177, 
177.15, 177.5, 178, 178.5, 179.05, 179.2, 180.7, 181.05, 181.25, 
181.5, 181.7, 182.1, 182.3, 182.35, 182.75, 183.1, 183.65, 184.3, 
184.6, 185.1, 185.15, 185.3, 185.15, 185.25, 185.3, 185.15), 
    Smooth.Vert.Speed = c(-0.550000000000011, -0.5, -0.900000000000006, 
    -0.75, -0.650000000000006, -0.399999999999977, -0.550000000000011, 
    -0.599999999999994, -0.5, -0.25, -0.5, -0.450000000000017, 
    -0.449999999999989, -0.150000000000006, -0.5, -0.299999999999983, 
    -0.100000000000023, -0.25, 0.150000000000006, -0.0999999999999943, 
    -0.0500000000000114, 0, 0.0500000000000114, -0.699999999999989, 
    -0.350000000000023, -0.549999999999983, -0.300000000000011, 
    -0.0999999999999943, -0.349999999999994, -0.650000000000006, 
    -0.25, -0.199999999999989, -0.450000000000017, -0.349999999999994, 
    -0.800000000000011, -0.5, -0.399999999999977, -0.5, -0.450000000000017, 
    -0.25, -0.549999999999983, -0.200000000000017, -0.349999999999994, 
    -0.0999999999999943, -0.150000000000006, -0.75, -0.400000000000006, 
    -0.5, -0.299999999999983, -0.400000000000006, -0.400000000000006, 
    -0.25, -0.199999999999989, -0.450000000000017, -0.349999999999994, 
    -0.449999999999989, -0.300000000000011, -0.150000000000006, 
    -0.349999999999994, -0.5, -0.5, -0.550000000000011, -0.149999999999977, 
    -1.5, -0.350000000000023, -0.199999999999989, -0.25, -0.199999999999989, 
    -0.400000000000006, -0.200000000000017, -0.049999999999983, 
    -0.400000000000006, -0.349999999999994, -0.550000000000011, 
    -0.650000000000006, -0.299999999999983, -0.5, -0.0500000000000114, 
    -0.150000000000006, 0.150000000000006, -0.0999999999999943, 
    -0.0500000000000114, 0.150000000000006), DIVE_SURF = c("dive21", 
    "dive21", "dive21", "dive21", "dive21", "dive21", "dive21", 
    "dive21", "dive21", "dive21", "dive21", "dive21", "dive21", 
    "dive21", "dive21", "dive21", "dive21", "dive21", "dive21", 
    "dive21", "dive21", "dive21", "dive21", "dive21", "dive21", 
    "dive21", "dive21", "dive21", "dive21", "dive21", "dive21", 
    "dive21", "dive21", "dive21", "dive21", "dive21", "dive21", 
    "dive21", "dive21", "dive21", "dive21", "dive21", "dive21", 
    "dive21", "dive21", "dive21", "dive21", "dive21", "dive21", 
    "dive21", "dive21", "dive21", "dive21", "dive21", "dive21", 
    "dive21", "dive21", "dive21", "dive21", "dive21", "dive21", 
    "dive21", "dive21", "dive21", "dive21", "dive21", "dive21", 
    "dive21", "dive21", "dive21", "dive21", "dive21", "dive21", 
    "dive21", "dive21", "dive21", "dive21", "dive21", "dive21", 
    "dive21", "dive21", "dive21", "dive21"), X = c(2050L, 2062L, 
    2026L, 2078L, 2058L, 2076L, 2050L, 2068L, 2060L, 2078L, 2058L, 
    2088L, 2080L, 2065L, 2088L, 2076L, 2084L, 2105L, 2084L, 2102L, 
    2123L, 2096L, 2074L, 2054L, 2090L, 2089L, 2080L, 2078L, 2068L, 
    2092L, 2084L, 2082L, 2094L, 2056L, 2062L, 2067L, 2082L, 2084L, 
    2091L, 2058L, 2076L, 2098L, 2104L, 2090L, 2058L, 2050L, 2080L, 
    2074L, 2074L, 2082L, 2070L, 2088L, 2062L, 2062L, 2082L, 2086L, 
    2070L, 2081L, 2092L, 2058L, 2060L, 2076L, 2094L, 2083L, 2072L, 
    2107L, 2104L, 2066L, 2110L, 2104L, 2072L, 2076L, 2065L, 2042L, 
    2066L, 2093L, 2080L, 2083L, 2108L, 2107L, 2086L, 2096L, 2126L
    ), Y = c(2036L, 2000L, 2049L, 1966L, 2042L, 2078L, 2072L, 
    2055L, 2036L, 2128L, 2044L, 2112L, 2066L, 2051L, 2102L, 2060L, 
    2054L, 2043L, 2034L, 2086L, 1980L, 2076L, 2003L, 2033L, 2107L, 
    1992L, 2028L, 2027L, 2024L, 2005L, 2050L, 2010L, 1944L, 2010L, 
    2046L, 2020L, 2088L, 2086L, 2034L, 2066L, 2060L, 2152L, 2044L, 
    2078L, 2040L, 2067L, 2080L, 2072L, 2073L, 2028L, 2066L, 2082L, 
    2030L, 2042L, 1990L, 2076L, 2054L, 2064L, 2016L, 2048L, 2029L, 
    2008L, 2090L, 2038L, 2026L, 2096L, 2002L, 2025L, 2001L, 2098L, 
    2061L, 2022L, 2054L, 2064L, 2043L, 2090L, 2042L, 2086L, 2073L, 
    2066L, 2040L, 2081L, 2087L), Z = c(2488L, 2484L, 2490L, 2486L, 
    2488L, 2492L, 2498L, 2490L, 2492L, 2484L, 2491L, 2494L, 2497L, 
    2493L, 2488L, 2493L, 2494L, 2484L, 2486L, 2487L, 2478L, 2490L, 
    2478L, 2493L, 2490L, 2486L, 2488L, 2486L, 2488L, 2482L, 2488L, 
    2480L, 2480L, 2488L, 2490L, 2490L, 2490L, 2489L, 2492L, 2490L, 
    2486L, 2480L, 2488L, 2491L, 2486L, 2488L, 2488L, 2494L, 2490L, 
    2488L, 2492L, 2498L, 2484L, 2491L, 2480L, 2491L, 2497L, 2487L, 
    2482L, 2490L, 2490L, 2478L, 2488L, 2492L, 2492L, 2482L, 2484L, 
    2489L, 2482L, 2484L, 2485L, 2492L, 2488L, 2493L, 2487L, 2490L, 
    2492L, 2488L, 2490L, 2487L, 2484L, 2486L, 2478L)), .Names = c("DEPTH", 
"Smooth.Vert.Speed", "DIVE_SURF", "X", "Y", "Z"), row.names = 7222:7304, class = "data.frame")

そして、共通のX軸を持つ別々のグラフにDEPTH、X、Y、Zをプロットしようとしています。

15
Jojo Ono

古風になりたい場合は、latticeを使用できます。 @aaronwolenとは異なり、データセットにtime変数が欠落していると想定したため、次のように構成しました。

dt$time <- seq(nrow(dt))
library(reshape2)
mm <- melt(subset(dt,select=c(time,DEPTH,X,Y,Z)),id.var="time")
library(lattice)
xyplot(value~time|variable,data=mm,type="l",
       scales=list(y=list(relation="free")),
       layout=c(1,4))

enter image description here

6
Ben Bolker

@PaulHiemstraに同意します。ggplot2が最適です。

Smooth.Vert.Speedは、プロットする一般的なx軸変数です。DEPTHXY、およびZ.。

library(ggplot2)
library(reshape2)

# Add time variable as per @BenBolker's suggestion
dt$time <- seq(nrow(dt))

# Use melt to reshape data so values and variables are in separate columns
dt.df <- melt(dt, measure.vars = c("DEPTH", "X", "Y", "Z"))

ggplot(dt.df, aes(x = time, y = value)) +
  geom_line(aes(color = variable)) +
  facet_grid(variable ~ ., scales = "free_y") +
  # Suppress the legend since color isn't actually providing any information
  opts(legend.position = "none")

Plotting multiple y-variables against a common x-variable

12
aaronwolen

ただ違うのは、latticeもggplot2も含まない解決策について言及させてください-私はこれを数年前にRomainのR Graph Galleryに entry 65 with code here として投稿しました。 par()設定を使用してグラフを積み上げ、積み重ねたままにします。

垂直方向のサイズは選択によって異なることに注意してください。同じ高さでも簡単にできます。

enter image description here

8

私は実際に、Zooライブラリを使用してこれを行う別の興味深い方法を考え出しました。

library(Zoo)
z <- with(dt, Zoo(cbind(DEPTH, X, Y, Z),as.POSIXct(time))) 
plot.Zoo(z,  ylab=c("Depth (m)", "Pitch Angle (degrees)", "Swaying Acceleration (m/s^2)", "Heaving Acceleration (m/s^2)"), col=c("black", "blue", "darkred", "darkgreen"), 
     xlab = c("Time"), lwd=2, ylim=list((rev(range(dt$DEPTH))), c(-90,90), c(-10,10), c(-10,10)))

したがって、Zooプロット内で、リストフォームとして新しい軸ラベルを作成でき、すべてのプロットに異なる色を付けることができます。

4
Jojo Ono