2017-04-10 5 views
0

私はプロットしたい点を持つデータシートを2つ持っています(最初のデータセットの各ポイントは異なる測定値の平均です)各点の偏差。2つのテーブルから来るデータのためのErrobarを持つ複数のラインプロット

以下は、うまく動作する最初のデータからlineplotを作成するためのRスクリプトを添付しました。コードで、私は今、私は以前同様のプロットを作成するために、第2のテーブル(標準偏差)を使用したいが、今も、エラーバーを示す、すなわち、以下のことを

enter image description here

のようなプロットを作成することができます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)) 
+0

がhttp://docs.ggplot2.org/0.9.3.1/geom_errorbar見ます.htmlあなたは 'geom_errorbar(data = data2、aes(x、sd)) 'を使ってエラーバーをポイントに追加することができます。 –

答えて

0

この整頓2 data.framegeom_errorbarを使用して、結果をそれらを結合し、plot

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) 

+0

これは非常にエレガントな方法で問題を解決します。 – user11924

関連する問題