How to name sections on x axis that are separated by vertical lines in an R plot (package ggplot2)?

落爺英雄遲暮 提交于 2020-01-02 04:46:10

问题


I have created a stacked area plot with ggplot2 and added vertical lines at some positions on the x-axis.

I now want to name the sections that are separated by these vertical lines. An example of it could look like it is shown on the example plot. Other solutions are also welcome. I have a vector of breaks (x-axis) and a vector of the names for the intervals.

Code:

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()


回答1:


Without data, it's a bit hard to reproduce your example (your breaks, etc). But the following should get you started.

One fairly straightforward solution is to use ggplot2's annotate() function to add text annotations to the plot panel midway between the vertical lines.

# 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")

The result is:

Or a solution that is close to your example plot as far as the annotations are concerned. It uses annotation_custom() to draw lines and to position text outside the plot panel. Even though the annotations are drawn outside the plot panel, the positioning of the annotations is in terms of the data coordinates. The bottom margin in the base plot is widened to give room for the annotations. The solution requires code to override ggplot's clipping of plot elements to the plot panel.

Update opts is deprecated; using theme instead.

# 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)

The result is:



来源:https://stackoverflow.com/questions/12196529/how-to-name-sections-on-x-axis-that-are-separated-by-vertical-lines-in-an-r-plot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!