r - ggplot2 中的卡特彼勒图

标签 r ggplot2

我正在开展一个项目,其中我有多个响应变量并且想要显示有序的毛毛虫图。这对于基本的 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/

相关文章:

r - qplot() 中的行为可能不一致?

r - ggplot2 在 x 轴上的 Year 变量结束时不断添加 .5

r - 比较两个向量的分布

在 data.table 中舍入 POSIXct

r - 用时间序列或同一列中的相邻值替换NA值-data.table方法

r - R : Error in svd(x, nu=0, nv=k) 中的 PCA: 'x' 中的无限值或缺失值

r - 在 ggplot2 中结合 position_dodge 和 position_fill

r - 为 geom_hline 添加图例条目

r - 使用 ggplot2 的发散堆积条形图 : issue with factor ordering in legend

r - 将不带引号的变量传递给 curly curly {{}} ggplot 函数