在由ggplot2生成的图下方显示文本 [英] Displaying text below the plot generated by ggplot2

查看:144
本文介绍了在由ggplot2生成的图下方显示文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 ggplot2 中创建的图下方显示一些有关数据的信息。我想使用图的X轴坐标来绘制N变量,但Y坐标需要距离屏幕底部10%。事实上,所需的Y坐标已经在y_pos变量的数据框中。



我可以使用 ggplot2 来考虑3种方法:

1)在实际绘图下方的空绘图,使用相同的刻度,然后使用geom_text绘制空白绘图上的数据。 这种方法 2>使用 geom_text 绘制数据,但以某种方式使用y坐标为屏幕的百分比(10%)。这将迫使数字显示在图下方。我无法弄清楚正确的语法。

3)使用grid.text显示文本。我可以很容易地将它设置在屏幕底部的10%处,但是我无法确定X坐标是如何设置为与剧情相匹配的。我尝试使用grconvert来捕捉最初的X位置,但无法让它工作。



下面是带有虚拟数据的基本图:

  graphics.off()#关闭图形窗口

库(汽车)
库(ggplot2)#加载ggplot
library(gridExtra)#load Grid
library(RGraphics)#支持R图形书籍,在CRAN上

#创建虚拟数据
test = data.frame(
Group = c(A,B,A,B,A,B),
x = c(1,2 ,2,3,3),
y = c(33,25,27,36,43,25),
n = c(71,55,65,58,65,58),$ b (b,b,b,b,b,b,测试,颜色=组)+
ylab(从基线的平均变化)+
geom_line()+
scale_x_continuous(Weeks,breaks = seq(-1,3,by = 1))+
opts(
legend.position = c(.1,0.9))

#display plot
p1

下面修改的gplot显示数字但是它们在情节中显示。他们强制延长Y量表。

  p1 < -  qplot(x,y,data = test,color)我想在下图中显示这些数字。 = Group)+ 
ylab(基线平均变化)+
geom_line()+
scale_x_continuous(Weeks,breaks = seq(-1,3,by = 1)) +
opts(plot.margin = unit(c(0,2,2,1),lines),
legend.position = c(.1,0.9))+
geom_text(data = test,aes(x = x,y = y_pos,label = n))

p1

显示数字的另一种方法是在实际绘图下方创建一个虚拟绘图。这里是代码:

  graphics.off()#close graphics windows 

library(car)
library(ggplot2)#load ggplot
library(gridExtra)#load Grid
library(RGraphics)#支持CRAN

#创建虚拟数据
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(15,6,15,6,15,6)



p1 < - qplot( x,y,data = test,color = group)+
ylab(基线平均变化)+
opts(plot.margin = unit(c(1,2,-1,1) ,lines))+
geom_line()+
scale_x_continuous(Weeks,breaks = seq(-1,3,by = 1))+
opts(legend.position = bottom,
legend.title = theme_blank(),
title.text =使用GGPLOT的线图)
p1

p2 < - qplot( x,y,data = test,geom =blank)+
ylab()+
opts(plot.margin = unit(c(0,2,-2,1), lines),
axis.line = theme_blank(),
axis.ticks = theme_segment(color =white),
axis.text.x = theme_text(angle = -90 ,color =white),
axis.text.y = theme_text(angle = -90,color =white),
panel.background = theme_rect(fill =transparent,color = NA),
panel.grid.minor = theme_blank(),
panel.grid.major = theme_blank()
)+
geom_text(data = test,aes(x = x,y = y_pos,label = n))
p2

grid.arrange(p1,p2,heights = c(8.5,1.5),nrow = 2)
输入这里的代码

然而,这是非常复杂的,并且很难修改不同的数据。理想情况下,我希望能够以屏幕百分比传递Y坐标。
在此先感谢!

解决方案

编辑 opts 已被弃用,取而代之的是 theme ; element_blank 已经取代 theme_blank ;和 ggtitle()来代替 opts(title = ...



桑迪 - 非常感谢你!!!!这正是我想要的,我希望我们可以在geom.text或geom.annotate中控制剪辑。



如果其他人感兴趣,我将以下程序放在一起。

  rm(list = ls()) #clear objects 
graphics.off()#关闭图形窗口

library(ggplot2)
library(gridExtra)

#create dummy data
test = data.frame(
group = c(Group 1,Group 1,Group 1,Group 2,Group 2,Group 2),
x = c(1,2,3,1,2,3),
y = c(33,25,27,36,23,25),
n = c(71,55,65, 58,65,58),
ypos = c(18,18,18,17,17,17)




p1< ; - qplot(x = x,y = y,data = test,color = group)+
ylab(从基线开始的平均变化)+
主题(plot.margin = unit(c(1 ,3,8,1),lines))+
geom_line()+
scale_x_continuous(Visits,breaks = seq( - 1,3))+
主题(legend.position =bottom,
legend.title = element_blank())+
ggtitle(线条图)


#为(ii in 1:nrow(test))创建textGrobs

{
#每次访问时显示的数字
p1 = p1 + annotation_custom(grob = textGrob(test $ n [ii]),
xmin = test $ x [ii],
xmax = test $ x [ii],
ymin = test $ ypos [ii],
ymax = test $ ypos [ii])

#显示组文本
if(ii%in%c(1,4))#这可能是更好的方法
{
p1 = p1 + annotation_custom(grob = textGrob(test $ group [ii]),
xmin = 0.85,
xmax = 0.85,
ymin = test $ ypos [ii],
ymax = test $ ypos [ii])
}

}




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


I am trying to display some information about the data below the plot created in ggplot2. I would like to plot the N variable using the X axis coordinate of the plot but the Y coordinate needs to be 10% from the bottom of the screen . In fact, the desired Y coordinates are already in the data frame as y_pos variable.

I can think of 3 approaches using ggplot2:

1) Create an empty plot below the actual plot, use the same scale and then use geom_text to plot the data over the blank plot. This approach sort of works but is extremely complicated.

2) Use geom_text to plot the data but somehow use y coordinate as percent of the screen (10%). This would force the numbers to be displayed below the plot. I can't figure out the proper syntax.

3) Use grid.text to display the text. I can easily set it at the 10% from the bottom of the screen but I can't figure how set the X coordindate to match the plot. I tried to use grconvert to capture the initial X position but could not get that to work as well.

Below is the basic plot with the dummy data:

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) ) +
  opts( 
        legend.position=c(.1,0.9))

#display plot
p1

The modified gplot below displays numbers of subjects, however they are displayed WITHIN the plot. They force the Y scale to be extended. I would like to display these numbers BELOW the plot.

    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) ) +
  opts( plot.margin = unit(c(0,2,2,1), "lines"),
        legend.position=c(.1,0.9))+
  geom_text(data = test,aes(x=x,y=y_pos,label=n))

p1

A different approach of displaying the numbers involves creating a dummy plot below the actual plot. Here is the code:

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(15,6,15,6,15,6)
  )


p1 <- qplot(x, y, data=test, colour=group) +
  ylab("Mean change from baseline") + 
  opts(plot.margin = unit(c(1,2,-1,1), "lines")) +
  geom_line()+
  scale_x_continuous("Weeks", breaks=seq(-1,3, by = 1) ) +
  opts(legend.position="bottom",
       legend.title=theme_blank(),
       title.text="Line plot using GGPLOT") 
p1

p2 <- qplot(x, y, data=test, geom="blank")+
  ylab(" ")+
  opts(     plot.margin = unit(c(0,2,-2,1), "lines"),
            axis.line = theme_blank(), 
            axis.ticks = theme_segment(colour = "white"),           
            axis.text.x=theme_text(angle=-90,colour="white"),
            axis.text.y=theme_text(angle=-90,colour="white"),
            panel.background = theme_rect(fill = "transparent",colour = NA), 
            panel.grid.minor = theme_blank(),      
            panel.grid.major = theme_blank()
            )+
  geom_text(data = test,aes(x=x,y=y_pos,label=n)) 
p2

grid.arrange(p1, p2, heights = c(8.5, 1.5),             nrow=2 )
enter code here

However, that is very complicated and would be hard to modify for different data. Ideally, I'd like to be able to pass Y coordinates as percent of the screen. Thanks in advance!

解决方案

Edited opts has been deprecated, replaced by theme; element_blank has replaced theme_blank; and ggtitle() is used in place of opts(title = ...

Sandy- thank you so much!!!! This does exactly what I want. I do wish we could control the clipping in geom.text or geom.annotate.

I put together the following program if anybody else is interested.

 rm(list = ls())     # clear objects  
 graphics.off()      # close graphics windows   

library(ggplot2)
library(gridExtra)

#create dummy data
test= data.frame(
  group = c("Group 1", "Group 1", "Group 1","Group 2", "Group 2", "Group 2"), 
  x = c(1 ,2,3,1,2,3 ),
  y = c(33,25,27,36,23,25),
  n=c(71,55,65,58,65,58),
  ypos=c(18,18,18,17,17,17)

  )


p1 <- qplot(x=x, y=y, data=test, colour=group) +
  ylab("Mean change from baseline") + 
  theme(plot.margin = unit(c(1,3,8,1), "lines")) +
  geom_line()+
  scale_x_continuous("Visits", breaks=seq(-1,3) ) +
  theme(legend.position="bottom",
       legend.title=element_blank())+
   ggtitle("Line plot") 


# Create the textGrobs 
for (ii in 1:nrow(test))
{
  #display numbers at each visit
  p1=p1+ annotation_custom(grob = textGrob(test$n[ii]),  
                           xmin = test$x[ii], 
                           xmax = test$x[ii], 
                           ymin = test$ypos[ii], 
                           ymax = test$ypos[ii])

    #display group text
    if (ii %in% c(1,4)) #there is probably a better way 
      {
    p1=p1+ annotation_custom(grob = textGrob(test$group[ii]),  
                             xmin = 0.85, 
                             xmax = 0.85, 
                             ymin = test$ypos[ii], 
                             ymax = test$ypos[ii])
    }

  }




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

这篇关于在由ggplot2生成的图下方显示文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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