带有 Errorbar 的多线图用于来自两个表的数据
Multiple lineplot with Errobar for data coming from two tables
我有两个数据表,一个是我要绘制的点(第一个数据集中的每个点是不同测量值的平均值),第二个数据包含每个点的标准偏差。
下面我附上了一个 R 脚本,用于根据工作正常的第一个数据创建线图。使用代码我可以创建如下图
现在我想使用第二个 table(标准差)来创建一个与之前类似的图,但现在还显示一个误差条,即以图形方式显示每个测量值的标准差,例如 this.
library(ggplot2)
##loads a dataframe and returns a ggplot object that can be externally modified and plotted
makeMultipleLinePlot <- function(data){
require(reshape2)
data$id <-rownames(data)
melted <- melt(data)
colnames(melted)<-c("Measurement","Month","Percentage")
g<-ggplot(data=melted,
aes(x=Month, y=Percentage, color=Measurement,group=Measurement)) +
geom_line(size=1,alpha=0.8) + geom_point(size=4,aes(shape=Measurement))
return(g)
}
##load a table from google sheets. assumes the sheet has a single table
loadTableFromGoogleSheet <- function(url, sheet) {
require(gsheet)
a <- gsheet2text(url,sheetid=sheet, format='csv')
data <- read.csv(text=a, stringsAsFactors=FALSE,header = TRUE,row.names = 1)
return(data)
}
#URL of the google spreadsheet
url <- "docs.google.com/spreadsheets/d/10clnt9isJp_8Sr7A8ejhKEZXCQ279wGP4sdygsit1LQ"
gid.humidity <- 2080295295 #gid of the google sheet containing humidity data
data.humidity<-loadTableFromGoogleSheet(url,gid.humidity)
gid.humidity_sd <- 1568896731 #gid of the google sheet containing standard deviations for each measurement in the humidity data
data.humidity_sd<-loadTableFromGoogleSheet(url,gid.humidity_sd)
ggsave(filename="lineplot/humidity.pdf", plot=makeMultipleLinePlot(data.humidity))
#ggsave(filename="lineplot/humidity.pdf", plot=makeMultipleErrorPlot(data.humidity,data.humidity_sd))
整理两个 data.frame
,加入它们并 plot
结果,使用 geom_errorbar
:
library(dplyr)
library(tidyr)
library(ggplot2)
df <- data.humidity %>%
mutate(measure = row.names(.)) %>%
gather(month, value, -measure)
df_sd <- data.humidity_sd %>%
mutate(measure = substr(row.names(.), 1, 2)) %>%
gather(month, sd, -measure)
dfF <- full_join(df, df_sd)
#> Joining, by = c("measure", "month")
ggplot(dfF, aes(month, value, group = measure, color = measure))+
geom_line(size=1,alpha=0.8) +
geom_point(aes(shape = measure)) +
geom_errorbar(aes(ymin = value - sd, ymax = value + sd), width = .3)
我有两个数据表,一个是我要绘制的点(第一个数据集中的每个点是不同测量值的平均值),第二个数据包含每个点的标准偏差。
下面我附上了一个 R 脚本,用于根据工作正常的第一个数据创建线图。使用代码我可以创建如下图
现在我想使用第二个 table(标准差)来创建一个与之前类似的图,但现在还显示一个误差条,即以图形方式显示每个测量值的标准差,例如 this.
library(ggplot2)
##loads a dataframe and returns a ggplot object that can be externally modified and plotted
makeMultipleLinePlot <- function(data){
require(reshape2)
data$id <-rownames(data)
melted <- melt(data)
colnames(melted)<-c("Measurement","Month","Percentage")
g<-ggplot(data=melted,
aes(x=Month, y=Percentage, color=Measurement,group=Measurement)) +
geom_line(size=1,alpha=0.8) + geom_point(size=4,aes(shape=Measurement))
return(g)
}
##load a table from google sheets. assumes the sheet has a single table
loadTableFromGoogleSheet <- function(url, sheet) {
require(gsheet)
a <- gsheet2text(url,sheetid=sheet, format='csv')
data <- read.csv(text=a, stringsAsFactors=FALSE,header = TRUE,row.names = 1)
return(data)
}
#URL of the google spreadsheet
url <- "docs.google.com/spreadsheets/d/10clnt9isJp_8Sr7A8ejhKEZXCQ279wGP4sdygsit1LQ"
gid.humidity <- 2080295295 #gid of the google sheet containing humidity data
data.humidity<-loadTableFromGoogleSheet(url,gid.humidity)
gid.humidity_sd <- 1568896731 #gid of the google sheet containing standard deviations for each measurement in the humidity data
data.humidity_sd<-loadTableFromGoogleSheet(url,gid.humidity_sd)
ggsave(filename="lineplot/humidity.pdf", plot=makeMultipleLinePlot(data.humidity))
#ggsave(filename="lineplot/humidity.pdf", plot=makeMultipleErrorPlot(data.humidity,data.humidity_sd))
整理两个 data.frame
,加入它们并 plot
结果,使用 geom_errorbar
:
library(dplyr)
library(tidyr)
library(ggplot2)
df <- data.humidity %>%
mutate(measure = row.names(.)) %>%
gather(month, value, -measure)
df_sd <- data.humidity_sd %>%
mutate(measure = substr(row.names(.), 1, 2)) %>%
gather(month, sd, -measure)
dfF <- full_join(df, df_sd)
#> Joining, by = c("measure", "month")
ggplot(dfF, aes(month, value, group = measure, color = measure))+
geom_line(size=1,alpha=0.8) +
geom_point(aes(shape = measure)) +
geom_errorbar(aes(ymin = value - sd, ymax = value + sd), width = .3)