r分数绘制标签图 [英] r label plots with fractions

查看:160
本文介绍了r分数绘制标签图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建3个图,每个图包含来自不同数据框的2行图,然后用特定分数标记每个图。



例如我有3个数据框:

  df1 <  -  data.frame(x = c(1,2,3,4),y = c(2,3,4,5),z = c(3,3,6,8))
df2 < - data.frame(x = c(3,4,5,6),y = c(1,3,6,7),z = c(2,4,4,8))$ b $数据帧(x = c(1,2,2,3),y = c(2,5,6,9),z = c(2,5,6,7))

我想:

<1>为每个数据框创建3个不同的图,每个图都有一条红线和一条蓝线;



2)在每幅图的蓝线上添加一个注释,使用每个图的一个不同的分数。

例如,数据帧1的图是这样的:

  p1 < -  ggplot(data = df1)+ geom_line( aes(x = x,y = y,color =blue))+ geom_line(aes(x = x,y = z,color =red))+ scale_colour_manual(name =data,values = c red,blue))

然后在蓝线上添加标签, :

  p1 + geom_text(aes(x = df1 $ x [which.max(df1 $ y)] + 1,y = max(df1 $ y)+4,label ={\frac {23 22 22} {44 28 32}},size = 2,parse = TRUE))

但这不起作用,而且我搜索了这么多个小时,并且找不到如何在注释中使用分数(以及括号中的括号)。任何帮助深表感谢!



-fra

解决方案

不清楚你想要什么。这是一个尝试;




  • 我使用 mapply 循环绘图和分数,生成一个图表列表。

  • 我使用 frac(x,y)
  • 创建分数
  • 我使用 scale_y_continuous

  • 设置图的限制我使用 gridExtra 来排列(可选)



这里是完整的代码:

 

code> ##一个通用函数,它将一个分数和一个数据帧作为输入
##它生成一个图
plot.frac< - function(dat,frac){
c <-ggplot(dat)+
geom_line(aes(x = x,y = y,color =blue))+
geom_line(aes(x = x,y = z, color =red))+
scale_colour_manual(name =data,values = c(red,blue))+
geom_text(x = dat $ x [which.max dat $ y)] - 0.05,y = max(dat $ y)+4,
label = frac,size = 5,parse = TRUE)+
##注意在这里使用限制来显示注释
scale_y_continuous(limits = c(min(dat $ y),max(dat $ y)+5))
p
}
##创建一个mapply数据框架列表
df.list< - list(df1,df2,df3)
## ggplot2使用plotmath所以对于使用frac(x,y)的分数
##这里我使用粘贴
frac.func< - function(num,den)paste( 'frac('',num,','',den,')',sep ='')
num1 < - line1:23 22 22
den1 < - line2:44 28 32
num2 - line1:23 50 22
den2 < - line2:44 50 32
num3 < - line1:23 80 22
den3 < - line2:44 80 32
##创建一个小数点分数列表

frac.list< - list(frac.func(num1 ,
frac.func(num2,den2),
frac.func(num3,den3))
frac.list< - list(frac,frac,frac)
##使用mapply来调用2列dat中的图a.frame和分数
ll< - mapply(plot.frac,df.list,frac.list,SIMPLIFY = FALSE)
library(gridExtra)
do.call(grid.arrange ,ll)


I would like to create 3 plots each containing a plot of 2 lines from different data frames, and then label each plot with a specific fraction.

So for example I have the 3 data frames:

df1 <- data.frame(x=c(1,2,3,4),y=c(2,3,4,5), z=c(3,3,6,8))
df2 <- data.frame(x=c(3,4,5,6),y=c(1,3,6,7), z=c(2,4,4,8))
df3 <- data.frame(x=c(1,2,2,3),y=c(2,5,6,9), z=c(2,5,6,7))

And I would like to:

1) Create 3 different plots for each data frame, each with one red and one blue line;

2) Add an annotation over the blue line of each plot using a different fraction for each plot.

For example the plot for data frame 1 is something like this:

p1 <- ggplot(data = df1) + geom_line(aes(x=x,y=y, colour="blue")) + geom_line(aes(x=x,y=z, colour="red")) +  scale_colour_manual(name="data", values=c("red", "blue"))

Then to add the labels over the blue line I have tried:

p1 + geom_text(aes(x=df1$x[which.max(df1$y)]+1, y = max(df1$y)+4,  label = "{\frac{23 22 22}{44 28 32}}", size=2, parse=TRUE))

But this does not work, and I have searched so many hours and cannot find how to use fractions (and brackets enclosing the fraction) in the annotations. Any help is deeply appreciated!

-fra

解决方案

It is not clear what do you want to have. This is an attempt;

  • I use mapply to loop over plots and fractions and generate a list of plots.
  • I create fractions using frac(x,y)
  • I set limits of plots using scale_y_continuous
  • I use gridExtra to arrange plots in the same plot (optional)

Here the complete code:

 ## a generic function that take a fraction ana a  data.frame as inputs
 ## it generate a plot
 plot.frac <- function(dat,frac){
     p <-  ggplot(dat) + 
           geom_line(aes(x=x,y=y, colour="blue")) + 
           geom_line(aes(x=x,y=z, colour="red")) +  
           scale_colour_manual(name="data", values=c("red", "blue"))+
           geom_text(x=dat$x[which.max(dat$y)]-0.05, y = max(dat$y)+4,  
                     label = frac, size=5,parse=TRUE)+
           ## Note the use of limits here to display the annotation 
           scale_y_continuous(limits = c(min(dat$y), max(dat$y)+5))
     p
    }
## create a list of data.frame of mapply    
df.list <- list(df1,df2,df3)
## ggplot2 use plotmath so  for fraction you use frac(x,y)
## here I construct the 2 terms using paste
frac.func <- function(num,den) paste('frac("',num,'","',den,'")',sep='')
num1 <- "line1:23 22 22"
den1 <- "line2: 44 28 32"
num2 <- "line1:23 50 22"
den2 <- "line2: 44 50 32"
num3 <- "line1:23 80 22"
den3 <- "line2: 44 80 32"
## create a list of fractions for mapply

frac.list <- list(frac.func(num1,den1),
              frac.func(num2,den2),
              frac.func(num3,den3))
frac.list <- list(frac,frac,frac)
## use mapply to call the plot over the 2 lists of data.frame and fractions
ll <- mapply(plot.frac,df.list,frac.list,SIMPLIFY=FALSE)
library(gridExtra)
do.call(grid.arrange,ll)

这篇关于r分数绘制标签图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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