2017-09-06 9 views
1

私は以下の再現可能なコードを持っています。凡例を追加してプロットを分割する

  • このプロットに凡例を追加して、赤い曲線がyを表し、緑の曲線がzを表すことを示すにはどうすればよいですか?
  • プロットを8つのセクションに分割するにはどうすればいいですか? A = 4〜9、B = 10〜15、C = 16〜21 D = 1,2,3,22,23,24 AA = 28〜33、BBは= 24 39、CC = 40

45とDD = 25、26、27、46、47及び​​48私のコードはここにある:へ

require(ggplot2) 
x <- 1:48 
y <- rnorm(length(x)) 
z <- rnorm(length(x)) 
df <- data.frame(x, y, z) 

ggplot(df, aes(x = x, y = y)) + 
    geom_line(aes(y = y), colour = "red") + 
    geom_line(aes(y = z), colour = "green") 
+0

これは、それらの値が不連続であるとき、DおよびDDは、「セクション」にどのように対応するかを理解するのは難しいですか?そして、私はBBが34-39であると考えていますか? – neilfws

+0

これは、ラベル(A、B、...)をX軸上の適切な場所に追加することです。 – Joe

答えて

3

あなたはセクションを示すために、プロットの下部にカラースケールを追加することができます。このような何か:

library(tidyr) 
library(ggplot2) 
df1 <- data.frame(x = 1:48, y = rnorm(48), z = rnorm(48), 
        section = c(rep("D", 3), rep("A", 6), rep("B", 6), rep("C", 6), 
           rep("D", 3), rep("DD", 3), rep("AA", 6), rep("BB", 6), 
           rep("CC", 6), rep("DD", 3)) 

df1 %>% 
    gather(var, val, -x, -section) %>% 
    ggplot(aes(x, val)) + 
    geom_line(aes(color = var, group = var)) + 
    scale_color_manual(values = c("red", "green")) + 
    geom_rect(aes(xmin = x, xmax = lead(x), ymin = -Inf, ymax = min(val), fill = section)) + 
    scale_fill_brewer(palette = "Spectral") + 
    theme_bw() 

enter image description here

は、あなたも、セクションで背景を埋めることができ、その後私はラインを区別するために、代わりに色の線種を使用すると思います。

df1 %>% 
    gather(var, val, -x, -section) %>% 
    ggplot(aes(x, val)) + 
    geom_line(aes(linetype = var, group = var)) + 
    geom_rect(aes(xmin = x, xmax = lead(x), ymin = -Inf, ymax = Inf, fill = section), 
       alpha = 0.2, show.legend = FALSE) + 
    scale_fill_brewer(palette = "Spectral") + 
    theme_bw() 

enter image description here

0

アンサー最初の質問:

library(ggplot2) 
library(tidyr) 

x <- 1:48 
y <- rnorm(length(x)) 
z <- rnorm(length(x)) 
df <- data.frame(x,y,z) 

df <- gather(df, 'y', 'z', key = "group", value = "value") 

ggplot(df, aes(x = x, y = value, color = group)) + 
geom_line() + 
scale_color_manual(values=c("red", "green")) 
0

あなたが意味プロットを分割していますが、それは次のようになりたい場合は、これは、2番目の質問に答える:

A faceted plot

# Converts numbers from 1 to 48 into one of (A, AA, B, ... D, DD) 
codify <- function(value) { 
     c(
      rep('D', 3), # 1 to 3 
      rep('A', 6), # 4 to 9 
      rep('B', 6), # 10 to 15 
      rep('C', 6), # 16 to 21 
      rep('D', 3), # 22 to 24 
      rep('DD', 6), # 25 to 27 
      rep('AA', 6), # 28 to 33 
      rep('BB', 6), # 34 to 39 (I'm assuming you meant BB goes from 34) 
      rep('CC', 6), # 40 to 45 (I'm assuming you meant BB goes from 34) 
      rep('DD', 3) # 46 to 48 (I'm assuming you meant BB goes from 34) 
      )[value] 
} 


require(ggplot2) 
x <- 1:48 
y <- rnorm(length(x)) 
z <- rnorm(length(x)) 
type <- codify(x) 
df <- data.frame(x, y, z, type) 

ggplot(df, 
     aes(x=x,y=y)) + 
    geom_line(aes(y=y),colour="red") + 
    geom_line(aes(y=z),colour="green") + 
    facet_wrap(~type) 
+0

あなたの助けてくれてありがとう、私が探しているものは、複数のプロットを持っていない。私はちょうど(A、B、...)のラベルをX軸上の適切な場所に追加する方法を探しています。 – Joe

関連する問題