作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有几个实验的数据,这些数据给出了我想用堆积条形图表示的样本中物种 DNA 的比例。示例数据是
sample_df <- structure(list(sample = c("R17108_BSSE_QGF_132757_HHWVVDRXX_1_G44937_CCGTGAAG_CAGTGGAT_S29_L001",
"R17108_BSSE_QGF_132757_HHWVVDRXX_1_G44937_CCGTGAAG_CAGTGGAT_S29_L001",
"R17108_BSSE_QGF_132757_HHWVVDRXX_1_G44937_CCGTGAAG_CAGTGGAT_S29_L001",
"R18676", "R18676", "R23399_COW6673A59_S33", "R23399_COW6673A59_S33",
"R23464_COW5599A32_S33", "R23464_COW5599A32_S33", "R23464_COW5599A32_S33",
"R24033_concatreseq", "R24033_concatreseq", "R24033_concatreseq",
"R24033_concatreseq", "R24033_concatreseq", "R24033_concatreseq",
"R24033_concatreseq", "R24033", "R24033", "R24033", "R24033",
"R24033", "R24033", "R30216_concatreseq", "R30216_concatreseq",
"R30216", "R30216", "R31417_concatreseq", "R31417", "R32064",
"R32064", "R32064", "R32064", "R4752_BSSE_QGF_132888_HHWVVDRXX_1_G45072_CAGGAGCC_GTCCAATC_S159_L001",
"R4752_BSSE_QGF_132888_HHWVVDRXX_1_G45072_CAGGAGCC_GTCCAATC_S159_L001",
"R4752_BSSE_QGF_132888_HHWVVDRXX_1_G45072_CAGGAGCC_GTCCAATC_S159_L001",
"R4752_BSSE_QGF_132888_HHWVVDRXX_1_G45072_CAGGAGCC_GTCCAATC_S159_L001",
"R4752_BSSE_QGF_132888_HHWVVDRXX_1_G45072_CAGGAGCC_GTCCAATC_S159_L001",
"R4775_LFO46Pool105_3311__L5_ACCACTGT_L005", "R4775_LFO46Pool105_3311__L5_ACCACTGT_L005"
), name = c("Microbacterium sp. LKL04", "Microbacterium oleivorans",
"Mycobacterium tuberculosis", "Staphylococcus cohnii", "Mycobacterium tuberculosis",
"Paraburkholderia fungorum", "Paraburkholderia xenovorans", "Paraburkholderia fungorum",
"Paraburkholderia aromaticivorans", "Paraburkholderia xenovorans",
"Bacillus safensis", "Bacillus sp. WP8", "Bacillus sp. PAMC28571",
"Bacillus sp. PAMC22265", "Bacillus pumilus", "Bacillus altitudinis",
"Bacillus subtilis", "Bacillus safensis", "Bacillus sp. WP8",
"Bacillus sp. PAMC28571", "Bacillus sp. PAMC22265", "Bacillus pumilus",
"Bacillus altitudinis", "Mycobacterium avium", "Mycobacterium tuberculosis",
"Mycobacterium avium", "Mycobacterium tuberculosis", "Mycobacterium avium",
"Mycobacterium avium", "Staphylococcus aureus", "Paenibacillus sp. 32O-W",
"Mycobacterium tuberculosis", "Homo sapiens", "Ralstonia pickettii",
"Ralstonia mannitolilytica", "Ralstonia insidiosa", "Ralstonia solanacearum",
"Mycobacterium tuberculosis", "Paenibacillus naphthalenovorans",
"Paenibacillus sp. B01"), fraction_total_reads = c(0.29347, 0.09071,
0.46242, 0.6525, 0.32403, 0.92541, 0.01772, 0.8842, 0.04011,
0.01561, 0.72733, 0.02744, 0.11121, 0.02673, 0.03845, 0.02282,
0.01176, 0.73674, 0.02711, 0.12122, 0.01858, 0.03677, 0.02115,
0.97964, 0.01579, 0.98397, 0.01227, 0.9907, 0.99348, 0.43967,
0.01337, 0.4288, 0.02825, 0.54439, 0.08077, 0.05916, 0.01978,
0.23135, 0.70247, 0.02424)), row.names = c(NA, -40L), class = c("tbl_df",
"tbl", "data.frame"))
然后像这样绘制
ggplot(data = sample_df, aes(fill=name, y=fraction_total_reads, x=sample)) +
geom_bar(position="stack", stat="identity", color="black") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
要得到
sample_df2 <- sample_df %>%
mutate(is_mtb = ifelse(name == "Mycobacterium tuberculosis", TRUE, FALSE))
然后用
ggplot(data = sample_df2, aes(fill=is_mtb, y=fraction_total_reads, x=sample)) +
geom_bar(position="stack", stat="identity") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
最佳答案
你可以这样处理它:
library(ggplot2)
# define default colours for each name
mynames <- unique(sample_df$name)
mycolours <- scales::hue_pal()(length(mynames))
mycolours <- setNames(mycolours, mynames)
# set up custom colour!
mycolours["Mycobacterium tuberculosis"] <- "red"
# your plot
ggplot(data = sample_df, aes(fill=name, y=fraction_total_reads, x=sample)) +
geom_bar(position="stack", stat="identity", color="black") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
# add this!
scale_fill_manual(values = mycolours)
library(ggplot2)
mynames <- unique(sample_df$name)
myfills <- scales::hue_pal()(length(mynames))
myfills <- setNames(myfills, mynames)
myfills["Mycobacterium tuberculosis"] <- "red"
mycolours <- rep("black", length(mynames))
mycolours <- setNames(mycolours, mynames)
mycolours["Mycobacterium tuberculosis"] <- "red"
myalphas <- rep(0.6, length(mynames))
myalphas <- setNames(myalphas, mynames)
myalphas["Mycobacterium tuberculosis"] <- 1
ggplot(data = sample_df, aes(y = fraction_total_reads,
x = sample,
alpha = name,
colour = name,
fill = name)) +
geom_bar(position = "stack", stat = "identity") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) +
scale_fill_manual (values = myfills ) +
scale_colour_manual(values = mycolours) +
scale_alpha_manual (values = myalphas )
关于r - 突出显示 ggplot 堆积条形图中的一个因素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64006625/
有没有一种方法可以“标记”对象的属性,使它们在反射中“突出”? 例如: class A { int aa, b; string s1, s2; public int AA
我是一名优秀的程序员,十分优秀!