简体   繁体   English

如何使用 R 在一个 plot 上叠加 3 个功能

[英]How to overlay 3 functions on one plot using R

I have three functions and a plot code:我有三个功能和一个 plot 代码:

f1 <- function(c){0.187*c-0.000236*c^2+0.194*10-0.00330*100-0.000406*10}
f2 <- function(c){0.187*c-0.000236*c^2+0.194*16.53-0.00330*(16.53^2)-0.000406*16.53}
f3 <- function(c){0.187*c-0.000236*c^2+0.194*20-0.00330*400-0.000406*20}

I wish to plot all three of these on the same graph.我希望 plot 所有这三个都在同一张图上。 I currently have:我目前有:

png("figure.png")
plot(f1(1:1000), type="l", xlab="x", ylab="y", main="the plot :)")
plot(f2(1:1000), type="l", xlab="x", ylab="y", add = T)
dev.off()

So far this produces just f1 on a plot as opposed to f1 and f2 .到目前为止,这仅在 plot 上产生f1而不是f1f2 I believe I am taking the wrong approach because I am producing another plot and trying to add it to a pre-existing plot.我相信我采取了错误的方法,因为我正在生产另一个 plot并试图将其添加到预先存在的 plot 中。 I am unsure whether to use geom_line or something similar and just overlay it.我不确定是否使用 geom_line 或类似的东西,只是覆盖它。

Is there a straight forward way to plot multiple functions and overlay them in the same plot? plot 多个功能是否有直接的方法并将它们覆盖在同一个 plot 中?

geom_line is for ggplot2 , which is an entirely different plotting system. geom_line用于ggplot2 ,这是一个完全不同的绘图系统。

If you start with plot() , you can use lines() to draw lines on your current plot.如果你从plot()开始,你可以使用lines()在你当前的 plot 上画线。 Your lines are pretty close together, so it doesn't matter much here, but with base plot you usually want to calculate the maximum range in advance so your can set your plot window up right from the start:您的线条非常接近,因此在这里并不重要,但是对于基本plot ,您通常需要提前计算最大范围,以便您可以设置您的 plot Z05B8C74CBD96FBF2DE4C1A352702FFF4:

x = 1:1000
y1 = f1(x)
y2 = f2(x)
y3 = f3(x)

y_range = range(c(y1, y2, y3))

plot(x, y1, ylim = y_range, type="l", xlab="x", ylab="y", main="the plot :)", col = "red")
lines(x, y2, col = "blue")
lines(x, y3, col = "chartreuse")

在此处输入图像描述

ggplot2 is made to work with data in data frames - particularly long-format data frames. ggplot2用于处理数据帧中的数据 - 特别是长格式数据帧。 Here's how we might approach the problem with ggplot .以下是我们如何使用ggplot解决问题。 (Note that, unlike above, ggplot calculates the plot limits and gives a nice legend automatically.) (注意,与上面不同, ggplot计算 plot 限制并自动给出一个漂亮的图例。)

library(ggplot2)
dd = data.frame(x, y1, y2, y3)
d_long = reshape2::melt(data = dd, id.vars = "x", variable.name = "fun", value.name = "y")
ggplot(d_long, aes(x = x, y = y, color = fun)) +
  geom_line()

在此处输入图像描述

OR sticking with base R plotting like your code, you can just add the extra functions using lines或者坚持使用基本 R 像您的代码一样绘图,您可以使用lines添加额外的功能

plot(f1(1:1000), type="l", xlab="x", ylab="y", main="the plot :)")
lines(1:1000, f2(1:1000))
lines(1:1000, f3(1:1000))

If you want two plots one right next to the other, you have to set the parameter of your palette.如果您想要两个紧挨着另一个的图,则必须设置调色板的参数。 Use par(mfrow=c(1,2)) after the png() command.在 png() 命令之后使用 par(mfrow=c(1,2))。

png("figure.png")
par(mfrow=c(1,2))
plot(f1(1:1000), type="l", xlab="x", ylab="y", main="the plot :)")
plot(f2(1:1000), type="l", xlab="x", ylab="y", add = T)
dev.off()

For functions you can also use curve :对于函数,您还可以使用curve

f1 <- function(c){0.187*c-0.000236*c^2+0.194*10-0.00330*100-0.000406*10}
f2 <- function(c){0.187*c-0.000236*c^2+0.194*16.53-0.00330*(16.53^2)-0.000406*16.53}
f3 <- function(c){0.187*c-0.000236*c^2+0.194*20-0.00330*400-0.000406*20}

c0 <- 1
c <- 1000
curve(f1, c0, c, main = 'the plot :)', xlab = 'x', ylab = 'y')
curve(f2, c0, c, add = T)
curve(f3, c0, c, add = T)

r 编程曲线

As @Gregor noted, geom_line() is a ggplot() call.正如@Gregor 所说, geom_line()是一个ggplot()调用。 To go all into the tidyverse , you can do:将 go 全部放入tidyverse中,可以这样做:

#or with ggplot / geom_line
library(tidyverse)

map_df(list(f1 =f1,f2 =  f2,f3 = f3), exec, 1:1000)%>%
  mutate(x = 1:1000)%>%
  gather(key = fx,value = value, -x)%>%
  ggplot(aes(x = x, y = value, col = fx)) + geom_line() 

ggplot 和 purrr 多种功能

Finally, you may be interested in facet_grid as well:最后,您可能也对facet_grid感兴趣:

map_df(list(f1 =f1,f2 =  f2,f3 = f3), exec, 1:1000)%>%
  mutate(x = 1:1000)%>%
  gather(key = fx,value = value, -x)%>%
  ggplot(aes(x = x, y = value)) + geom_line() +
  facet_grid(rows = vars(fx))

ggplot facet_grid

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM