> data_long
# A tibble: 1,459,188 × 6
AM1 group clusters `Tissue structure` CellType Value
<dbl> <chr> <chr> <chr> <chr> <dbl>
1 0.352 SiO2_7 Interstitial zone Alveolar NAKAMURA_BRONCHIAL_AND_BRONCHIOLAR_EPITHELIA 349.
2 0.352 SiO2_7 Interstitial zone Alveolar GOBP_ESTABLISHMENT_OF_LEFT_RIGHT_ASYMMETRY -0.158
3 0.352 SiO2_7 Interstitial zone Alveolar GOBP_AXONEMAL_CENTRAL_APPARATUS_ASSEMBLY 0.0180
4 0.352 SiO2_7 Interstitial zone Alveolar HP_ABNORMAL_RESPIRATORY_MOTILE_CILIUM_PHYSIOLOGY 0.0167
5 0.352 SiO2_7 Interstitial zone Alveolar HP_COILED_SPERM_FLAGELLA -0.115
6 0.352 SiO2_7 Interstitial zone Alveolar GOBP_INNER_DYNEIN_ARM_ASSEMBLY -0.108
7 0.352 SiO2_7 Interstitial zone Alveolar HP_ABNORMAL_SPERM_TAIL_MORPHOLOGY -0.110
8 0.352 SiO2_7 Interstitial zone Alveolar GOBP_AXONEME_ASSEMBLY -0.0562
9 0.352 SiO2_7 Interstitial zone Alveolar REACTOME_CONJUGATION_OF_BENZOATE_WITH_GLYCINE -0.03
10 0.352 SiO2_7 Interstitial zone Alveolar GOBP_DETERMINATION_OF_PANCREATIC_LEFT_RIGHT_ASYMMETRY -0.105
# ℹ 1,459,178 more rows
# ℹ Use `print(n = ...)` to see more rows
> ggplot(data_long, aes(x = data_long[, my_desired_celltype], y = data_long[,"Value"])) +
+ geom_point()
Don't know how to automatically pick scale for object of type <tbl_df/tbl/data.frame>. Defaulting to continuous.
Don't know how to automatically pick scale for object of type <tbl_df/tbl/data.frame>. Defaulting to continuous.
Error in `data[[var]][scale_index[[i]]]`:
! Can't subset columns past the end.
ℹ Locations 2, 3, 4, …, 1459187, and 1459188 don't exist.
ℹ There is only 1 column.
Run `rlang::last_trace()` to see where the error occurred.
Warning message:
In geom_point() :
All aesthetics have length 1, but the data has 1459188 rows.
ℹ Did you mean to use `annotate()`?
>
解释:
aes(x = !!sym(my_desired_celltype), y = Value)
:这里的!!sym(my_desired_celltype)
是为了动态地将my_desired_celltype
变量作为列名传入,确保 ggplot 正确找到对应的列。Value
列不需要加索引,可以直接使用它的名字。如果
my_desired_celltype
是一个字符变量,指代列名(如"CellType"
),你可以使用!!sym(my_desired_celltype)
来动态解析这个列名。如果列名是直接写入代码中的话,你可以直接这样写:r
复制代码
# 如果列名已知,直接使用 ggplot(data_long, aes(x = CellType, y = Value)) + geom_point()
这样可以避免索引超出范围的错误。
Imagine you're setting up a scene for a play, with each actor in the right place. But when the curtain rises, suddenly, all the actors are in the wrong spots! This is a little like what happened with your code. Here's how the story unfolds:
You’ve got a huge cast of characters—over 1.4 million rows of data in your
data_long
tibble. And you're askingggplot2
to set the stage by mapping values from two columns:my_desired_celltype
andValue
. But there's a mix-up: you're usingdata_long[, my_desired_celltype]
to grab one of your actors, which refers to an entire data frame instead of a single column. That’s whyggplot2
doesn't know how to handle this—it’s like handing it a whole crew instead of just one actor.Here's the fix: instead of indexing the data within
ggplot
, let’s tell the plot exactly which columns to use in a more direct way. We'll pointggplot
to the right column names, like this:r
复制代码
ggplot(data_long, aes(x = !!sym(my_desired_celltype), y = Value)) + geom_point()
In this version of the story, the sym() function from the rlang package turns your
my_desired_celltype
string into a symbol thatggplot
understands, ensuring that the correct column is selected for the x-axis. The !! operator then unquotes it, allowingggplot
to treat it like a column.Now, when the curtain rises again, everything is in its right place: your data actors are set, and the show (your plot) can go on!
正确写法
ggplot(data_long, aes(x = !!sym(my_desired_celltype), y = Value)) + geom_point() + geom_smooth(method = "lm") + facet_wrap(~ CellType) + # This automatically uses the column names labs(x = paste0(my_desired_celltype), y = "", title = "Correlation with AT2_coarse") + theme_classic() + scale_fill_manual(values = c("blue", "green", "red")) + theme( axis.text.x = element_text(angle = 45, vjust = 0.5, hjust = 0.5), axis.text.y = element_text(size = 12), axis.text = element_text(color = 'black', size = 12), plot.title = element_text(family = '', face = 'bold', colour = 'black', size = 12, hjust = 0.5, vjust = 0.5) ## Title centered )