R matplot功能

时间:2014-01-14 17:19:55

标签: r plot

由于我是初学者R,我允许自己向R用户提出一个小问题。 我想用图形(点,线,曲线)表示两个治疗组和未用药物(0,1)治疗的人组的体重值十次(月)。

drug    NumberIndividu Mar Apr May June July August September November October December
1   9    25.92    24.6  31.85   38.50  53.70   53.05    65.65    71.45      69.10      67.20
1   10   28.10    26.6  32.00   38.35  53.60   53.25    65.35    65.95      67.80      65.95
1   11   29.10    28.8  30.80   38.10  52.25   47.30    62.20    68.05      66.20      67.55
1   13   27.16    25.0  27.15   34.85  47.30   43.85    54.65    62.25      60.85      58.05
0   5    25.89    25.2  26.50   27.45  37.05   38.95    43.30    50.60      48.20      50.10
0   6    28.19    27.6  28.05   28.60  36.15   37.20    40.40    47.80      45.25      44.85
0   7    28.06    27.2  27.45   28.85  39.20   41.80    51.40    57.10      54.55      55.30
0   8    22.39    21.2  30.10   30.90  42.95   46.30    48.15    54.85      53.35      49.90

我试过了:

w= read.csv (file="/file-weight.csv", header=TRUE, sep=",")
w<-data.frame(w)
rownames(w[1:8,])
rownames(w)<-(c(w[,1]))

cols <- character(nrow(w))
cols[rownames(w) %in% c(rownames(w[1:4,]))]<-"blue"
cols[rownames(w) %in% c(rownames(w[5:8,]))]<-"red"
pairs(w,col=cols)

我的问题是如何配置matplot函数以获得一个图形视图(点或曲线或hist +曲线) 我的主要目标是在一张图像中为所有日期显示第一列(药物)的两种颜色的所有个体分布。

非常感谢你的建议

1 个答案:

答案 0 :(得分:5)

这是你的想法吗?

代码基于 - &gt; this question&lt; - 的答案,只使用您的数据集(df)而不是iris。所以在那个回复中,替换:

x <- with(iris, data.table(id=1:nrow(iris), group=Species, Sepal.Length, Sepal.Width,Petal.Length, Petal.Width))

使用:

xx <- with(df, data.table(id=1:nrow(df), group=drug, df[3:12]))

如果你想要的只是密度图/直方图,那就更容易了(见下文)。这些是互补的,因为它们表明对照组和测试组的重量都在增加,在测试组中速度更快。你不会从散点图矩阵中选择它。此外,有人认为对照组的体重变异性更大,并随着时间的推移而增长。

library(ggplot2)
library(reshape2)    # for melt(...)
# convert df into a form suitable to use with ggplot
gg <- melt(df,id=1:2, variable.name="Month", value.name="Weight")
# density plots
ggplot(gg) + 
  stat_density(aes(x=Weight, y=..scaled.., color=factor(drug)),geom="line", position="dodge")+
  facet_grid(Month~.)+
  scale_color_discrete("Drug")
# histograms
ggplot(gg) + 
  geom_histogram(aes(x=Weight, fill=factor(drug)), position="dodge")+
  facet_grid(Month~.)+
  scale_fill_discrete("Drug")

相关问题