2014-09-22 114 views
1

XLIM我有我的數據如下:R:列標題ylim和行的名稱用作R

Date   Medicine1  Medicine2  Medicine3  Medicine4 
2014-01-01  0.986   0.897   0.765   0.566 
2014-02-01  0.989   0.888   0.778   0.587 
2014-03-01  0.991   0.867   0.788   0.565 
2014-04-01  0.982   0.883   0.771   0.532 
2014-05-01  0.995   0.873   0.799   0.588 

我使用matplot()函數來繪製上述數據。以下是我的代碼:

med_temp = as.matrix(Medicine[,2:(length(Medicine[1,]))]) 
matplot(med_temp, type = c("b"), pch = 1, col = 2:(length(Medicine[1,]))) 

以下是劇情:

Med_plot

我想列標題爲ylim和日期列XLIM。我試過使用

ylim = as.vector("Medicine1","Medicine2","Medicine3","Medicine4") and 
ylim = c("Medicine1","Medicine2","Medicine3","Medicine4") 

這兩個都給我錯誤。我該如何獲得我想要的格式?有沒有辦法做到這一點?

在此先感謝您的幫助

真實數據:

Month  Retention1  Diff 
Month1  100.0   0.0 
Month2  95.5   -4.5 
Month3  90.6   -4.9 
Month4  85.9   -4.7 
Month5  82.0   -3.9 
+0

你的意思'xlab'和'ylab'? – 2014-09-22 22:54:36

回答

2

我想你用錯誤的參數打在這裏。 ylim=xlim=用於控制沿軸的值範圍,它們與標記軸無關。因此,也許你想要的東西像

med_temp = as.matrix(Medicine[,2:(length(Medicine[1,]))]) 
matplot(med_temp, type = c("b"), pch = 1, col = 2:(length(Medicine[1,])), xaxt="n") 

axis(1, at=1:nrow(med_temp), labels=Medicine$Date) 
axis(4, at=med_temp[nrow(med_temp), ],labels=colnames(med_temp), 4) 

enter image description here

+0

謝謝MrFlick!這是我想達到的。我能夠得到左邊的數字,而不是右邊顯示的線條的名稱。謝謝您的幫助! – EsBee 2014-09-23 15:26:14

+0

這是我在這裏發表的測試數據。當我應用到我的實際數據時,我收到一條警告消息:「xant」不是圖形參數(當我執行第一行和第二行代碼時 - 這裏是med_temp和matplot(...)的等價物)。你碰巧知道我爲什麼這樣做?謝謝! – EsBee 2014-09-23 20:59:16

+0

該消息是針對「xant」的?這可能應該是「xaxt」,對吧? – MrFlick 2014-09-23 21:02:49

2

我想更好地在這裏與軸(axes=F),並使用text只是註釋你的情節來繪製。您還可以使用legend添加適當的圖例。

enter image description here

cols <- seq_len(ncol(med_temp)) 
matplot(med_temp, type = c("b"), pch = 1, col=cols,axes=FALSE) 
text(1.5,med_temp[1,],labels = colnames(med_temp),col=cols,srt=10) 
axis(1,at=1:5,labels=Medicine$Date) 
2

您還可以使用ggplot此:

mm = melt(ddf) 
ggplot(mm, aes(x=Date, y=value, group=variable, color=variable))+geom_line()+geom_point() 

enter image description here

+0

不要忘記導入'reshape2'和'ggplot2'。 – oba2311 2018-02-26 17:23:18