【问题标题】:Multiple plots on one graph (filled.contour)一张图上的多个图(fill.contour)
【发布时间】:2023-03-12 12:46:01
【问题描述】:

我想在一张图上组合多个等高线图。我的代码如下所示。

d1<- read.table("/home/amy/Desktop/data.csv",sep=",",header=T)
ax<-d1$c1
ay<-d1$c2
library(MASS)
density1 <- kde2d(ax, ay)
par(mfrow=c(1,2))
filled.contour(density1, xlab= "x axis",
    ylab= "y axis ", main ="first plot", xlim=c(0,100), ylim=c(0,100), 
    color.palette=colorRampPalette(c('white','blue','yellow','darkgreen','darkred')))


    bx<-d1$c3
    by<-d1$c4
    library(MASS)
    density2 <- kde2d(bx, by)
    filled.contour(density2, xlab= "x axis",
        ylab= "y axis ", main ="second plot", xlim=c(0,100), ylim=c(0,100), 
        color.palette=colorRampPalette(c('white','blue','yellow','darkgreen','darkred')))

我的数据示例如下所示。

数据.csv

c1     c2    c3    c4

23.7  36.8   45.6  32.5
45.5  23.8   67.5  34.8 
21.6  56.9   12.5  56.7
89.5  45.4   23.3  45.7

【问题讨论】:

  • 在运行您的绘图代码之前,请执行par(mfrow=c(1,2)),例如。运行?par 以获取有关mfrow 和基本图形的其他图形参数的更多信息。
  • 在我的情况下,par(mfrow=c(1,2)) 不起作用。我只是得到了第二个地块而不是两个地块。

标签: r plot contour


【解决方案1】:

看起来filled.contour 覆盖了任何预先存在的布局,因此您不能并排绘制两个单独的图。您可以尝试修改函数代码以更改其处理布局的方式(在控制台中键入 filled.contour 以查看函数代码)以创建函数的自定义版本。另一种方法是使用lattice 包中的levelplot 绘制地块,然后使用gridExtra 包中的grid.arrange 布置它们。这是一个例子:

library(lattice)
library(gridExtra)

# Fake data (taken from the fill.contour help examples)
x <- y <- seq(-4*pi, 4*pi, len = 100)
r <- sqrt(outer(x^2, y^2, "+"))

p1 = levelplot(cos(r^2)*exp(-r/(2*pi)))
p2 = levelplot(cos(r)*exp(-r/(2*pi)))

# Lay out both plots
grid.arrange(p1, p2, ncol=2)

【讨论】:

    猜你喜欢
    • 2018-03-08
    • 2023-01-31
    • 2017-06-27
    • 2012-02-05
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多