2017-07-31 33 views
0

我有以下的(樣品)data.frame如何將data.frame列中的值更改爲數字?

x <- data.frame(gene = 1:3, Sample1 = 5:7, Sample2 = 4:6, Sample3 = 6:8) 

我想改變列名,然後使用數字新標題爲x軸的值,我的陰謀

colnames(x) <- c("Gene", "HeLa_0.2", "HeLa_2.0", "HeLa_5.0") 

x_gather <- x %>% 
    gather(key=treatment, value=values, -c(Gene)) %>% 
    tidyr::separate(treatment, into=c("Cell_line", "treatment"),sep="_") 

ggplot()+ 
    geom_line(x_gather, mapping=aes(treatment, y=values, group=Gene)) 

但我希望這些數字在x軸上像this那樣間隔,而不是像this這樣的軸(我只有在將數據複製到excel時將其格式化,然後將它們格式化爲數字,然後再次將其加載到R中。 ) 如何解決這個問題的任何建議?

謝謝! :)

+1

您需要使用'轉換= TRUE'在'separate'轉換爲新類型。現在'治療'是一個角色。 – aosmith

+0

非常感謝!正如你所理解的那樣,我對R很陌生:) – Beate

回答

0

所有您需要做的是使treatment變量數字。例如:

x_gather <- x %>% 
    gather(key=treatment, value=values, -c(Gene)) %>% 
    tidyr::separate(treatment, into=c("Cell_line", "treatment"),sep="_") %>% 
    mutate(treatment = as.numeric(treatment)) 

ggplot()+ 
    geom_line(x_gather, mapping=aes(treatment, y=values, group=Gene)) 

enter image description here

+0

我必須使用%>%mutate(as.numeric(處理),但它可以工作 :) – Beate

相關問題