ggplot2中的X轴刻度线

时间:2016-07-25 19:27:41

标签: r ggplot2

这里我有一个数据框架由3264个标准化数据点组成,包含34个样本(3264 x 34)。我使用以下脚本生成了一个ggplot:

cl21  <- geom_line(aes(y=CL021, col="CL"))
cl22  <- geom_line(aes(y=CL022, col="CL"))
cl24  <- geom_line(aes(y=CL024, col="CL"))
cl25  <- geom_line(aes(y=CL025, col="CL"))
cl27  <- geom_line(aes(y=CL027, col="CL"))
cl28  <- geom_line(aes(y=CL028, col="CL"))
cl30  <- geom_line(aes(y=CL030, col="CL"))
cl33  <- geom_line(aes(y=CL033, col="CL"))
cl35  <- geom_line(aes(y=CL035, col="CL"))
cl36  <- geom_line(aes(y=CL036, col="CL"))
cl37  <- geom_line(aes(y=CL037, col="CL"))
cl38  <- geom_line(aes(y=CL038, col="CL"))
cl39  <- geom_line(aes(y=CL039, col="CL"))
cl40  <- geom_line(aes(y=CL040, col="CL"))
ng172  <- geom_line(aes(y=NG172, col="NG"))
ng176  <- geom_line(aes(y=NG176, col="NG"))
ng178  <- geom_line(aes(y=NG178, col="NG"))
ng190  <- geom_line(aes(y=NG190, col="NG"))
ng191  <- geom_line(aes(y=NG191, col="NG"))
ng195  <- geom_line(aes(y=NG195, col="NG"))
ng218  <- geom_line(aes(y=NG218, col="NG"))
ng232  <- geom_line(aes(y=NG232, col="NG"))
ng244  <- geom_line(aes(y=NG244, col="NG"))
ng264  <- geom_line(aes(y=NG264, col="NG"))
ng285  <- geom_line(aes(y=NG285, col="NG"))
ng289  <- geom_line(aes(y=NG289, col="NG"))
ng299  <- geom_line(aes(y=NG299, col="NG"))
ng302  <- geom_line(aes(y=NG302, col="NG"))
ng306  <- geom_line(aes(y=NG306, col="NG"))
ng318  <- geom_line(aes(y=NG318, col="NG"))
ng320  <- geom_line(aes(y=NG320, col="NG"))
ng335  <- geom_line(aes(y=NG335, col="NG"))
ng338  <- geom_line(aes(y=NG338, col="NG"))
ng367  <- geom_line(aes(y=NG367, col="NG"))
gplot(normDF, aes(x=pos, y= value))+ cl21 +cl22  +cl24 +cl25 +cl27 +cl28 +cl30 +cl33 +cl35 +cl36 +cl37 +cl38 +cl39 +cl40 + ng172+ ng176+ ng178+ ng190+ ng191+ ng195+ ng218+ ng232+ ng244+ ng264+ ng285+ ng289+ ng299+ ng302+ ng306+ ng318+ ng320+ ng335+ ng338+ ng367  -> p

我尝试通过定义中断和标签来沿x轴添加自定义刻度线:

p + xlab("FGD1 exons")+ ylab('Normalized Depth')+ scale_alpha_discrete(breaks= c("327","492","675","822","945","997","1098","1212","1380","1460","1620","1798","1968","2079","2542","2741","2936","3263"), labels= c("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18"))+ theme(axis.text.x  = element_text(angle=90,vjust=0.5, size=15,hjust=1))

但是我的情节上没有出现蜱!我想知道这里是否有人可以就此提出建议!

1 个答案:

答案 0 :(得分:2)

我认为您需要使用scale_x_continuous,因为您的值是连续的。

以下是一个示例,包括一个MWE,展示如何使用melt包中的reshape2来简化您的流程以简化操作:

library(reshape2)
library(ggplot2)

df <-
  data.frame(
    x = 1:100
    , ya = 1:100
    , yb = seq(1,50, length.out = 100)
    , yc = seq(1,25, length.out = 100)
    , yd = seq(1,10, length.out = 100)
  )

melted <- 
  melt(df, id.vars ="x")

melted$set <-
  ifelse(melted$variable %in% c("ya","yb")
         , "set A"
         , "set B")

head(melted)

ggplot(melted
       , aes(x, value
             , group = variable
             , col = set)) +
  geom_line() +
  scale_x_continuous(breaks = c(1, 20, 55, 80)
                     , labels = c(1,2,3,4))

enter image description here

我错过了列名包含原始集中的颜色信息,您甚至可以使用substr自动执行颜色:

df <-
  data.frame(
    x = 1:100
    , CLa = 1:100
    , CLb = seq(1,50, length.out = 100)
    , NGc = seq(1,25, length.out = 100)
    , NGd = seq(1,10, length.out = 100)
  )

melted <- 
  melt(df, id.vars ="x")

melted$set <-
  substr(melted$variable, 1, 2)

head(melted)

ggplot(melted
       , aes(x, value
             , group = variable
             , col = set)) +
  geom_line() +
  scale_x_continuous(breaks = c(1, 20, 55, 80)
                     , labels = c(1,2,3,4))