我正在开展一个项目,其中我有多个响应变量并且想要显示有序的毛毛虫图。这对于基本的 R 绘图功能来说相当简单
#generate random data frame
set.seed(42)
my_df<-data.frame(x=rnorm(100), y=runif(100,-2,2), z=rpois(100, 10))
#3 panels of plots
par(mfrow=c(1,3))
#note the abline to show an axis at y=0
sapply(c("x", "y", "z"), function(i){ plot(sort(my_df[[i]])); abline(0,0)})
但是我不知道如何使用 ggplot2 来做到这一点。要将这三个面板放在一起,我知道我必须融化数据框,但是……如何按变量排序并稍后用 0 轴绘图?到目前为止我只有...
melt_df<-melt(my_df)
qplot(1:100, value, data=melt_df, geom="point",facets=~variable)+theme_bw(base_size=16)
最佳答案
构建 jebyrnes 答案 - 您可以将 y 轴设置为具有自由刻度作为 facet_wrap
中的参数。我们可以用 geom_hline()
添加一条水平线:
ggplot(melt_df, aes(1:100, value)) + geom_point() +
facet_wrap(~ variable, ncol = 3, scales = "free_y") +
geom_hline(aes(intercept = 0), linetype = 2) +
theme_bw(base_size = 16)
您可以使用 stat_qq()
获得相同的结果,并避免预先使用 ddply
进行排序。不同的是,我们只需要将sample
参数传入aes
即可:
ggplot(melt_df, aes(sample = value)) + geom_point(stat = "qq") +
facet_wrap(~ variable, ncol = 3, scales = "free_y") +
geom_hline(aes(intercept = 0), linetype = 2) +
theme_bw(base_size = 16)
关于r - ggplot2 中的卡特彼勒图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5463471/