[英]Catepillar plots in ggplot2
I'm working on a project where I have multiple response variables and want to show ordered caterpillar plots. 我正在开展一个项目,我有多个响应变量并希望显示有序的履带图。 This is fairly straightforward with the base R plotting capabilities
基本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)})
But I am at a loss as to how to do this with ggplot2. 但我不知道如何使用ggplot2来做到这一点。 To put together the three panels, I know I have to melt the data frame, but then...how does one do the ordering by variable and plotting with a 0 axis later?
为了将这三个面板放在一起,我知道我必须融合数据框,但是......如何通过变量进行排序并在以后用0轴进行绘图? All I have so far is...
我到目前为止所有的一切......
melt_df<-melt(my_df)
qplot(1:100, value, data=melt_df, geom="point",facets=~variable)+theme_bw(base_size=16)
Building off jebyrnes answer - you can set the y-axis to have free scales as an argument in facet_wrap
. 建立jebyrnes答案 - 您可以设置y轴以在
facet_wrap
自由比例作为参数。 We can add a horizontal line with geom_hline()
: 我们可以用
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)
You can get equivalent results by using stat_qq()
and avoid the sorting with ddply
beforehand. 你可以使用相同的结果
stat_qq()
并避免与排序ddply
事前。 The difference is that we only need to pass in the sample
argument to aes
: 不同之处在于我们只需要将
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)
######## Begin: base R approach ###########
#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)})
######## End: base R approach ###########
######## Begin: data.table + ggplot2 approach ###########
library(data.table)
library(ggplot2)
melt_df<-melt(my_df)
melt_dt<-melt_df
setDT(melt_dt)
setkey(melt_dt, variable, value)
catey <- ggplot(melt_dt, aes(x=rep(1:100,3), y=value)) + geom_point() +
facet_wrap(~ variable, ncol = 3, scales = "free_y") +
geom_hline(aes(intercept = 0), linetype = 2) +
theme_bw(base_size = 16)
## catepillar:
catey
## with line ranges:
melt_dt[, minvalue:= -2*value+value]
melt_dt[, maxvalue:= 2*value+value]
butterfly <- catey + geom_linerange(aes(ymin=minvalue, ymax=maxvalue))
butterfly
######## End: data.table + ggplot2 approach ###########
Step 1: The ordering is done by ddply. 第1步:订购由ddply完成。
melt_df<-melt(my_df)
melt_df<-ddply(melt_df, .(variable), summarize, value=sort(value))
qplot(1:100, value, data=melt_df, geom="point",facets=~variable)+theme_bw(base_size=16)
The axis part is still puzzling to me, though, particularly given the 3 facets. 轴部分仍然令我感到困惑,特别是考虑到3个方面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.