R 中所有列与列号的点图
Dot plot of all columns in R against the column number
假设我有一个如下所示的数据框
mydf=data.frame(method=factor(1:10), x1=rpois(10,2), x2=round(rnorm(10),3), x3=rgeom(10,0.3))
我想在同一图中绘制从 X1
到 X3
的所有变量的点图。我尝试了以下代码
plot(mydf$x1~rep(1,10), xlab="", ylab = "")
plot(mydf$x2~rep(2,10), xlab="", ylab = "")
plot(mydf$x3~rep(3,10), xlab="", ylab = "")
如何将它们绘制成单图?
可能不是您想要的,但也许 ggplot
试试看?
library(dplyr)
library(tidyr)
library(ggplot)
mydf %>%
pivot_longer(-method, names_to = "variable") %>%
ggplot(aes(x = variable, y = value)) +
geom_point()
否则,只需使用 points
而不是 plot
:
plot(mydf$x1~rep(1,10), xlab="", ylab = "", xlim = c(0.5,3.5), ylim = range(mydf[,2:4]), xaxt = 'n')
points(mydf$x2~rep(2,10))
points(mydf$x3~rep(3,10))
axis(1,at = 1:3)
您可能正在寻找 matplot
。
matplot(t(mydf[-1]), pch=16, col=1, xaxt="n")
axis(1, 1:3, names(mydf[-1]))
数据:
set.seed(42)
mydf=data.frame(method=factor(1:10), x1=rpois(10,2), x2=round(rnorm(10),3), x3=rgeom(10,0.3))
假设我有一个如下所示的数据框
mydf=data.frame(method=factor(1:10), x1=rpois(10,2), x2=round(rnorm(10),3), x3=rgeom(10,0.3))
我想在同一图中绘制从 X1
到 X3
的所有变量的点图。我尝试了以下代码
plot(mydf$x1~rep(1,10), xlab="", ylab = "")
plot(mydf$x2~rep(2,10), xlab="", ylab = "")
plot(mydf$x3~rep(3,10), xlab="", ylab = "")
如何将它们绘制成单图?
可能不是您想要的,但也许 ggplot
试试看?
library(dplyr)
library(tidyr)
library(ggplot)
mydf %>%
pivot_longer(-method, names_to = "variable") %>%
ggplot(aes(x = variable, y = value)) +
geom_point()
否则,只需使用 points
而不是 plot
:
plot(mydf$x1~rep(1,10), xlab="", ylab = "", xlim = c(0.5,3.5), ylim = range(mydf[,2:4]), xaxt = 'n')
points(mydf$x2~rep(2,10))
points(mydf$x3~rep(3,10))
axis(1,at = 1:3)
您可能正在寻找 matplot
。
matplot(t(mydf[-1]), pch=16, col=1, xaxt="n")
axis(1, 1:3, names(mydf[-1]))
数据:
set.seed(42)
mydf=data.frame(method=factor(1:10), x1=rpois(10,2), x2=round(rnorm(10),3), x3=rgeom(10,0.3))