我有这个数据集:
head(long_format_female)
SampleID variable value
R1 Week7 0.0000
R1 Week9 478.8631
R1 Week11 14412.3013
R1 Week13 146708.1029
R1 Week15 265801.9593
R1 Week17 518708.2570
R1 Week19 978497.0982
R1 Week21 NA
R2 Week7 0.0000
R2 Week9 0.0000
R2 Week11 0.0000
R2 Week13 0.0000
R2 Week15 0.0000
R2 Week17 440.5659
R2 Week19 1551.0876
R2 Week21 12523.7732
我想绘制各个指数曲线(每个SampleID
一行),显示几周内的特征增长。我尝试过这段代码,它给出了显示geom_points的预期输出数字,但没有geom_line。
ggplot(long_format_female, aes(x = variable, y = value, colour = factor(SampleID))) +
geom_point() +
geom_line() +
stat_smooth(method = 'nls', formula = y ~ a*exp(b *x), aes(colour = 'Exponential'), se = FALSE, start = list(a=1,b=1)) + xlab("Time(weeks)") +
ylab("Trait") +
theme(panel.border = element_blank(),
axis.title=element_text(size=16),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"))
我也收到这两条消息:
geom_smooth: Only one unique x value each group.Maybe you want aes(group = 1)?
geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?
如何将geom_line
添加到我的情节中?
答案 0 :(得分:0)
根据我的经验,如果您要使用geom_line
,则需要为x和y设置数值,geom_point
对因素的容忍度更加宽容。
注意:在与Heroka讨论后,我们发现geom_line
可以正常使用“分组因子”(即如果你在你的情节中指定了一个组参数),但不喜欢它们未分组和抱怨并且不会画一条线。
我真的不知道你想要做什么顺利,但我可能需要更好的数据。在任何情况下,当前ggplot2 2.0.0 start
中都没有stat_smooth
参数,因此您可能需要考虑在进一步操作之前更新ggplot2。
所以这是一个开始:
# fake some data
set.seed(1235)
weeks <- sprintf("week%d",1:9)
nsid <- 5
nwk <- 8
k <- 1
lst <- list()
slst <- list()
for (i in 1:nsid){
sid <- sprintf("R%d",i)
for (j in 1:nwk){
wk <- sprintf("week%d",j)
v <- round(i*rnorm(1,200,1) + rnorm(1,3,0.2)^j,1)
lst[[k]] <- list(sid,i,wk,j,v)
k <- k+1
}
}
df <- lapply(data.frame(do.call(rbind,lst)),unlist) # combine then flatten
names(df) <- c("s","i","w","j","v")
long_format_female <- data.frame(SampleID=df$s,variable=df$w,value=df$v)
# need a numeric version of this factor
long_format_female$nvariable <- as.numeric(long_format_female$variable)
ggplot(long_format_female, aes(x = variable, y = value, colour = SampleID)) +
geom_point() +
geom_smooth(aes(x = nvariable, y = value)) +
xlab("Time(weeks)") +
ylab("Trait") +
scale_y_log10() +
theme(panel.border = element_blank(),
axis.title=element_text(size=16),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"))
产量: