2016-05-17 37 views
1

假设我有这样的数据。如何在R中创建如下所示的图形?

set.seed(23) 
n <- 5 
data <- data.frame(group1=rnorm(n, 100, 5), group2=rnorm(n, 100, 5),  
     group1.se=runif(n, 0.5, 3), group2.se=runif(n, 0.5, 3)) 
data 
##  group1 group2 group1.se group2.se 
## 1 100.96606 105.53745 1.9659945 2.5511267 
## 2 97.82659 98.60957 1.1868524 2.2123432 
## 3 104.56634 105.09603 0.8691424 2.7084732 
## 4 108.96694 100.22719 2.5035258 0.7798019 
## 5 104.98303 107.87890 1.4660246 2.4470850 

我想创建一个显示每个时间跨越不同点两个实验组的平均体重的图表,为每个平均重量标准误差酒吧。我想要显示每个组的方法为点连线的点,x轴的“天数”和y轴的“权重”。最终产品应该看起来像这样。

+0

[你尝试过这么远吗?(http://whathaveyoutried.com) 请[编辑]你的问题以显示你是有问题的代码 一个[MCVE]那么我们可以尝试帮助 解决具体问题。你还应该阅读[问]。 –

回答

1

你可以做这样的事情。

# Simulate data 
set.seed(23) 
n <- 5 
group1 <- rnorm(n, 100, 5) 
group2 <- rnorm(n, 100, 5) 
group1.se <- runif(n, 0.5, 3) 
group2.se <- runif(n, 0.5, 3) 

# Make line plots 
x <- c(1:n) 
plot(group1 ~ x, ylim=c(90, 115), type="b", lwd=2, col="red", ylab="weights", xlab="days") 
lines(group2 ~ x, type="b", lwd=2, pch=2, col="blue") 

# Add standard error bars 
arrows(x0=x, y0=group1+0.5, y1=group1+group1.se, length=0.05, angle=90, col="lightpink") 
arrows(x0=x, y0=group1-0.5, y1=group1-group1.se, length=0.05, angle=90, col="lightpink") 
arrows(x0=x, y0=group2+0.5, y1=group2+group2.se, length=0.05, angle=90, col="lightblue") 
arrows(x0=x, y0=group2-0.5, y1=group2-group2.se, length=0.05, angle=90, col="lightblue") 

# Add legend 
legend("bottomright", legend=c("group1", "group2"), col=c("red", "blue"), lty=1) 

1

你可以做基础R图形任何东西,如果你知道这种API。

plot

## define data 
x <- 0:14; 
dat <- list(
    Sentinel=list(
     mean=c(-0.95,-0.15,-0.40,-0.10,-1.30,-0.95,-1.10,-0.60,-1.10,-1.20, 0.30,-0.50,-2.60, 0.10,-0.95), 
     sd =c(0.55, 0.55, 1.40, 0.25, 0.60, 1.20, 0.40, 1.00, 0.80, 0.15, 0.25, 0.22, 0.52, 0.30, 1.50), 
     pch=22L, pt.cex=1.7, pt.lwd=2.5, pt.bg='white' 
    ), 
    Infected=list(
     mean=c(-1.35, 0.50,-0.26,-0.05,-0.40,-0.94, 0.55, 0.55,-0.48, 0.23,-1.30,-0.23,-1.05, 0.40, 0.20), 
     sd =c(0.70, 0.15, 0.70, 0.27, 0.87, 0.50, 0.80, 0.70, 0.50, 0.28, 0.40, 0.45, 1.02, 0.45, 0.35), 
     pch=21L, pt.cex=1.4, pt.lwd=2.5, pt.bg='#5555BB' 
    ) 
); 

## plot parameters 
xoff <- 1; 
xlim <- c(0-xoff,14+xoff); 
ylim <- c(-4,2); 
xticks <- seq(x[1L],x[length(x)],2); 
yticks <- -4:2; 
datline.lwd <- 2; 
err.spread <- 0.12; 
err.lwd <- 2.2; 
err.col <- '#777777'; 

## helper function 
errorbar <- function(x,mean,sd) { 
    segments(x,mean-sd,y1=mean+sd,lwd=err.lwd,col=err.col); 
    segments(x-err.spread,mean-sd,x1=x+err.spread,lwd=err.lwd,col=err.col); 
    segments(x-err.spread,mean+sd,x1=x+err.spread,lwd=err.lwd,col=err.col); 
}; ## end errorbar() 

## plot 
plot(NA,xlim=xlim,ylim=ylim,xaxs='i',yaxs='i',axes=F,ann=F); 
for (prop in names(dat)) { 
    d <- dat[[prop]]; 
    lines(x,d$mean,lwd=datline.lwd); 
    errorbar(x,d$mean,d$sd); 
    points(x,d$mean,pch=d$pch,cex=d$pt.cex,lwd=d$pt.lwd,bg=d$pt.bg); 
}; ## end for 
axis(1L,xticks,cex.axis=1.3,lwd=3,col='#777777'); 
mtext('Days post Infection-Aerosol Group',1L,2.3,cex=1.47); 
axis(2L,yticks,cex.axis=1.3,lwd=3,col='#777777',las=1L); 
mtext('Change in Temperature (Fahrenheit)',2L,2.3,cex=1.47); 
rect(xlim[1L],ylim[1L],xlim[2L],ylim[2L],lwd=4,border='#777777',xpd=NA); 
lp <- c('pch','pt.cex','pt.lwd','pt.bg'); 
do.call(legend,c(list(11.7,1.7,names(dat),bty='n',adj=0.1,lwd=datline.lwd),setNames(nm=lp,lapply(lp,function(p) sapply(dat,`[[`,p))))); 

你的新的随机测试数据涵盖范围不同,所以我们要调整一些东西,使绘图代码工作。

plot2

## OP's new randomized input 
set.seed(23L); 
N <- 5L; 
data <- data.frame(group1=rnorm(N,100,5),group2=rnorm(N,100,5),group1.se=runif(N,0.5,3),group2.se=runif(N,0.5,3)); 

## transfer to dat 
x <- seq_len(nrow(data))-1L; 
dat <- list(
    group1=list(
     mean=data$group1, 
     sd =data$group1.se, 
     pch=22L, pt.cex=1.7, pt.lwd=2.5, pt.bg='white' 
    ), 
    group2=list(
     mean=data$group2, 
     sd =data$group2.se, 
     pch=21L, pt.cex=1.4, pt.lwd=2.5, pt.bg='#5555BB' 
    ) 
); 

## plot parameters 
xoff <- 1; 
xlim <- c(x[1L]-xoff,x[length(x)]+xoff); 
ylim <- c(95,113); 
xticks <- seq(x[1L],x[length(x)]); 
yticks <- seq(ylim[1L],ylim[2L]); 
datline.lwd <- 2; 
err.spread <- 0.12; 
err.lwd <- 2.2; 
err.col <- '#777777'; 

## helper function 
errorbar <- function(x,mean,sd) { 
    segments(x,mean-sd,y1=mean+sd,lwd=err.lwd,col=err.col); 
    segments(x-err.spread,mean-sd,x1=x+err.spread,lwd=err.lwd,col=err.col); 
    segments(x-err.spread,mean+sd,x1=x+err.spread,lwd=err.lwd,col=err.col); 
}; ## end errorbar() 

## plot 
plot(NA,xlim=xlim,ylim=ylim,xaxs='i',yaxs='i',axes=F,ann=F); 
for (prop in names(dat)) { 
    d <- dat[[prop]]; 
    lines(x,d$mean,lwd=datline.lwd); 
    errorbar(x,d$mean,d$sd); 
    points(x,d$mean,pch=d$pch,cex=d$pt.cex,lwd=d$pt.lwd,bg=d$pt.bg); 
}; ## end for 
axis(1L,xticks,cex.axis=1.3,lwd=3,col='#777777'); 
mtext('Days',1L,2.3,cex=1.47); 
axis(2L,yticks,cex.axis=1.3,lwd=3,col='#777777',las=1L); 
mtext('Weight',2L,2.7,cex=1.47); 
rect(xlim[1L],ylim[1L],xlim[2L],ylim[2L],lwd=4,border='#777777',xpd=NA); 
lp <- c('pch','pt.cex','pt.lwd','pt.bg'); 
do.call(legend,c(list(3.7,99,names(dat),bty='n',adj=0.1,lwd=datline.lwd),setNames(nm=lp,lapply(lp,function(p) sapply(dat,`[[`,p))))); 
1

这里是一个ggplot2溶液。为了提供一个可重复的例子,我使用数据集BodyWeight{nlme},以及不同饮食的大鼠体重随时间变化的数据。

library(data.table) 
library(ggplot2) 
library(ggthemes) 
library(nlme) 


data(BodyWeight) # get the data 
setDT(BodyWeight) # convert into data.table 


# summarize your data into the information you want, getting stats by each time and Diet group 
    df <- BodyWeight[, .(mean= mean(weight), 
         SE_upper = mean(weight) + sd(weight)/sqrt(length(weight)), 
         SE_lower = mean(weight) - sd(weight)/sqrt(length(weight))), 
        by=.(Time,Diet)] 


# Plot 
    ggplot(data=df, aes(x=Time, y=mean, group= Diet)) + 
    geom_errorbar(aes(ymin=SE_lower, ymax=SE_upper), color="gray40") + 
    geom_line(color="gray10") + 
    geom_point(aes(shape=Diet, color=Diet), size=3) + 
    theme_bw() + 
    theme(panel.grid = element_blank()) + 
    labs(x = "Days of Diet", y = "Weight") 

如果你想调整的情节,ggplot2是用大量的例子在那里非常灵活,well documented

enter image description here