R语言 根据数据中的值有条件地标记x刻度,table

mxg2im7a  于 2023-05-04  发布在  其他
关注(0)|答案(1)|浏览(122)

我有一个图表,我试图使,其中任何数据点-标准差,高于阈值是红色的“命中”,所有其他点(失败)是黑色的。同样,只有“命中”的x刻度才应该显示。x-tick应该是条件(MUT10MUT9等)。不幸的是,我在x刻度中得到了一些失败的标签,这些标签显示不应该这样做。另外,我不明白为什么我要获取行的索引,而不是condition列中该行的值。下面是目前的数字。
有谁知道我能做些什么来解决这个问题吗?谢谢你!

> norm_cond_tab
    condition      data1     data2 mean_of_norms    median         sd        sem
 1:     MUT10 0.43516143 0.9977380     0.7164497 0.7164497 0.39780172 0.28128829
 2:      MUT9 0.13804990 1.0729768     0.6055134 0.6055134 0.66109318 0.46746347
 3:      MUT8 0.04577757 0.4477349     0.2467562 0.2467562 0.28422672 0.20097864
 4:      MUT7 0.10926102 0.4216915     0.2654763 0.2654763 0.22092172 0.15621524
 5:      MUT6 0.97922528 0.6028519     0.7910386 0.7910386 0.26613618 0.18818670
 6:      MUT5 1.30589892 2.3693953     1.8376471 1.8376471 0.75200548 0.53174817
 7:      MUT4 0.15938661 2.8779801     1.5186834 1.5186834 1.92233589 1.35929674
 8:      MUT3 2.38469180 2.3490335     2.3668626 2.3668626 0.02521424 0.01782916
 9:      MUT2 3.72940920 3.0275164     3.3784628 3.3784628 0.49631315 0.35094640
10:      MUT1 4.68433931 2.2624763     3.4734078 3.4734078 1.71251573 1.21093148
only_hits_above_cutoff <- function(x) {
  x <- unique(x)
  ifelse((norm_cond_tab[['mean_of_norms']] - norm_cond_tab[['sd']]) < 1, x <- norm_cond_tab[['condition']], x <- "")     
}

#plot sum(movement) of the experimental conditions / (mean + 2 standard deviations) of the control
above_2sd <- ggplot(norm_cond_tab, aes(x=condition, y=mean_of_norms)) + 
  geom_point(size = 1) +
  labs(color = 'Conditions', x = 'Condition', y = 'Sum of movement normalized to mutant control mean + 2 sd') +
  geom_hline(yintercept=1, linetype="dashed", color = "black", linewidth=0.5) +
  geom_errorbar(aes(ymin = mean_of_norms - sd, 
                    ymax = mean_of_norms + sd,
                    color = ifelse((mean_of_norms - sd) > 1, 'Hit', 'Fail'))) +
  scale_color_manual(values = c('#000000', '#FF0000')) + #red is a hit, black is a fail
  scale_x_discrete(labels = only_hits_above_cutoff(norm_cond_tab[['condition']])) +
  theme_classic(base_size = 20) +
  theme(
    legend.position = 'right', #also controls 'no legend'
    plot.title = element_text(face = 'bold', margin = margin(10, 0, 10, 0), hjust = 0.5, size = 12), #margin(t, r, b, l)
    axis.text.y = element_text(size = 18),
    axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1, size = 18),
    axis.title = element_text(color = 'black', face = 'bold'),
    axis.title.x = element_text(margin = margin(t = 12), size = 24), #margin(t, r, b, l)
    axis.title.y = element_text(margin = margin(r = 10), size = 24))

数据的可使用格式

norm_cond_tab <- data.table::data.table(
  condition = factor(
    c("MUT10", "MUT9", "MUT8", "MUT7", "MUT6", "MUT5", "MUT4", "MUT3", "MUT2", "MUT1"),
    levels = c(
      "MUT1", "MUT2", "MUT3", "MUT4", "MUT5", "MUT6", "MUT7", "MUT8", "MUT9",
      "MUT10", "MUT_CTL", "WT_CTL"
    )
  ),
  data1 = c(
    0.435161429360894, 0.13804989823591, 0.045777565897694, 0.109261020196454,
    0.979225278646829, 1.30589891964651, 0.159386614385043, 2.38469180490313,
    3.7294092039302, 4.68433931418873
  ),
  data2 = c(
    0.997738017658577, 1.07297683710463, 0.447734851377348, 0.421691506665906,
    0.602851881280868, 2.36939526207563, 2.87798009700854, 2.3490334904144,
    3.02751641018818, 2.26247634990184
  ),
  mean_of_norms = c(
    0.716449723509736, 0.605513367670269, 0.246756208637521, 0.26547626343118,
    0.791038579963849, 1.83764709086107, 1.51868335569679, 2.36686264765876,
    3.37846280705919, 3.47340783204529
  ),
  median = c(
    0.716449723509736, 0.605513367670269, 0.246756208637521, 0.26547626343118,
    0.791038579963849, 1.83764709086107, 1.51868335569679, 2.36686264765876,
    3.37846280705919, 3.47340783204529
  ),
  sd = c(
    0.397801720522084, 0.661093178388051, 0.28422672231, 0.220921715631961,
    0.26613618153569, 0.752005475498722, 1.92233588685263, 0.0252142359806634,
    0.496313154120956, 1.71251572515181
  ),
  sem = c(
    0.281288294148841, 0.467463469434359, 0.200978642739827, 0.156215243234726,
    0.18818669868298, 0.53174817121456, 1.35929674131175, 0.017829157244365,
    0.350946396871012, 1.21093148214344
  )
)
anauzrmj

anauzrmj1#

解决这个问题的一个方法是使用命名向量来设置标签。此外,在ifelse中,将因子condition转换为字符以获得标签而不是索引:

library(ggplot2)

only_hits_above_cutoff <- function(x) {
  ifelse(norm_cond_tab[["mean_of_norms"]] - norm_cond_tab[["sd"]] < 1, as.character(x), "")
}

norm_cond_tab$condition1 <- only_hits_above_cutoff(norm_cond_tab[["condition"]])

labels <- setNames(norm_cond_tab$condition1, norm_cond_tab$condition)

ggplot(norm_cond_tab, aes(x = condition, y = mean_of_norms)) +
  geom_point(size = 1) +
  labs(color = "Conditions", x = "Condition", y = "Sum of movement normalized to mutant control mean + 2 sd") +
  geom_hline(yintercept = 1, linetype = "dashed", color = "black", linewidth = 0.5) +
  geom_errorbar(aes(
    ymin = mean_of_norms - sd,
    ymax = mean_of_norms + sd,
    color = ifelse((mean_of_norms - sd) > 1, "Hit", "Fail")
  )) +
  scale_color_manual(values = c("#000000", "#FF0000")) +
  scale_x_discrete(labels = labels) +
  theme_classic(base_size = 10)

相关问题