如何使用ggplot绘制具有每行标准偏差的线 [英] How to plot line with standard deviation of each row with ggplot
问题描述
我想用每行(而不是列!)的中位数(第一列的值除外)绘制图形,并以标准偏差作为误差线.结果应类似于以下内容:
I want to plot a graph with the median of each row (not column!)(except values from the first column) with the standard deviation as errorbar. The result should look similar to that:
我有一个像这样的数据框:
I have a dataframe like this:
myTable <- "
1 -50 -52
2 -44 -51
3 -48 -50
4 -50 -49
5 -44 -49
6 -48 -49
7 -48 -49
8 -44 -48
9 -49 -48
10 -48 -45
11 -60 -48
10 -50 -48
11 -80 -47"
df <- read.table(text=myTable, header = TRUE)
df <- c("ID","Value1","Value2");
我的数据存储在一个.csv文件中,该文件通过以下行加载:
My data is stored in a .csv file, which I load with the following line:
df <- read.csv(file="~/path/to/myFile.csv", header=FALSE, sep=",")
推荐答案
下面的代码创建了一个辅助函数,以提供绘图的中值和sd值.在绘制之前,我们还将数据转换为长"格式.
The code below creates a helper function to provide the median and sd values for plotting. We also transform the data to "long" format before plotting.
library(tidyverse)
theme_set(theme_bw())
df <- read.table(text=myTable, header = TRUE)
names(df) <- c("ID","Value1","Value2")
median_sd = function(x, n=1) {
data_frame(y = median(x),
sd = sd(x),
ymin = y - n*sd,
ymax = y + n*sd)
}
ggplot(df %>% gather(key, value, -ID), aes(ID, value)) +
stat_summary(fun.data=median_sd, geom="errorbar", width=0.1) +
stat_summary(fun.y=median, geom="line") +
stat_summary(fun.y=median, geom="point") +
scale_x_continuous(breaks=unique(df$ID))
您可以通过以下代码避免使用helper函数,但是如果您打算做很多事情,那么该函数很方便.
You can avoid the helper function with the following code, but the function is handy to have around if you're going to do this a lot.
ggplot(df %>% gather(key, value, -ID), aes(ID, value)) +
stat_summary(fun.y=median, fun.ymin=function(x) median(x) - sd(x),
fun.ymax=function(x) median(x) + sd(x), geom="errorbar", width=0.1) +
stat_summary(fun.y=median, geom="line") +
stat_summary(fun.y=median, geom="point") +
scale_x_continuous(breaks=unique(df$ID))
这篇关于如何使用ggplot绘制具有每行标准偏差的线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!