如何在R中创建下面这样的图?
假设我有这样的数据如何在R中创建下面这样的图?,r,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
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
# 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)
set.seed(23)
你可以这样做
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
# 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)
#模拟数据
种子(23)
你可以这样做
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
# 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)
#模拟数据
种子(23)
n如果你熟悉API,你可以用base R图形做任何事情
定义数据
如果你熟悉API,你可以用BaseR图形做任何事情
定义数据
这是一个ggplot2
解决方案。为了提供一个可重复的例子,我使用了数据集体重{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")
库(data.table)
图书馆(GG2)
图书馆(主题)
图书馆(nlme)
数据(体重)#获取数据
setDT(体重)#转换为data.table
#将您的数据汇总到您想要的信息中,按时间和饮食组获取统计数据
df这是一个ggplot2
解决方案。为了提供一个可重复的例子,我使用了数据集体重{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")
库(data.table)
图书馆(GG2)
图书馆(主题)
图书馆(nlme)
数据(体重)#获取数据
setDT(体重)#转换为data.table
#将您的数据汇总到您想要的信息中,按时间和饮食组获取统计数据
df请您的问题显示您遇到问题的代码,然后我们可以尝试帮助解决具体问题。你也应该阅读。请您的问题显示您遇到问题的代码的一部分,然后我们可以尝试帮助解决具体问题。你也应该阅读。