2017-04-10 14 views
0

두 개의 데이터 시트가 있습니다. 첫 번째 데이터 세트의 각 포인트는 다른 측정 값의 평균입니다. 두 번째 데이터 시트 각 점에 대한 편차.두 테이블에서 오는 데이터에 대한 Errobar가있는 다중 라인 플롯

다음은 잘 작동하는 첫 번째 데이터에서 lineplot을 만들기위한 R 스크립트를 첨부했습니다. 코드와 함께 난 다음

enter image description here

지금은 유사 이전을 플롯을 만드는 두 번째 테이블 (표준 편차)를 사용하기를 원하지만 이제 즉, errorbar을 보여주는 같은 플롯을 만드는 것이 있습니다 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

이 단정 한 두 data.frame, geom_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