简体   繁体   English

用来自不同数据框ggplot R的误差线绘制点

[英]Plotting points with error bars from different dataframes ggplot R

I have 3 dataframes one with actual data and others with upper and lower limits data respectively.我有 3 个数据框,一个包含实际数据,另一个分别包含上限和下限数据。 How can I combine these dataframes and create a scatter plot on ggplot with error bars (where the upper and lower limit data serve as the bars)?如何组合这些数据框并在 ggplot 上创建带有误差线的散点图(上限和下限数据用作条形)?

Here is the example of the data and plotting code这是数据和绘图代码的示例

library(tidyverse)
library(dplyr)
library(ggplot2)

DT <- data.frame(Samples=c("D1", "D2", "D3", "D4"),
             ACE=c(0.184, -0.169, 0.054, 0.087),
             FLN=c(-0.257, -0.239, -0.009, 0.038),
             ANT=c(-0.166, -0.304, 0.238, "" ),
             stringsAsFactors=FALSE)



 LowerLim <- data.frame(Samples=c("D1", "D2", "D3", "D4"),
              ACE=c(-1.514, "", -1.644, -1.611),
              FLN=c("", "", "", -1.660),
              ANT=c(-1.865, -2.003, 1.937, ""),
              stringsAsFactors=FALSE)

 UpperLim <- data.frame(Samples=c("D1", "D2", "D3", "D4"),
                   ACE=c(-0.560, "", -0.689,-0.657),
                   FLN=c("", "", "", -0.706),
                   ANT=c(-0.911,-1.049,0.983, "" ),
                   stringsAsFactors=FALSE)

 DTlong <- gather(DT, Cds, Values, "ACE":"ANT", factor_key=TRUE)

DTlong$Samples <- as.character(DTlong$Samples)

DTlong$Samples <- factor(DTlong$Samples, levels=unique(DTlong$Samples))

ggplot(DTlong)+geom_point(aes(color=Samples, y=Values, x=Cds))+
     theme(axis.title.x = element_blank())+ 
     theme(axis.text.x=element_text(size=7, angle=90,hjust=0.95,vjust=0.2))+
    ggtitle("Points")

There are a couple of issues here.这里有几个问题。 Firstly, some of your columns are in character format rather than numeric format because you have put missing values as "" rather than NA .首先,您的某些列是字符格式而不是数字格式,因为您将缺失值设置为""而不是NA I appreciate that this might look more natural when printing the data frame to the screen, but it means any column containing "" is no longer numeric, and can't be used for calculations or plotting the values the column appears to contain.我很欣赏在将数据框打印到屏幕时这可能看起来更自然,但这意味着任何包含""的列都不再是数字,并且不能用于计算或绘制该列似乎包含的值。

That means the first step should be converting the three value columns in each data frame to numeric values:这意味着第一步应该将每个数据框中的三个值列转换为数值:

DT[-1] <- lapply(DT[-1], as.numeric)
LowerLim[-1] <- lapply(LowerLim[-1], as.numeric)
UpperLim[-1] <- lapply(UpperLim[-1], as.numeric)

The second issue is that your DT data frame and UpperLit data frame appear to be round the wrong way, since all of the values in DT are higher than the values in UpperLimit .第二个问题是您的DT数据框和UpperLit数据框似乎是错误的,因为 DT 中的所有值都高于UpperLimit中的值。 For the purposes of this answer, I've had to assume this is a mistake.出于此答案的目的,我不得不假设这是一个错误。 If your real data doesn't have this problem, you can omit this step:如果你的真实数据没有这个问题,可以省略这一步:

tmp <- DT
DT <- UpperLim
UpperLim <- tmp
rm(tmp)

The third issue is that the data are in the wrong shape to be plotted.第三个问题是要绘制的数据形状错误。 Since the row and column orders are all the same, we can achieve this by pivoting each data frame into long format, and just keeping the value column from the upper and lower lim data frames.由于行和列的顺序都是相同的,我们可以通过将每个数据帧转换为长格式来实现这一点,并且只保留上下 lim 数据帧的值列。

df <- cbind(pivot_longer(DT, -1, names_to = "Cds", values_to = "Values"),
            lower = pivot_longer(LowerLim, -1)$value,
            upper = pivot_longer(UpperLim, -1)$value)

Now we are ready to plot.现在我们准备好绘图了。 We will need to use position_dodge on both the points and the error bars to spread them out:我们需要在点和误差条上使用position_dodge来将它们展开:

ggplot(df, aes(Cds, Values, color = Samples)) +
  geom_point(position = position_dodge(width = 0.5), size = 3) +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.25, size = 1,
                position = position_dodge(width = 0.5)) +
  scale_color_brewer(palette = "Set1") +
  theme_minimal(base_size = 16) +
  theme(axis.title.x = element_blank()) +
  ggtitle("Points")

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM