R 用不同的颜色覆盖两个随机数图
我想知道在R-thru中是否有一种方法可以在R中的一个绘图中覆盖以下两个绘图 注意:我需要保留每个绘图的显示功能,例如,当您独立运行每个绘图时,注意,例如,每个绘图中的R 用不同的颜色覆盖两个随机数图,r,plot,R,Plot,我想知道在R-thru中是否有一种方法可以在R中的一个绘图中覆盖以下两个绘图 注意:我需要保留每个绘图的显示功能,例如,当您独立运行每个绘图时,注意,例如,每个绘图中的ylim()必须不同 这是我的R代码: # Plot #1: yy = rnorm(1000) xx = seq(-4, 4, len=1000) plot(xx, yy, ty="p", ylim=c(-4, 4), pch=20) ## Note ylim() here ## abline(h=c(0, 3, -3
ylim()
必须不同
这是我的R代码:
# Plot #1:
yy = rnorm(1000)
xx = seq(-4, 4, len=1000)
plot(xx, yy, ty="p", ylim=c(-4, 4), pch=20) ## Note ylim() here ##
abline(h=c(0, 3, -3 ), col='green', lwd=3)
# Plot # 2:
y = rcauchy(1000, 0, 1)
x = seq(-6, 6, len=1000)
plot(x, y, ty="p", col='red4', ylim=c(-10, 10), pch=20) ## Note ylim() here ##
abline(h=c(0, 2, -2), col='cyan', lwd=3)
我会这样做:
library(dplyr)
library(ggplot2)
df1 <- data.frame(x = c(seq(-4, 4, len = 1000), seq(-6, 6, len = 1000)),
y = c(rnorm(1000), rcauchy(1000, 0, 1)),
method = c(rep("rnorm", 1000), rep("rcauchy", 1000)))
df1 %>%
ggplot(aes(x, y)) + geom_point(aes(color = method)) + facet_grid(method ~ ., scales = "free")
库(dplyr)
图书馆(GG2)
df1%
ggplot(aes(x,y))+几何点(aes(颜色=方法))+刻面网格(方法),scales=“free”)
结果:
带底
R
:
par(mfrow = c(2, 1))
par(oma = c(4, 4, 0, 0))
par(mar = c(1, 1, 2, 2))
palette(colors())
# Plot #1:
yy = rnorm(1000)
xx = seq(-4, 4, len=1000)
plot(xx, yy, ty="p", col='green4', xaxt = 'n', xlim=c(-6,6), ylim=c(-4, 4), pch=20, main='Gaussian') ## Note ylim() here ##
abline(h=c(0, 3, -3 ), col='green1', lwd=3)
# Plot # 2:
y = rcauchy(1000, 0, 1)
x = seq(-6, 6, len=1000)
plot(x, y, ty="p", col='red4', ylim=c(-10, 10), pch=20, main='Cauchy') ## Note ylim() here ##
abline(h=c(0, 2, -2), col='red1', lwd=3)
mtext('x', side = 1, outer = TRUE, line = 2)
mtext('y', side = 2, outer = TRUE, line = 2)
使用晶格
:
library(lattice)
xyplot(y~x|type,
data = rbind(data.frame(x=xx, y=yy, type='Gaussian'),
data.frame(x=x, y=y, type='Cauchy')),
ylim = list(c(-4, 4), c(-10, 10)),
groups=type,
col = c("red","blue"),
pch = 19,
layout = c(1,2),
scales = list(y = list(relation = "free")))
使用ggplot网格
library(grid)
grid.newpage()
grid.draw(rbind(ggplotGrob(ggplot() + aes(x,y) + geom_point(col='red') + ylim(-10,10) +
ggtitle('Cauchy') + theme(axis.title.x = element_blank(), axis.text.x = element_blank())),
ggplotGrob(ggplot() + aes(xx,yy) + geom_point(col='blue') +
ggtitle('Gaussian') + xlim(c(-6,6)) + xlab('x') + ylab('y')), size = "last"))
我认为首先将数据合并,然后生成一个带有点的绘图,这些点根据一些描述性变量着色。@d.b,好的,我将使用点。@neilfws,请您再解释一下,我的目标是向非统计查看器显示CaCy在中间的集中度是一个标准的正常值?我会创建一个3行的数据帧:<代码> x<代码>,<代码> y>代码>和<代码>方法< /代码>。它将有2000行:前1000行是rnorm的x/y,下1000行是RCachy的x/y。
方法
列将是标签“rnorm”(第1-1000行)和“rcuchy”(第1001-2000行)。然后我会使用ggplot2
和颜色或刻面方法。这是一个很好的答案。然而,对我来说,这里使用的dplyr
可能增加很少。正确-在这种情况下,它只是通过导入magrittr
来提供%%
。我只是发现加载dplyr
比显式加载magrittr
更有用。我的观点是,您没有将任何函数调用链接在一起,因此(对我来说)为该管道加载包感觉是多余的。当然,这个问题根本不需要。只是反映了我一直在为其他东西加载的内容:)