简体   繁体   English

ggplot2 汇总统计中的所有值均未出现标准偏差

[英]Standard deviation not appearing for all values in ggplot2 summary statistics

I'm having a strange and specific issue with ggplot2 with this data .我在使用此数据的 ggplot2 时遇到了一个奇怪而具体的问题。

The output plot from my code (below) is very close to what I want, but after much troubleshooting, I am unable to resolve why the correct standard deviation is not displayed for the bottom-left panel, which should clearly have visible error bars.我的代码(下方)中的 output plot 与我想要的非常接近,但经过多次故障排除后,我无法解决为什么左下面板没有显示正确的标准偏差,该面板应该清楚地显示错误栏。 Many thanks in advance for any insights on this problem.非常感谢您对此问题的任何见解。

library(tidyverse)
data <- read_csv('sort.csv')
data$Number <- as.factor(data$Number)
data$Time <- as.factor(data$Time)
data$Condition <- factor(data$Condition,
                            labels = c('0 mg/mL','0.5 mg/mL','1 mg/mL','5 mg/mL','10 mg/mL'))

data_sum <- function(x) {
  m <- mean(x)
  ymin <- m-sd(x)
  ymax <- m+sd(x)
  return(c(y=m,ymin=ymin,ymax=ymax))
}

ggplot(data, aes(x = Time, y = Percent)) +
  geom_dotplot(binaxis = 'y', stackdir = 'center') +
  scale_x_discrete(labels=c('1 hour','6 hours')) +
  xlab("Timepoint") +
  ylab("Percent live cells") +
  ylim(0,100) +
  facet_grid(vars(data$`Cell line`), vars(data$Condition)) +
  stat_summary(fun.data=data_sum, color="red")

在此处输入图像描述


edit: Didn't know about dput – thanks, @s__!编辑:不知道dput – 谢谢,@s__! Here is my data :这是我的data

> dput(data)
structure(list(`Cell line` = c("MCF10A", "MCF10A", "MCF10A", 
"MCF10A", "MCF10A", "MCF10A", "MCF10A", "MCF10A", "MCF10A", "MCF10A", 
"MCF10A", "MCF10A", "MCF10A", "MCF10A", "MCF10A", "MCF10A", "MCF10A", 
"MCF10A", "MCF10A", "MCF10A", "MCF10A", "MCF10A", "MCF10A", "MCF10A", 
"MCF10A", "MCF10A", "MCF10A", "MCF10A", "MCF10A", "MCF10A", "MCF10A", 
"MCF10A", "MCF10A", "MCF10A", "MCF10A", "MCF10A", "HELA", "HELA", 
"HELA", "HELA", "HELA", "HELA", "HELA", "HELA", "HELA", "HELA", 
"HELA", "HELA", "HELA", "HELA", "HELA", "HELA", "HELA", "HELA", 
"HELA", "HELA", "HELA", "HELA", "HELA", "HELA", "HELA", "HELA", 
"HELA", "HELA", "HELA", "HELA", "HELA", "HELA", "HELA", "HELA", 
"HELA", "HELA", "3T3", "3T3", "3T3", "3T3", "3T3", "3T3", "3T3", 
"3T3", "3T3", "3T3", "3T3", "3T3", "3T3", "3T3", "3T3", "3T3", 
"3T3", "3T3", "3T3", "3T3", "3T3", "3T3", "3T3", "3T3", "3T3", 
"3T3", "3T3", "3T3", "3T3", "3T3", "3T3", "3T3", "3T3", "3T3", 
"3T3", "3T3"), Time = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L), .Label = c("1", "2"), class = "factor"), 
    Condition = structure(c(5L, 5L, 5L, 4L, 4L, 4L, 3L, 3L, 3L, 
    2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 5L, 5L, 4L, 4L, 4L, 
    3L, 3L, 3L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 5L, 5L, 
    4L, 4L, 4L, 3L, 3L, 3L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 
    5L, 5L, 5L, 4L, 4L, 4L, 3L, 3L, 3L, 2L, 2L, 2L, 1L, 1L, 1L, 
    1L, 1L, 1L, 5L, 5L, 5L, 4L, 4L, 4L, 3L, 3L, 3L, 2L, 2L, 2L, 
    1L, 1L, 1L, 1L, 1L, 1L, 5L, 5L, 5L, 4L, 4L, 4L, 3L, 3L, 3L, 
    2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("0 mg/mL", 
    "0.5 mg/mL", "1 mg/mL", "5 mg/mL", "10 mg/mL"), class = "factor"), 
    Number = structure(c(23L, 24L, 22L, 34L, 26L, 11L, 24L, 31L, 
    25L, 33L, 16L, 17L, 43L, 14L, 56L, 56L, 63L, 44L, 6L, 10L, 
    12L, 32L, 29L, 3L, 20L, 37L, 36L, 40L, 27L, 21L, 30L, 61L, 
    60L, 4L, 18L, 39L, 15L, 19L, 18L, 68L, 70L, 51L, 57L, 71L, 
    89L, 84L, 97L, 85L, 83L, 95L, 94L, 92L, 82L, 93L, 1L, 5L, 
    2L, 28L, 42L, 41L, 49L, 81L, 96L, 86L, 87L, 78L, 74L, 67L, 
    91L, 58L, 54L, 47L, 13L, 15L, 8L, 57L, 61L, 65L, 48L, 64L, 
    62L, 79L, 72L, 65L, 33L, 45L, 67L, 35L, 38L, 46L, 12L, 9L, 
    7L, 53L, 69L, 52L, 59L, 55L, 50L, 75L, 66L, 76L, 77L, 73L, 
    88L, 90L, 80L, 76L), .Label = c("7", "9", "10", "13", "15", 
    "17", "18", "20", "24", "26", "30", "33", "34", "39", "45", 
    "50", "57", "58", "63", "66", "68", "75", "80", "85", "87", 
    "90", "104", "107", "118", "120", "121", "126", "127", "131", 
    "139", "142", "147", "155", "157", "167", "168", "175", "195", 
    "204", "206", "209", "222", "226", "227", "235", "236", "239", 
    "243", "246", "249", "253", "254", "258", "263", "265", "272", 
    "276", "285", "286", "292", "295", "296", "298", "306", "316", 
    "331", "336", "338", "348", "355", "369", "371", "372", "374", 
    "379", "382", "388", "395", "398", "408", "413", "416", "418", 
    "421", "443", "453", "454", "468", "478", "497", "544", "553"
    ), class = "factor"), Percent = c(20.83, 22.61, 20.38, 47.29, 
    42.86, 37.5, 79.44, 90.98, 84.47, 80.89, 80.65, 80.28, 99.49, 
    78, 95.83, 94.05, 94.06, 95.77, 19.32, 20.8, 14.29, 41.58, 
    43.7, 45.45, 82.5, 80.77, 85.54, 93.3, 97.2, 87.18, 98.36, 
    97.84, 98.15, 59.09, 87.88, 92.9, 66.18, 54.78, 54.72, 96.13, 
    96.34, 98.33, 98.83, 99.1, 98.14, 99, 99.28, 97.61, 99, 99.2, 
    97.75, 97.63, 98.23, 99.15, 18.92, 22.06, 15.79, 86.99, 75.11, 
    87.05, 97.41, 98.45, 98.37, 98.57, 99.05, 99.47, 98.58, 98.67, 
    99.12, 99.61, 99.6, 100, 31.48, 39.82, 80, 96.58, 98.55, 
    97.33, 97.41, 97.95, 99.28, 98.68, 97.67, 99.32, 100, 99.52, 
    98.67, 99.29, 98.1, 98.12, 27.05, 48, 66.67, 95.67, 96.53, 
    97.15, 97.77, 92.57, 97.92, 98.88, 98.99, 99.19, 97.38, 100, 
    99.29, 100, 98.95, 99.46)), row.names = c(NA, -108L), spec = structure(list(
    cols = list(`Cell line` = structure(list(), class = c("collector_character", 
    "collector")), Time = structure(list(), class = c("collector_double", 
    "collector")), Condition = structure(list(), class = c("collector_double", 
    "collector")), Number = structure(list(), class = c("collector_double", 
    "collector")), Percent = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1), class = "col_spec"), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"))

Just to close this question — many thanks to @e.matt.只是为了结束这个问题——非常感谢@e.matt。

The ylim(0,100) line in my code was hiding the bottom-left panel's error bars because they extended past the plot's ymax value.我代码中的ylim(0,100)行隐藏了左下方面板的错误栏,因为它们超出了绘图的ymax值。 Adding scale_y_continuous() resolved this error:添加scale_y_continuous()解决了这个错误:

在此处输入图像描述

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

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