2015-04-15 40 views
0

我有一個csv文件與多個列。我只考慮其中的兩個,'時間'和'RiseOrFall'。兩者都是因子數據類型。樣本數據是這樣的:如何繪製因子數據類型的折線圖?

Time RiseOrFallRiseOrFall 
12 32 
34 0 
56 0 
78 25 
90 29 
123 0 
567 50 

我想R中跌倒每次創建一個折線圖「RiseOrFall」打0時,它不是上升0.1(X軸「時間」和「RiseOrFall」上Y軸)

我嘗試:

countFile <- read.csv(file = "counts.csv", nrows = 1000)[, c ('TIME','RiseOrFall')] 

ggplot(data=countFile, aes(x=RiseOrFall)) + geom_line() 

如何在此R(可以實現可能使用GGPLOT2或任何東西)??

我的預期輸出圖表如下(在採樣數據是所述記錄很少..實際數據是非常大,具有更大的「RiseOrFall」值(Y軸): enter image description here

回答

0

必須有一包這種類型的數據... 你可以試試這個:

require(ggplot2) 
require(zoo) #na.locf 

#dummy data 
countFile <- read.table(text="Time RiseOrFall 
12 32 
34 0 
56 0 
78 25 
90 29 
123 0 
567 50",header=TRUE) 

#assign values for every x value 
filler <- data.frame(Time=min(countFile$Time):max(countFile$Time)) 
df <- merge(filler, countFile,all.x=TRUE) 
df$RiseOrFall <- na.locf(df$RiseOrFall) 

#plot 
ggplot(df,aes(x=Time,y=RiseOrFall)) + 
    geom_line() 

enter image description here

+0

看起來不錯..將嘗試它在我的數據現在..謝謝你這麼多:) – LearneR

+0

而且還可以請你扔爲什麼我們使用na.locf一些光()在這裏?? NA值是否會在這裏產生問題? – LearneR

+0

在您的示例圖中,幾乎x軸上的每個點都有數據,因此我創建了填充數據框以與數據合併,並用最後一個已知數據點填充NA。所以當它爲零時,它會一直保持到零,直到下一個非零點。 – zx8754

1

試試這個

0123。

我已經將x軸(時間)和y軸(RiseOrFall)滴答分別設置爲6個單位和200個單位。

ggplot(data=countFile, aes(x=Time,y = TRPM)) + geom_line(colour="brown") + scale_x_continuous(expand = c(0, 0),breaks = round(seq(min(countFile$Time), max(countFile$Time), by = 6),1)) + scale_y_continuous(expand = c(0, 0),breaks = round(seq(min(countFile$TRPM), max(countFile$TRPM), by = 200),1))+ xlab("Time") + ylab("RiseOrFall")+ theme(panel.background = element_blank(),axis.line = element_line(colour = "black")) 

enter image description here