【问题标题】:R matplot functionR matplot 函数
【发布时间】:2014-01-15 23:52:18
【问题描述】:

由于我是 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 +curves) 我的主要目标是在一张图像中的所有日期的第一列(药物)的两种颜色之后可视化所有个体的分布。

非常感谢您的建议

【问题讨论】:

    标签: r plot


    【解决方案1】:

    这是你的想法吗?

    代码基于 ->this questiondf) 而不是 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")
    

    【讨论】:

    • @user3064106 我想他刚刚做到了:-)
    • 谢谢,但我仍然有一条错误消息处理:找不到对象“药物”。 color=factor(drug)),geom="line", position="dodge")+
    • 也许我应该在 df 中将药物精确为 colname 吗?像 colname(def$drug)
    • 对不起,我应该更清楚。我将您问题顶部的数据集读入数据框df,最终包含以下列:drugNumberIndividuMarApril 等。我认为这相当于您的@987654335 @.
    猜你喜欢
    • 1970-01-01
    • 2011-07-09
    • 1970-01-01
    • 2015-12-16
    • 1970-01-01
    • 1970-01-01
    • 2020-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多