簡體   English   中英

如何在x軸上顯示ggplot cumsum plot的刻度標簽

[英]How to show ticks label in x-axis for ggplot cumsum plot

我有以下數據框架。 它由兩列和九十四行組成。

library(tidyverse)

ndat <- structure(list(sample_name = c("scFOOBAR_96_S98", "scFOOBAR_20_S22", 
"scFOOBAR_83_S85", "scFOOBAR_24_S26", "scFOOBAR_76_S78", "scFOOBAR_72_S74", 
"scFOOBAR_19_S21", "scFOOBAR_60_S62", "scFOOBAR_18_S20", "scFOOBAR_23_S25", 
"scFOOBAR_92_S94", "scFOOBAR_67_S69", "scFOOBAR_08_S10", "scFOOBAR_77_S79", 
"scFOOBAR_27_S29", "scFOOBAR_71_S73", "scFOOBAR_63_S65", "scFOOBAR_80_S82", 
"scFOOBAR_36_S38", "scFOOBAR_31_S33", "scFOOBAR_86_S88", "scFOOBAR_82_S84", 
"scFOOBAR_22_S24", "scFOOBAR_14_S16", "scFOOBAR_04_S6", "scFOOBAR_30_S32", 
"scFOOBAR_10_S12", "scFOOBAR_88_S90", "scFOOBAR_91_S93", "scFOOBAR_46_S48", 
"scFOOBAR_25_S27", "scFOOBAR_29_S31", "scFOOBAR_38_S40", "scFOOBAR_34_S36", 
"scFOOBAR_51_S53", "scFOOBAR_85_S87", "scFOOBAR_35_S37", "scFOOBAR_79_S81", 
"scFOOBAR_95_S97", "scFOOBAR_56_S58", "scFOOBAR_48_S50", "scFOOBAR_52_S54", 
"scFOOBAR_03_S5", "scFOOBAR_47_S49", "scFOOBAR_73_S75", "scFOOBAR_87_S89", 
"scFOOBAR_40_S42", "scFOOBAR_55_S57", "scFOOBAR_65_S67", "scFOOBAR_43_S45", 
"scFOOBAR_41_S43", "scFOOBAR_09_S11", "scFOOBAR_05_S7", "scFOOBAR_33_S35", 
"scFOOBAR_90_S92", "scFOOBAR_57_S59", "scFOOBAR_01_S3", "scFOOBAR_94_S96", 
"scFOOBAR_70_S72", "scFOOBAR_49_S51", "scFOOBAR_81_S83", "scFOOBAR_75_S77", 
"scFOOBAR_68_S70", "scFOOBAR_21_S23", "scFOOBAR_74_S76", "scFOOBAR_64_S66", 
"scFOOBAR_17_S19", "scFOOBAR_53_S55", "scFOOBAR_26_S28", "scFOOBAR_78_S80", 
"scFOOBAR_06_S8", "scFOOBAR_84_S86", "scFOOBAR_15_S17", "scFOOBAR_66_S68", 
"scFOOBAR_28_S30", "scFOOBAR_44_S46", "scFOOBAR_32_S34", "scFOOBAR_50_S52", 
"scFOOBAR_54_S56", "scFOOBAR_02_S4", "scFOOBAR_62_S64", "scFOOBAR_69_S71", 
"scFOOBAR_07_S9", "scFOOBAR_59_S61", "scFOOBAR_13_S15", "scFOOBAR_45_S47", 
"scFOOBAR_37_S39", "scFOOBAR_61_S63", "scFOOBAR_42_S44", "scFOOBAR_11_S13", 
"scFOOBAR_58_S60", "scFOOBAR_16_S18", "scFOOBAR_12_S14", "scFOOBAR_39_S41"
), readcount = c(7.5e-05, 0.208259, 0.317617, 0.217022, 0.24163, 
0.178144, 0.203187, 0.326574, 0.46154, 0.241296, 3.8e-05, 0.180657, 
0.296669, 0.2436, 0.372329, 0.154357, 0.332183, 0.100498, 0.110694, 
0.304405, 0.150185, 0.20115, 0.28345, 0.411268, 0.249103, 0.389757, 
0.348236, 0.071293, 5.3e-05, 0.383666, 0.221019, 0.368074, 0.164428, 
0.121094, 0.056566, 0.12801, 0.045516, 0.054762, 2.3e-05, 0.037221, 
0.053614, 0.0308, 0.060173, 0.061752, 0.019005, 0.011073, 0.004948, 
0.00827, 0.011163, 0.010636, 0.017856, 0.019902, 0.021611, 0.010224, 
2.9e-05, 0.015984, 0.011805, 3.1e-05, 0.017305, 0.00265, 0.018211, 
0.010304, 0.011447, 0.033347, 0.011484, 0.015949, 0.042047, 0.005027, 
0.033604, 0.019413, 0.032072, 0.010956, 0.012573, 0.014042, 0.021858, 
0.01491, 0.017772, 0.008882, 0.016791, 0.022836, 0.023896, 0.012391, 
0.026814, 0.011281, 0.015943, 0.01875, 0.010579, 0.017783, 0.019474, 
0.016439, 0.015619, 0.009522, 0.009722, 0.011995)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -94L))

ndat
#> # A tibble: 94 x 2
#>    sample_name     readcount
#>    <chr>               <dbl>
#>  1 scFOOBAR_96_S98  0.000075
#>  2 scFOOBAR_20_S22  0.208   
#>  3 scFOOBAR_83_S85  0.318   
#>  4 scFOOBAR_24_S26  0.217   
#>  5 scFOOBAR_76_S78  0.242   
#>  6 scFOOBAR_72_S74  0.178   
#>  7 scFOOBAR_19_S21  0.203   
#>  8 scFOOBAR_60_S62  0.327   
#>  9 scFOOBAR_18_S20  0.462   
#> 10 scFOOBAR_23_S25  0.241   
#> # ... with 84 more rows

我想做的是做一個累積的情節。 這是我使用的:

ggplot(data = ndat, aes(x = 1:dim(ndat)[1], y = cumsum(readcount))) + 
  geom_line()  + 
  geom_point() +
  theme(axis.text.x = element_text(angle=90, hjust = 1)) +
  scale_x_discrete(labels = ndat$sample_name) +
  ylab("Cumulative read counts (million)") +
  xlab("barcode")

這是我得到的結果:

在此輸入圖像描述

請注意,盡管我的代碼中包含以下行,但x軸刻度標簽已消失: scale_x_discrete(labels = ndat$sample_name)

scFOOBAR_96_S98這樣的文本應該在x軸上顯示為刻度標簽。

制作情節的正確方法是什么?

這是一種方法,我將sample_name為有序因子,以便按表格行的順序而不是按字母順序繪制。

ndat %>% 
  mutate(cuml_read = cumsum(readcount),
         sample_name = fct_reorder(sample_name, row_number())) %>%
  ggplot(aes(x = sample_name, y = cuml_read, group = 1)) + 
  geom_line()  + 
  geom_point() +
  theme(axis.text.x = element_text(angle=90, hjust = 1, size = 6)) +
  ylab("Cumulative read counts (million)") +
  xlab("barcode")

在此輸入圖像描述

編輯:OP注意到在plotly::ggplotly運行繪圖時plotly::ggplotly 這是try的替代方法,它從使用x軸因子切換到帶有從sample_name列獲取的標簽的連續數字刻度。

sample_names <- ndat$sample_name

ndat %>% 
  mutate(cuml_read = cumsum(readcount),
         row = row_number(),
         sample_name = fct_reorder(sample_name, row_number())) %>%
  ggplot(aes(x = row, y = cuml_read, group = 1)) + 
  geom_line()  + 
  geom_point() +
  theme(axis.text.x = element_text(angle=90, hjust = 1, size = 6)) +
  scale_x_continuous(breaks = 1:nrow(ndat), 
                     labels = sample_names) +
  ylab("Cumulative read counts (million)") +
  xlab("barcode")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM