我试图在ggplot2中创建的图表下面显示一些有关数据的信息.我想使用图的X轴坐标绘制N变量,但Y坐标需要距离屏幕底部10%.实际上,期望的Y坐标已经作为y_pos变量存在于数据框中.
我可以想到使用ggplot2的3种方法:
1)在实际绘图下方创建一个空图,使用相同的比例,然后使用geom_text在空白图上绘制数据.这种方法有点有效,但非常复杂.
2)geom_text
用于绘制数据,但以某种方式使用y坐标作为屏幕的百分比(10%).这将强制数字显示在图表下方.我无法弄清楚正确的语法.
3)使用grid.text显示文本.我可以轻松地将它设置在屏幕底部的10%,但我无法确定如何设置X coordindate以匹配绘图.我试图使用grconvert捕获最初的X位置但是也无法使其工作.
以下是虚拟数据的基本情节:
graphics.off() # close graphics windows
library(car)
library(ggplot2) #load ggplot
library(gridExtra) #load Grid
library(RGraphics) # support of the "R graphics" book, on CRAN
#create dummy data
test= data.frame(
Group = c("A", "B", "A","B", "A", "B"),
x = c(1 ,1,2,2,3,3 ),
y = c(33,25,27,36,43,25),
n=c(71,55,65,58,65,58),
y_pos=c(9,6,9,6,9,6)
)
#create ggplot
p1 <- qplot(x, y, data=test, colour=Group) +
ylab("Mean change from baseline") +
geom_line()+
scale_x_continuous("Weeks", breaks=seq(-1,3, by = 1) …
Run Code Online (Sandbox Code Playgroud)