ggplot2仅绘制分组数据帧的最后一个变量

时间:2016-10-11 21:49:42

标签: r plot ggplot2 drc

我正在尝试使用group=model绘制3个模型,但正在绘制仅绘制data.frame中的最后一个模型。我已经在网上进行了大量的搜索,但却无法弄清问题是什么。

data.frame有3列:剂量(x轴),p1(y轴)和模型(mod1,mod2,mod3)

library(ggplot2)

ggplot(df, aes(x=dose,y=p1, group=model))+
   geom_line(aes(color=model))+
   geom_step(aes(x=dose,y=p1,group=model, col=model))+
   coord_trans(x = 'log10', limx = c(0.01,200), limy=c(0.01,0.05)) +

#Assigning colors to mod1,mod2,mod3
   values = c("mod1" = "darkgreen", "mod2" = "blue", "mod3" = "red"))

#Log Aesthetics, cut for brevity of code,     Help with log ticks [enter image description here]

- 这不是mod1和mod2在帧外绘制的问题。

如果它有任何相关性:我在下面包含的数据(我在上面创建了图形)仅包含3个模型下限的几个点 - 实际数据集包含8个型号,每个有100个点。

df <- structure(list(dose = c(0.5, 0.565608284362011, 0.639825462677876, 
  0.723781164472726, 0.818753245381915, 0.5, 0.565608284362011, 
  0.639825462677876, 0.723781164472726, 0.818753245381915, 0.926187236872587, 
  0.5, 0.565608284362011, 0.639825462677876, 0.723781164472726, 
  0.818753245381915, 0.926187236872587, 1.04771834809099, 1.18519635471669, 
  1.34071375364684, 1.51663761204148, 1.71564559549136), p1 = c(0.0103075076812739, 
  0.0116952370538794, 0.0132672958208565, 0.0150474513444454, 0.017062331184752, 
  0.0103075076812739, 0.0116952370538794, 0.0132672958208565, 0.0150474513444454, 
  0.017062331184752, 0.0193417088954045, 0.0103075076812739, 0.0116952370538794, 
  0.0132672958208565, 0.0150474513444454, 0.017062331184752, 0.0193417088954045, 
  0.0219188012260592, 0.0248305714535104, 0.0281180313564242, 0.0318265316115288, 
  0.036006027058739), model = c("mod1", "mod1", "mod1", "mod1", 
  "mod1", "mod2", "mod2", "mod2", "mod2", "mod2", "mod2", "mod3", 
  "mod3", "mod3", "mod3", "mod3", "mod3", "mod3", "mod3", "mod3", 
  "mod3", "mod3")), .Names = c("dose", "p1", "model"), row.names = c(1L, 
  2L, 3L, 4L, 5L, 101L, 102L, 103L, 104L, 105L, 106L, 201L, 202L, 
  203L, 204L, 205L, 206L, 207L, 208L, 209L, 210L, 211L), class = "data.frame")

1 个答案:

答案 0 :(得分:1)

据我所知,问题在于这些点是相互叠加的,这意味着你只能看到更完整的线(mod3)而其他的线在下面。

白色的一些基本数据争论,我们可以摆脱重复,并得到这样的东西

library(ggplot2)
require(tidyr)
require(dplyr)

 df1 <- df %>% spread(model,p1) %>% 
  mutate(mod3=ifelse(is.na(mod2*mod3)==FALSE,NA,mod3),mod2=ifelse(is.na(mod2*mod1)==FALSE,NA,mod2)) 
%>% gather(model,p1,mod1:mod3) %>% filter(is.na(p1)==FALSE) %>% arrange(p1) 

df1 <- rbind(df1,df1[which(df1$p1==max(df1[df1$model=="mod1",]$p1)),])
df1 <- rbind(df1,df1[which(df1$p1==min(df1[df1$model=="mod3",]$p1)),])
df1[nrow(df1),2] <- "mod2"
df1[nrow(df1)-1,2] <- "mod2"


ggplot(df1, aes(x=dose,y=p1, group=model))+
geom_line(aes(color=model))+
geom_step(aes(x=dose,y=p1,group=model, col=model))+
coord_trans(x = 'log10', limx = c(0.01,200), limy=c(0.01,0.05))

输出以下图像:

enter image description here