改变起源barplot R ggplot2

时间:2014-04-03 15:50:42

标签: r plot ggplot2

我想在图表的原点留一个空格(删除左角(350000,17)坐标)。

我的数据集(dat):

group  x       y
Sens 17    4059
Sens 18    3289
Sens 19    3785
Sens 20    5241
Sens 21   17179
Sens 22   11004
Sens 23   23624
Sens 24   61544
Sens 25   87324
Sens 26   85610
Sens 27   53652
Sens 28   26243
Sens 29   10840
Sens 30    3277
Antisens 17   -7145
Antisens 18   -8334
Antisens 19  -10020
Antisens 20  -14247
Antisens 21  -33285
Antisens 22  -32575
Antisens 23  -79349
Antisens 24 -217690
Antisens 25 -338036
Antisens 26 -291708
Antisens 27 -172231
Antisens 28  -86063
Antisens 29  -29685
Antisens 30   -7914

我该怎么做?enter image description here

我想获得这样的东西: enter image description here

使用的命令行:

d <- ggplot(dat, aes(x=x, y=y, fill=group)) + 
       geom_bar(stat="identity", position="identity") + 
       scale_y_continuous(limits=c(-350000, 90000),breaks=c(90000,0,-90000,-180000,-270000,-350000),labels=abs(c(90000,0,-90000,-180000,-270000,-350000))) + 
       scale_x_continuous(breaks=seq(17,30,by=1), labels=c(17,"",19,"",21,"",23,"",25,"",27,"",29,"")) + 
       xlab("small RNA length [nt]") + ylab("normalized small RNA counts") + 
       scale_fill_manual(values = c("red", "blue"))
d <- d + theme_bw() + 
           theme(axis.line = element_line(colour = "black"), panel.grid.major = element_blank(), panel.grid.minor = element_blank()) + 
           theme(panel.border=element_blank())
d

1 个答案:

答案 0 :(得分:0)

根据我的评论,geom_segment可用于重新创建轴。我已经调整了轴的极限以允许额外的空间。我不知道有另一种方法可以继续,虽然我确信会有。我试图评论对您的代码所做的补充。

# Your data [you can use dput(dat) to get structure below]

dat <- structure(list(group = c("Sens", "Sens", "Sens", "Sens", "Sens", 
      "Sens", "Sens", "Sens", "Sens", "Sens", "Sens", "Sens", "Sens", 
      "Sens", "Antisens", "Antisens", "Antisens", "Antisens", "Antisens", 
      "Antisens", "Antisens", "Antisens", "Antisens", "Antisens", "Antisens", 
      "Antisens", "Antisens", "Antisens"), x = c(17L, 18L, 19L, 20L, 
      21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 17L, 18L, 19L, 
      20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L), y = c(4059L, 
      3289L, 3785L, 5241L, 17179L, 11004L, 23624L, 61544L, 87324L, 
      85610L, 53652L, 26243L, 10840L, 3277L, -7145L, -8334L, -10020L, 
     -14247L, -33285L, -32575L, -79349L, -217690L, -338036L, -291708L, 
     -172231L, -86063L, -29685L, -7914L)), .Names = c("group", "x", 
     "y"), class = "data.frame", row.names = c(NA, -28L))


# ---------------------------------------------------------------------------
library(ggplot2)

# Initial plot
d <- ggplot(dat, aes(x=x, y=y, fill=group)) + 
         geom_bar(stat="identity", position="identity") + 
         scale_y_continuous(
                 limits=c(-380000, 90000), # lowered the min limit slightly
                 breaks=c(90000,0,-90000,-180000,-270000,-350000),
                 labels=abs ,  # note the use of abs
                 expand=c(0,0)) + # use expand so axis start exactly at limits
         scale_x_continuous(
                 limits=c(16,30),  # added x-axis limits (min is < your min break)
                 breaks=seq(18,30,by=2), 
                 labels=seq(18,30,by=2) , 
                 expand=c(0,0)) + 
         xlab("small RNA length [nt]") + 
         ylab("normalized small RNA counts") + 
         scale_fill_manual(values = c("red", "blue"))

d <- d + theme_bw() + 
         theme(axis.line = element_blank(), # remove both axis lines
               panel.grid.major = element_blank(), 
               panel.grid.minor = element_blank(),
               panel.border=element_blank())

# Add in segments for the axis - allow a gap at the corner
d + 
geom_segment(x=17,xend=30,y=-380000,yend=-380000) + # x-axis
geom_segment(x=16,xend=16,y=-350000,yend=90000) # y-axis

enter image description here