2014-09-22 123 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