如何在图中每列的每一行旁边添加值

问题描述 投票:0回答:1

我想生成这样的图形,其中每一行都应该在 y 轴上有其值,我使用红色箭头来指示值应该在哪里,这些值源于数据,所以每个条形都应该有一个值,我添加了手动计算图中的值,它们看起来是相同的数字,显然它们不是。我正在使用 synergyfinder 包(https://github.com/IanevskiAleksandr/SynergyFinder#readme),它使用 ggplot: enter image description here

这是我的代码:

if (!require("BiocManager", quietly = TRUE))
  install.packages("BiocManager")

BiocManager::install("synergyfinder")
s
a
.libPaths()

library(synergyfinder)
library(readxl)


res <- ReshapeData(
  data = data,
  data_type = "inhibition",
  impute = TRUE,
  impute_method = NULL,
  noise = TRUE,
  seed = 1)


res <- CalculateSynergy(
  data = res,
  method = c("ZIP", "HSA", "Bliss", "Loewe"),
  Emin = NA,
  Emax = NA,
  correct_baseline = "non")


res$drug_pairs




res <- CalculateSensitivity(
  data = res,
  correct_baseline = "non"
)


sensitive_columns <- c(
  "block_id", "drug1", "drug2",
  "ic50_1", "ic50_2",
  "ri_1", "ri_2",
  "css1_ic502", "css2_ic501", "css")
res$drug_pairs[, sensitive_columns]




for (i in c(1, 2)){
  PlotDoseResponseCurve(
    data = res,
    plot_block = 1,
    drug_index = i,
    plot_new = FALSE,
    record_plot = FALSE
  )
}

par(mar = c(4, 8, 4, 6) + 0.1) 

PlotMultiDrugBar(
  data = res,
  plot_block = 1,
  plot_value = c("response", "ZIP_synergy", "Loewe_synergy", "HSA_synergy", "Bliss_synergy"),
  sort_by = "response",
  highlight_label_size = 4
)

这是 dput 的头部:

 dput(head(data))

structure(list(block_id = c(1, 1, 1, 1, 1, 1), drug1 = c("X", 
"X", "X", "X", "X", "X"), drug2 = c("N", "N", "N", "N", "N", 
"N"), cell_line_name = c("A", "A", "A", "A", "A", 
"A"), conc1 = c(0, 10, 3.333, 1.111, 0.37, 0.123), conc2 = c(0, 
0, 0, 0, 0, 0), response = c(0, 94.7124199185235, 93.1970077742489, 
94.9121253949008, 92.6627816399623, 90.6942562299578), conc_unit = c("μM", 
"μM", "μM", "μM", "μM", "μM")), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

这是我能想到的最好的:


install.packages("scales")
library(dplyr)
library(scales)
library(ggplot2)
library(gridExtra)
library(readxl) 


data <- read_xlsx("N+X+syn.xlsx")
head(data)

res <- ReshapeData(
  data = data,
  data_type = "inhibition",
  impute = TRUE,
  impute_method = NULL,
  noise = TRUE,
  seed = 1
)

res <- CalculateSynergy(
  data = res,
  method = c("ZIP", "HSA", "Bliss", "Loewe"),
  Emin = NA,
  Emax = NA,
  correct_baseline = "non"
)

res <- CalculateSensitivity(
  data = res,
  correct_baseline = "non"
)

# Convert to data frame and clean the data
res <- as.data.frame(res)

res_clean <- res %>%
  rename(
    N = synergy_scores.conc1,
    X = synergy_scores.conc2,
    response = response.response,
    ZIP_synergy = synergy_scores.ZIP_synergy,
    Bliss_synergy = synergy_scores.Bliss_synergy,
    Loewe_synergy = synergy_scores.Loewe_synergy,
    HSA_synergy = synergy_scores.HSA_synergy
  )


res_selected <- res_clean %>%
  select(N, X, response, ZIP_synergy, Bliss_synergy, Loewe_synergy, HSA_synergy)


highlight_threshold <- 1


create_plot <- function(data, column_name, plot_number) {
  fill_condition <- if (column_name %in% c("N", "X", "response")) {
    "FALSE"  # No highlighting for N, X, or response
  } else {
    paste0(column_name, " > ", highlight_threshold)  # Apply threshold to synergy scores
  }
  
  ggplot(data, aes_string(x = column_name, y = column_name, fill = fill_condition)) +
    geom_bar(stat = "identity", width = 0.6, show.legend = FALSE) +
    geom_text(aes_string(label = column_name), hjust = -0.3, size = 5, color = "black") +
    scale_fill_manual(values = c("lightblue", "orange")) + # Keep threshold highlight color consistent
    coord_flip() +
    theme_minimal() +
    labs(
      title = paste(column_name),
      subtitle = "",
      x = "",
      y = "Value"
    ) +
    theme(
      plot.title = element_text(hjust = 0.5, face = "bold", size = 16),
      plot.subtitle = element_text(hjust = 0.5, size = 12),
      axis.text.y = element_blank() # Remove y-axis labels
    )
}


plots <- lapply(1:7, function(i) create_plot(res_selected, names(res_selected)[i], i))


grid.arrange(grobs = plots, ncol = 7, nrow = 1)




并得到了这个丑陋的身材......哈哈:

enter image description here

r ggplot2
1个回答
0
投票

很高兴您在问题中添加了数据,但您并没有确保该数据重现了问题……您知道……使其可重现。为了快速获得最佳答案,这始终是您最好的选择。

我使用了 synergy 包中使用的示例数据来代替您的数据。

您写道,“我使用红色箭头来指示值应该在哪里”...但我不知道您的“红色箭头”在哪里...

我从一些简单的示例开始,说明数据的来源以及如何将标签移动到我想要的位置。

首先,这是我用

PlotMultiDrugBar
构建的数据和绘图。请注意,这里是一个名为
plt
的对象。

library(synergyfinder)
library(tidyverse)

data("mathews_screening_data")    # from example in synergy pkg
dta <- ReshapeData(mathews_screening_data)
res <- CalculateSynergy(dta)

plt <- PlotMultiDrugBar(          # from your question
  data = res,
  plot_block = 1,
  plot_value = c("response", "ZIP_synergy", "Loewe_synergy", "HSA_synergy", "Bliss_synergy"),
  sort_by = "response",
  highlight_label_size = 4
)

通常,您可以通过调用数据来获取绘图中使用的数据。例如,我将绘图命名为

plt
,因此通常我可以通过调用
plt$data
来获取数据。然而,这里的情况并非如此。数据嵌套在构建条形图的图层中,因此
plt$layer[[1]]$data

在第一个示例中,我保持简单,使用值作为标签和 y 位置 - 这并不是一个多面图 - 认为 x -> y 和 y -> x,如果我移动

y
它左右移动。

真丑!

plt + geom_text(data = plt[["layers"]][[1]][["data"]],
                mapping = aes(x = id, y = value, label = value))

ugly

如果我只是格式化值,设置 y = 10,并使大小relative 到绘图的大小......

还是很丑……而且难以辨认

plt + geom_text(data = plt[["layers"]][[1]][["data"]],
                mapping = aes(x = id, label = format(value, digits = 3)), # <-format me!
                size = rel(2.3), y = 10)   # <--- I'm new

ugly too

如果我想右对齐我的标签,我必须按组进行,因为每个组的范围都不同。在下一个示例中,我按数据所在的列对数据进行分组 (

metric
),然后捕获组的最大值。新列 (
mxg
) 是我分配给
y
的内容。

这是迄今为止最不难看的。

plt + geom_text(data = plt[["layers"]][[1]][["data"]] %>% group_by(metric) %>% 
                  mutate(mxg = max(value)) %>% ungroup(),  # group max val!
                mapping = aes(x = id, y = mxg,             # <--- I'm new
                              label = format(value, digits = 3)),
                size = rel(2.3))

a bit better

好吧,有一些标签选项供您选择。至于您在代码中调用的其他一些元素,我认为您正在尝试执行以下操作。

我将首先重命名数据中的列名称,然后添加您确定的颜色和主题选项。

这里的外观和可读性肯定得到了很大的改善。 x 轴有点难以阅读。

levels( plt[["layers"]][[1]][["data"]]$metric )  # current levels
# [1] "ispinesib\n(nM)"          "ibrutinib\n(nM)"         
# [3] "Response\n(% inhibition)" "ZIP Synergy Score"       
# [5] "Loewe Synergy Score"      "HSA Synergy Score"       
# [7] "Bliss Synergy Score"      

# change the names
levels(plt$layers[[1]]$data$metric) <- c("N", "X", "response", "Zip Synergy", 
                                         "Bliss Synergy", "Loewe Synergy", "HSA Synergy")

# with renamed columns 
plt + geom_text(data = plt[["layers"]][[1]][["data"]] %>% group_by(metric) %>% 
                 mutate(mxg = max(value)) %>% ungroup(),  # group max val!
               mapping = aes(x = id, y = mxg,
                             label = format(value, digits = 3)),
               size = rel(2.3)) +
  scale_fill_discrete(c("lightblue", "orange")) + 
  theme_minimal() %>% 
  theme(plot.title = element_text(face = "bold", size = rel(4)))  
# there is no subtitle, the titles are already centered, there is no y-axis text...

updated plot

© www.soinside.com 2019 - 2024. All rights reserved.