簡體   English   中英

R ggplot2:在多個變量的相同圖中添加均值和標准差

[英]R ggplot2: add mean and standard deviation in same plot for multiple variables

我有4個向量,每個向量16個值; 每個值都是一個項目的平均值,我在4個數據集中有相同的16個項目。

我使用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標准差為虛線),最好的情況是通過數據集區分兩者的圖例(正如我目前所做的那樣)除了線條和虛線的“均值”與“標准偏差”之外。

謝謝您的幫助!

這有幫助嗎?

為了使它看起來不那么丑陋,我將所有隨機平均值都設為正值,然后只是將示例標准偏差設為負值。 在同一圖表上繪制值的方法是將數據集分別輸入到每個geom,而不是在初始ggplot()函數中定義。

如果這不是您的想法,請告訴我:

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()

在此輸入圖像描述

要回答您的圖例查詢。 簡而言之,我認為這非常困難,因為兩個數據集都使用相同的映射美學。

但是,使用這個答案代碼 - 我做了以下。 我們的想法是從兩個情節中獲取圖例,只繪制平均值/ 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) 

在此輸入圖像描述

我建議你只需要一個數據幀來實現一個實現。 此外,您不需要對代碼進行太多調整,但您仍然可以區分數據集(即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()

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM