如何命名R图中包含垂直线的x轴上的部分(包ggplot2)? [英] How to name sections on x axis that are separated by vertical lines in an R plot (package ggplot2)?

查看:484
本文介绍了如何命名R图中包含垂直线的x轴上的部分(包ggplot2)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在 x轴 ggplot2 的叠加区域图并添加了垂直线 >。

我现在想命名由这些垂直线分隔的部分。它的一个例子可能看起来像它在示例图中所示。其他解决方案也很受欢迎。
我有一个向量中断(x轴)和一个间隔名称的向量。



(...)
x_breaks = c(-3999,1,599,4076,7557,11556)

png(输出,宽度= 800,高度= 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()

但是下面的内容应该让你开始。



一个相当直接的解决方案是使用ggplot2的 annotate()函数来添加文本注释到垂直线之间的绘图面板中间。

 #加载包
库(ggplot2)
库(网格)

#一些数据
df = data.frame(x = 1:10,y = 1:10)

#基底图
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)

#添加批注
p +批注(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 已被弃用; 主题。 - 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))

#创建文本Grobs
Text1 = textGrob(Part 1)
Text2 = textGrob(Part 2)
Text3 = textGrob(Part 3)

#Add注释
#段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)

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

#覆盖剪辑的代码
gt< - ggplot_gtable(ggplot_build(p1))
gt $ layout $ clip [gt $ layout $ name ==panel]< - off
grid.draw(gt)

结果是:




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

解决方案

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:

这篇关于如何命名R图中包含垂直线的x轴上的部分(包ggplot2)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆