2017-03-05 70 views
1

我有4個矢量,每個矢量16個值;每個值都是項目的平均值,並且我在4個數據集中具有相同的16個項目。R ggplot2:將多個變量的平均值和標準差添加到同一圖中

我使用ggplot2來繪製這些方法:這裏有一個可重複的例子。

library("ggplot2") 
library("dplyr")  

means <- as.data.frame(cbind(rnorm(16),rnorm(16), rnorm(16), rnorm(16))) 
means <- mutate(means, id = rownames(means)) 
colnames(means)<-c("1", "2", "3", "4", "Symptoms") 
means_long <- melt(means, id="Symptoms") 
means_long$Symptoms <- as.numeric(means_long$Symptoms) 
names(means_long)[2] <- "Datasets" 

ggplot(data=means_long, aes(x=Symptoms, y=value, colour=Datasets)) + 
     geom_line() + 
     geom_point(shape = 21, fill = "white", size = 1.5, stroke = 1) + 
     xlab("Symptoms") + ylab("Means") + 
     scale_y_continuous() + 
     scale_x_continuous(breaks=c(1:16)) + 
     theme_bw() + 
     theme(panel.grid.minor=element_blank()) + 
     coord_flip() 

現在,我還有4個其他矢量,它們是4個數據集的16個項目的標準偏差。我想把他們繪製成同一個情節。數據與上述格式相同,因此它實際上是相同的代碼。

我想要使用相同的顏色,但不同的線條類型(因此數據集1的意思是紅色的,數據集1的標準差是虛線的)在同一圖表中的標準偏差,並且在最好的情況下,無論是通過數據集(如我目前的數據),除了「平均」與線和虛線的「標準偏差」之外。

謝謝你的幫助!

回答

1

我建議你只需要1個單數據幀繪製的實現。另外你不需要調整你的代碼,但是你仍然能夠區分數據集(即1,2,3,4)和值的類型(例如,平均值,sd)。

library("ggplot2") 
library("dplyr")  

# Means 
means <- as.data.frame(cbind(rnorm(16),rnorm(16), rnorm(16), rnorm(16))) 
means <- mutate(means, id = rownames(means)) 
colnames(means)<-c("1", "2", "3", "4", "Symptoms") 
means_long <- melt(means, id="Symptoms") 
means_long$Symptoms <- as.numeric(means_long$Symptoms) 
names(means_long)[2] <- "Datasets" 

# Sd 
sds_long <- means_long 
sds_long$value <- -sds_long$value 

################################################################################ 
# Add "Type" column to distinguish means and sds 
################################################################################ 
type <- c("Mean") 
means_long <- cbind(means_long, type) 

type <- c("Sd") 
sds_long <- cbind(sds_long, type) 

merged <- rbind(means_long, sds_long) 

colnames(merged)[4] <- "Type" 

################################################################################ 
# Plot 
################################################################################ 
ggplot(data = merged) + 
    geom_line(aes(x = Symptoms, y = value, col = Datasets, linetype = Type)) + 
    geom_point(aes(x = Symptoms, y = value, col = Datasets), 
      shape = 21, fill = "white", size = 1.5, stroke = 1) + 
    xlab("Symptoms") + ylab("Means") + 
    scale_y_continuous() + 
    scale_x_continuous(breaks=c(1:16)) + 
    theme_bw() + 
    theme(panel.grid.minor=element_blank()) + 
    coord_flip() 

enter image description here

+0

真棒,謝謝! – Torvon

2

這有幫助嗎?

爲了不讓它看起來超級難看,我將所有的隨機均值作爲正值,然後將示例標準偏差作爲負值。繪製同一圖表上的值的方法是將數據集分別提供給每個幾何圖元,而不是在初始函數中定義。

讓我知道這是不是你在想什麼:

library("ggplot2") 
library("dplyr")  

means <- as.data.frame(abs(cbind(rnorm(16),rnorm(16), rnorm(16), rnorm(16)))) 
means <- mutate(means, id = rownames(means)) 
colnames(means)<-c("1", "2", "3", "4", "Symptoms") 
means_long <- reshape2::melt(means, id="Symptoms") 
means_long$Symptoms <- as.numeric(means_long$Symptoms) 
names(means_long)[2] <- "Datasets" 


sds_long <- means_long 
sds_long$value <- -sds_long$value 

ggplot() + 
    geom_line(aes(x=Symptoms, y=value, colour=Datasets), lty=1, data=means_long) + 
    geom_point(aes(x=Symptoms, y=value, colour=Datasets), data=means_long, shape = 21, fill = "white", size = 1.5, stroke = 1) + 
    geom_line(aes(x=Symptoms, y=value, colour=Datasets), lty=2, data=sds_long) + 
    geom_point( aes(x=Symptoms, y=value, colour=Datasets), data=sds_long, shape = 21, fill = "white", size = 1.5, stroke = 1) + 
    xlab("Symptoms") + ylab("Means") + 
    scale_y_continuous() + 
    scale_x_continuous(breaks=c(1:16)) + 
    theme_bw() + 
    theme(panel.grid.minor=element_blank()) + 
    coord_flip() 

enter image description here

要回答你的傳奇查詢。總之,我認爲這非常困難,因爲兩個數據集都使用了相同的映射美學。

但是,使用code from this answer - 我做了以下。這個想法是從兩張圖中獲得傳說,只繪製手段/ sds,然後將這些圖例添加到沒有圖例的情節版本中。它可以適用,所以你更手動定位的傳說......

### Step 1 
# Draw a plot with the colour legend 
p1 <- ggplot() + 
    geom_line(aes(x=Symptoms, y=value, colour=Datasets), lty=1, data=means_long) + 
    geom_point(aes(x=Symptoms, y=value, colour=Datasets), data=means_long, shape = 21, fill = "white", size = 1.5, stroke = 1) + 
    scale_color_manual(name = "Means",values=c("red","blue", "green","pink")) + 
    coord_flip()+ 
    theme_bw() + 
    theme(panel.grid.minor=element_blank()) + 
    theme(legend.position = "top") 

# Extract the colour legend - leg1 
library(gtable) 
leg1 <- gtable_filter(ggplot_gtable(ggplot_build(p1)), "guide-box") 

### Step 2 
# Draw a plot with the size legend 
p2 <- ggplot() + 
    geom_line(aes(x=Symptoms, y=value, color=Datasets), lty=2, data=sds_long) + 
    geom_point( aes(x=Symptoms, y=value, color=Datasets), data=sds_long, shape = 21, fill = "white", size = 1.5, stroke = 1) + 
    coord_flip()+ 
    theme_bw() + 
    theme(panel.grid.minor=element_blank()) + 
    scale_color_manual(name = "SDs",values=c("red","blue", "green","pink")) 

# Extract the size legend - leg2 
leg2 <- gtable_filter(ggplot_gtable(ggplot_build(p2)), "guide-box") 

# Step 3 
# Draw a plot with no legends - plot 
p3<-ggplot() + 
    geom_line(aes(x=Symptoms, y=value, colour=Datasets), lty=1, data=means_long) + 
    geom_point(aes(x=Symptoms, y=value, colour=Datasets), data=means_long, shape = 21, fill = "white", size = 1.5, stroke = 1) + 
    geom_line(aes(x=Symptoms, y=value, color=Datasets), lty=2, data=sds_long) + 
    geom_point( aes(x=Symptoms, y=value, color=Datasets), data=sds_long, shape = 21, fill = "white", size = 1.5, stroke = 1) + 
    xlab("Symptoms") + ylab("Means") + 
    scale_y_continuous() + 
    scale_x_continuous(breaks=c(1:16)) + 
    theme_bw() + 
    theme(panel.grid.minor=element_blank()) + 
    coord_flip()+ 
    scale_color_manual(values=c("red","blue", "green","pink")) + 
    theme(legend.position = "none") 

### Step 4 
# Arrange the three components (plot, leg1, leg2) 
# The two legends are positioned outside the plot: 
# one at the top and the other to the side. 
library(grid) 
plotNew <- arrangeGrob(leg1, p3, 
         heights = unit.c(leg1$height, unit(1, "npc") - leg1$height), ncol = 1) 

plotNew <- arrangeGrob(plotNew, leg2, 
         widths = unit.c(unit(1, "npc") - leg2$width, leg2$width), nrow = 1) 

grid.newpage() 
grid.draw(plotNew) 

enter image description here

相關問題