如何在ggplot累积图的x轴上显示刻度标签

时间:2019-02-20 05:23:12

标签: r ggplot2

我有以下数据框。它由两列和九十四行组成。

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")

这是我得到的结果:

enter image description here

请注意,尽管我的代码scale_x_discrete(labels = ndat$sample_name)中包含以下行,但x轴刻度标签已消失。

scFOOBAR_96_S98之类的文字应在x轴上显示为刻度标签。

制作情节的正确方法是什么?

1 个答案:

答案 0 :(得分:3)

在这种方法中,我将import { Platform } from 'ionic-angular'; import { AppVersion } from "@ionic-native/app-version"; constructor(private platform: Platform, private appVersion: AppVersion) { this.platform.ready().then(function () { if (platform.is('cordova')) { this.appVersion.getVersionNumber().then(ver => { this.appVerStr = ver; }); } }); } 设为有序因子,以便它按表行的顺序而不是按字母顺序绘制。

sample_name

enter image description here

编辑:OP注意到在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") 中运行绘图存在问题。这是一种尝试的替代方法,它从使用x轴因子转换为带有从sample_name列中获取标签的连续数字刻度。

plotly::ggplotly