如何在具有多个组的ggplot geom_line的X轴上创建分类变量

我从

开始
>df57 <- data.frame(cellType = c("4.57","4.57","8.57","8.28.57","8.28.57"),ORR = c("PD","nonPD"),BL = rep(0,each=6),Treated = c(10,-5,8,-4,15,-2))
>df57melt <- melt(df57)
>df57melt

cellType    ORR variable  value
1   4.57    PD      BL      0
2   4.57    nonPD   BL      0
3   8.57    PD      BL      0
4   8.57    nonPD   BL      0
5   8.28.57 PD      BL      0
6   8.28.57 nonPD   BL      0
7   4.57    PD      Treated 10
8   4.57    nonPD   Treated -5
9   8.57    PD      Treated 8
10  8.57    nonPD   Treated -4
11  8.28.57 PD      Treated 15
12  8.28.57 nonPD   Treated -2                        

我想绘制一条线图,其中在x轴上显示处理(BL,已处理),在y轴上显示值(连续)。我想拥有三种具有响应变量的单元格类型(4.57、8.57和8.28.57;我希望通过线条颜色编码)(PD和nonPD;我希望通过线条样式进行编码)。

我标出了我认为应该起作用的地方:

>ggplot(data=df57melt,aes(x=variable,y = value)) + 
  geom_line(aes(linetype = ORR,color = cellType))

geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?

#so I add group info
>ggplot(data=df57melt,y = value,group = cellType)) + 
  geom_line(aes(linetype = ORR,color = cellType))

Error: geom_path: If you are using dotted or dashed lines,colour,size and linetype must be constant over the line

#but if I change from categorical x to continuous x...
>ggplot(data=df57melt,aes(x=as.numeric(variable),color = cellType))

它给了我类似我想要的东西...但不完全是

如何在具有多个组的ggplot geom_line的X轴上创建分类变量

如何获得将我的x识别为类别的信息?

linwen86 回答:如何在具有多个组的ggplot geom_line的X轴上创建分类变量

使用group的美感告诉ggplot将哪些列组合视为单独的组。 ggplot将在给定组内的所有点之间画线。

默认情况下,ggplot使用分类的x轴将每个x值视为一个单独的组。在这种情况下,数据最终按variableORRcellType分组,因此每组仅产生一个值。我们可以通过设置group美学来覆盖它。我们需要为ORRcellType的每个唯一组合使用单独的行,因此我们使用interaction(ORR,cellType)来按这两个变量的每种组合进行分组。

在下面的代码中,我还使用了gather包中的tidyr将数据帧转换为长格式,因为reshape2是一个较旧的包,不再包含在积极发展。

library(tidyverse)
theme_set(theme_classic())

df57 %>% 
  gather(key,value,BL:Treated) %>% 
  ggplot(aes(x=key,y=value)) + 
    geom_line(aes(linetype = ORR,color = cellType,group=interaction(ORR,cellType))) +
    scale_x_discrete(expand=c(0.05,0.05))

enter image description here

像这样的替代品呢?

df57 %>% 
  ggplot(aes(x=cellType,Treated,colour=ORR)) + 
    geom_hline(yintercept=0,colour="grey50",size=0.5) +
    geom_text(aes(label=sprintf("%1.1f",Treated))) +
    geom_text(data=. %>% 
                arrange(cellType) %>%
                group_by(ORR) %>% 
                slice(1) %>% 
                mutate(Treated=0.5*Treated),aes(label=gsub("nP","n-P",ORR),x=0.65),hjust=1,fontface="bold") +
    geom_segment(aes(xend=cellType,yend=BL),linetype="11",size=0.3) +
    labs(x="Cell Type",y="Treatment Effect") +
    scale_x_discrete(expand=expand_scale(add=c(1,0.25))) + 
    guides(colour=FALSE) + 
    theme_classic(base_size=15)

enter image description here

如果PDnonPD都可以为正(或均为负),则可以执行以下操作:

df57 <- data.frame(cellType = c("4.57","4.57","8.57","8.28.57","8.28.57"),ORR = c("PD","nonPD"),BL = rep(0,each=6),Treated = c(10,5,8,4,15,-2))

pd=position_dodge(0.5)
df57 %>% 
  ggplot(aes(x=cellType,Treated)),position=pd,show.legend=FALSE) +
    geom_linerange(aes(ymin=BL,ymax=Treated),size=0.3,position=pd) +
    labs(x="Cell Type",y="Treatment Effect") +
    theme_classic(base_size=15) +
    theme(legend.position="bottom",legend.margin=margin(t=-5)) +
    scale_x_discrete(expand=expand_scale(add=c(0.3,0.3))) +
    guides(colour=guide_legend(override.aes=list(linetype="solid",size=4)))

enter image description here

当然,您也可以颠倒ORRcellType的角色:

df57 %>% 
  ggplot(aes(x=ORR,colour=cellType)) + 
    geom_hline(yintercept=0,position=pd) +
    labs(x="ORR",size=4)))

enter image description here

,

我知道这并不是真正的答案,但是我认为您正在尝试将线路推到不应有的位置。我会建议:

{{1}}

enter image description here

本文链接:https://www.f2er.com/3115631.html

大家都在问