2016-07-06 63 views
0

數據: 我有2列問題與R中圖解

Col1 20160628 20160629 20160630 20160701 20160702 
Col2 500  600  700  800  900 

一個數據幀我需要Col1中在x軸和col2上爲y軸

在我使用情節,使圖形,在X軸上顯示20160680等數據點,這在圖表上很具誤導性。

我急需在圖上等間隔的這5個點。

+0

請問你COL1轉換爲因素幫助實現你想要什麼? –

+0

非常感謝,這回答了我的問題。 –

+0

@ZheyuanLi我雖然可以標記2個答案!!!!,你的是第一個爲我工作的人。 –

回答

1

你可以嘗試一個很明顯的一點是:

x <- c(20160628,20160629,20160630,20160701,20160702) 
y <- 5:9 * 100 

x <- as.character(as.Date(as.character(x),format = "%Y%m%d")) ## convert to date 
# [1] "2016-06-28" "2016-06-29" "2016-06-30" "2016-07-01" "2016-07-02" 
plot(y, xaxt = "n", xlab = "Date") ## do not draw x-axis 
axis(1, at = 1:length(y), labels = x) ## add x-axis, using date as axis labels 

enter image description here

+0

似乎正確的路要走。如果您正在繪製日期數據,請不要假定它們的間距相等,並使用實際日期。 – thelatemail

1

既然你把它作爲一個數據幀,可以使用GGPLOT2用於繪圖。

x <- c(20160628,20160629,20160630,20160701,20160702) 
y <- 5:9 * 100 
x <- as.character(as.Date(as.character(x),format = "%Y%m%d")) 
data <- data.frame(x,y) 

library(ggplot2) 
ggplot(data,aes(x=x,y=y)) + 
    geom_point() + 
    labs(x="Date",y="Value") 

enter image description here