R:列标题为ylim,行名称为Rlim中的xlim

时间:2014-09-22 22:51:40

标签: r plot visualization analysis

我的数据如下:

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,Date列为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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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 :(得分:2)

您也可以使用ggplot:

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

enter image description here