2016-09-20 38 views
0

我使用「閃亮」,使繪出一些數據的應用程序,主要情節有8米不同的賽道,有名字是 -有條件的軌道

102G長,102G- 103G長,103G短,102G熱,103G命中,102G命中,103G命中。

長/短的軌道應該是可選的(有一個單選按鈕隱藏)

情節是一個小網格,所以我只是從data.frame刪除所有數據與用戶決定隱藏的軌道相關(當然,我保留這些數據,我有2個副本並且只更改其中一個),並且它會自動刪除軌道,問題是如果我刪除了兩個或更長的軌道短的軌道,我得到這個錯誤 -

美學必須是長度1或相同的數據(1):x,y,xend,yend,size,c olour

我認爲問題是,當我繪製我「設計」了不同的軌道像這個 -

ggplot(seg) + facet_grid(id~chr) + 
    geom_rect(data=subset(seg,grepl("heat$",id)), 
      mapping=aes(xmin=start,ymin=0,xmax=end,ymax=log2,fill=xlog2)) + 
    geom_segment(data=subset(seg,!grepl("heat$",seg$id) & !grepl("short$",seg$id) & !grepl("long$",seg$id)), 
       mapping=aes(x=start,xend=end,y=log2,yend=log2,log2>0,color=log2>0)) + 
    geom_segment(data=subset(seg,grepl("short$",id)), 
       mapping=aes(x=start,xend=end,y=log2,yend=log2,size=3,color=log2>0)) + 
    geom_segment(data=subset(seg,grepl("long$",id)), 
       mapping=aes(x=start,xend=end,y=log2,yend=log2,size=3,color=log2>0)) + 

,如果我刪除這兩個短例如,那麼在沒有數據這條線 -

geom_segment(data=subset(seg,grepl("short$",id)), 
       mapping=aes(x=start,xend=end,y=log2,yend=log2,size=3,color=log2>0)) + 

有沒有把此行的狀態,所以如果有一些數據指短期將只執行的方法嗎?或者用不同的方法來做到這一點?

在此先感謝!

+0

你得到一個錯誤? ggplot可能會執行正常,如果你傳遞一個空的data.frame – Nate

+0

就像我寫的,我確實得到一個錯誤 - 美學必須是長度1或相同的數據(1):x,y,xend,yend,size,顏色 –

回答

0

好了,答案很簡單,我覺得愚蠢,我只是創建了一個圖形對象,並添加東西的條件的對象,像這個 -

temp_plot <- ggplot(seg) + facet_grid(id~chr) + 
    geom_rect(data=subset(seg,grepl("heat$",id)), 
      mapping=aes(xmin=start,ymin=0,xmax=end,ymax=log2,fill=xlog2)) + 
    geom_segment(data=subset(seg,!grepl("heat$",seg$id) & !grepl("short$",seg$id) & !grepl("long$",seg$id)), 
       mapping=aes(x=start,xend=end,y=log2,yend=log2,log2>0,color=log2>0)) 
if(!("102G-short" %in% input$hide_tracks & "103G-short" %in% input$hide_tracks)) { 
    temp_plot <- temp_plot + geom_segment(data=subset(seg,grepl("short$",id)), 
             mapping=aes(x=start,xend=end,y=log2,yend=log2,size=3,color=log2>0)) 
} 

if(!("102G-long" %in% input$hide_tracks & "103G-long" %in% input$hide_tracks)) { 
    temp_plot <- temp_plot + geom_segment(data=subset(seg,grepl("long$",id)), 
             mapping=aes(x=start,xend=end,y=log2,yend=log2,size=3,color=log2>0)) 
} 
temp_plot <- temp_plot + scale_fill_gradient2(limits=c(-3,3),low="darkblue",mid="white",high="darkred",na.value="white",midpoint=0) + 
    scale_color_manual(values=c("blue","red")) + 
    theme(panel.background=element_rect(fill="white")) + 
    theme(legend.position = "none") + 
    theme(strip.text.x = element_text(size = 12)) + 
    theme(strip.text.y = element_text(size = 12)) + 
    ylab("") + 
    ylim(c(-2,2)) + 
    coord_cartesian(xlim = ranges$x, ylim = ranges$y) 
temp_plot