在ggplot2中添加带有点的图线 [英] Adding line with points to a plot in ggplot2

查看:404
本文介绍了在ggplot2中添加带有点的图线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了方便起见,我想写一个函数,将线图和散点图添加到已经存在的图中。我定义:

  addlinetoplot<  - 函数(dataset,varx,vary)
{
p< - geom_line(data = dataset,aes_string(x = varx,y = vary))+
geom_point(data = dataset,aes_string(x = varx,y = vary))

p

上述功能不起作用。如果我删除了 + 标记和 geom_point()部分,它就可以工作。以上不起作用,因为某些原因,不能添加这两个geoms。以下示例说明了这个问题。

然后,我试过:

  df1 < -  data。帧(c1 = c(1:10),c2 = c(1:10))
c1 < - c(1:10)
csq < - c1 ^ 2
df2 < - data.frame(c1 = c(1:10),csq)
pltbase< - ggplot()+ geom_line(df1,aes(x =c1,y =c2))

#这不起作用。
pltbase + addlinetoplot(dataset = df2,varx =c1,vary =csq)



  addthistotheplot<  -  geom_line(data = df2,aes_string(x = c1,y =csq))+ 
geom_point(data = df2,aes_string(x =c1,y =csq))
pre>

尝试定义上述语句会抛出一个错误:二元运算符的非数字参数。



如何定义 addlinetoplot()函数以便我可以:

  pltbase + addlinetoplot(dataset = df2,varx =c1,vary =csq)

工作。一种方法是分离geom_line和geom_point,并为每个定义不同的函数定义。有没有办法做到这一点在同一个函数?

谢谢!

解决方案

现在我找不到引用,但是在某处有一个答案,说明您可以通过将新参数作为列表进行传递来添加到ggplot图中。因此,你的函数变成:
$ b $ pre $ addlinetoplot < - function(dataset,varx,vary){
list(
geom_line(data = dataset,aes_string(x = varx,y = vary)),
geom_point(data = dataset,aes_string(x = varx,y = vary))

}

然后您的绘图代码如下所示:

  pltbase < -  ggplot()+ geom_line(data = df1,aes(x = c1,y = c2))
pltbase + addlinetoplot(df2,varx = c1,vary =csq)

结果为:


For my convenience, I want to write a function that adds a line plot and a scatter plot to an already existing plot. I defined:

addlinetoplot <- function(dataset, varx, vary)
     { 
       p <- geom_line(data=dataset, aes_string(x=varx, y=vary)) + 
            geom_point(data=dataset, aes_string(x=varx, y=vary))

       p
     }

The above function does not work. It works if I remove the + sign and the geom_point() part. The above does not work because for some reason one cannot add those two geoms. The following example illustrates the problem.

Then, I tried:

df1 <- data.frame(c1 = c(1:10), c2 = c(1:10))
c1 <- c(1:10)
csq <- c1^2
df2 <- data.frame(c1 = c(1:10), csq)
pltbase <- ggplot() + geom_line(df1, aes(x="c1", y="c2")) 

# This does not work.
pltbase + addlinetoplot(dataset=df2, varx = "c1", vary = "csq")

I figured the problem is with the statement:

addthistotheplot <- geom_line(data=df2, aes_string(x="c1", y="csq")) + 
                    geom_point(data=df2, aes_string(x="c1", y="csq"))

Trying to define the above statement throws an error: non-numeric argument to binary operator.

How can I define the addlinetoplot() function so that I can make:

pltbase + addlinetoplot(dataset=df2, varx = "c1", vary = "csq")

work. One way is to separate out the geom_line and geom_point and have different function definition for each. Is there a way to do it in the same function?

Thanks!

解决方案

I can't find the reference right now, but there's an answer on SO somewhere that explains you can add to a ggplot plot by passing new arguments as a list. Accordingly, your function becomes:

addlinetoplot <- function(dataset, varx, vary) { 
  list(
    geom_line(data=dataset, aes_string(x=varx, y=vary)), 
    geom_point(data=dataset, aes_string(x=varx, y=vary))
  )
}

And then your plotting code looks like:

pltbase <- ggplot() + geom_line(data = df1, aes(x=c1, y=c2))
pltbase + addlinetoplot(df2, varx = "c1", vary = "csq")

Results in:

这篇关于在ggplot2中添加带有点的图线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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