5

ggplot2を使用してスタック領域プロットを作成し、のいくつかの位置に垂直線を追加しましたx-axis

ここで、これらの垂直線で区切られたセクションに名前を付けます。その例は、プロット例に示されているように見える可能性があります。他の解決策も歓迎します。区間の名前のベクトルと名前のベ​​クトルがbreaks (x-axis)あります。

コード:

library(ggplot2)
d <- read.delim(...)
x_breaks = c(-3999,1,599,4076,7557,11556)

png(output, width=800, height=400)

ggplot(d, aes(x=p, y=c, group=Groups, fill=Groups)) +
geom_area(position="stack") +
opts(title="testtestest",
...) +
scale_x_continuous(expand=c(0,0), breaks=x_breaks) +
scale_y_continuous(expand=c(0,0)) +
geom_vline(xintercept=x_breaks[which(x_breaks != min(x_breaks) & x_breaks != max(x_breaks))])

dev.off()

縦線で区切られたセクションに名前を付けるにはどうすればよいですか?

4

1 に答える 1

5

データがなければ、例(休憩など)を再現するのは少し難しいです。しかし、以下はあなたが始めるのに役立つはずです。

かなり簡単な解決策の1つは、ggplot2のannotate()関数を使用して、垂直線の中間のプロットパネルにテキスト注釈を追加することです。

# Load packages
library (ggplot2)
library(grid)

# Some data
df = data.frame(x = 1:10, y = 1:10)

# Base plot
p <- ggplot(df, aes(x,y)) + geom_point() + 
  scale_x_continuous(limits = c(0, 11), expand = c(0,0)) +
  geom_vline(xintercept = 3) + geom_vline(xintercept = 7)

# Add the annotations
p + annotate("text", x = .5*(0+3), y = 11, label = "Part 1") +
     annotate("text", x = .5*(3+7), y = 11, label = "Part 2") +
     annotate("text", x = .5*(7+11), y = 11, label = "Part 3")

結果は次のとおりです。

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

または、注釈に関する限り、プロット例に近いソリューション。annotation_custom()線を描画し、プロットパネルの外側にテキストを配置するために使用します。注釈はプロットパネルの外側に描画されますが、注釈の配置はデータ座標の観点から行われます。ベースプロットの下マージンは、注釈の余地を与えるために広げられています。このソリューションでは、ggplotによるプロット要素のプロットパネルへのクリッピングをオーバーライドするコードが必要です。

更新 optsは非推奨です。代わりに使用しthemeます。

# Base plot
p <- ggplot(df, aes(x,y)) + geom_point() + 
  scale_x_continuous(limits = c(0, 11), expand = c(0,0)) +
  geom_vline(xintercept = 3) + geom_vline(xintercept = 7) +
  theme(plot.margin = unit(c(1,1,4,1), "lines"))

# Create the text Grobs
Text1 = textGrob("Part 1")
Text2 = textGrob("Part 2")
Text3 = textGrob("Part 3")

# Add the annotations
# Segment 1
p1 = p + 
    annotation_custom(grob = linesGrob(), xmin = 0, xmax = 0, ymin = -1, ymax = -.75) +
    annotation_custom(grob = linesGrob(), xmin = 0, xmax = 3, ymin = -1, ymax = -1) +
    annotation_custom(grob = linesGrob(), xmin = 3, xmax = 3, ymin = -1, ymax = -.75) +
    annotation_custom(grob = Text1,  xmin = 0, xmax = 3, ymin = -1.25, ymax = -1.25)

# Segment 2
p1 = p1 + 
    annotation_custom(grob = linesGrob(), xmin = 3, xmax = 7, ymin = -1, ymax = -1) +
    annotation_custom(grob = linesGrob(), xmin = 7, xmax = 7, ymin = -1, ymax = -.75) +
    annotation_custom(grob = Text2,   xmin = 3, xmax = 7, ymin = -1.25, ymax = -1.25) 

# Segment 3
p1 = p1 + 
    annotation_custom(grob = linesGrob(), xmin = 7, xmax = 11, ymin = -1, ymax = -1) +
    annotation_custom(grob = linesGrob(), xmin = 11, xmax = 11, ymin = -1, ymax = -.75) +
    annotation_custom(grob = Text3,  xmin = 7, xmax = 11, ymin = -1.25, ymax = -1.25)

# Code to override clipping
gt <- ggplot_gtable(ggplot_build(p1))
gt$layout$clip[gt$layout$name=="panel"] <- "off"
grid.draw(gt)

結果は次のとおりです。

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

于 2012-08-31T23:38:30.800 に答える