gpt4 book ai didi

r - 堆积条形图 ggplot2-按特定变量从最高值到最低值对样本进行排序

转载 作者:行者123 更新时间:2023-12-03 02:52:00 28 4
gpt4 key购买 nike

我正在尝试以堆叠条形图格式绘制包含超过 2000 个样本的数据集,其中每个样本(由“SampleID”表示)位于 x 轴上,y 轴上有 6 个测量值(测量1-6)。我希望按以下测量变量顺序显示/排序样本:Measurement4、1、5、2、3 和 6,以及从​​最高到最低测量值。下面是 15 个样本的子集,作为我正在使用的示例,我将其称为“dummy_set”数据框:

    SampleID Measurement1 Measurement2 Measurement3 Measurement4 Measurement5 Measurement6
1 A 0.05 0.00 0.95 0.00 0.0 0.00
2 B 0.00 0.00 0.43 0.56 0.0 0.01
3 C 0.64 0.36 0.00 0.00 0.0 0.00
4 D 0.00 0.82 0.18 0.00 0.0 0.00
5 E 0.00 0.60 0.00 0.40 0.0 0.00
6 F 0.80 0.00 0.00 0.20 0.0 0.00
7 G 0.00 0.00 0.00 1.00 0.0 0.00
8 H 0.00 0.00 0.00 1.00 0.0 0.00
9 I 0.00 0.00 1.00 0.00 0.0 0.00
10 J 0.00 0.00 1.00 0.00 0.0 0.00
11 K 0.25 0.00 0.00 0.45 0.3 0.00
12 L 0.10 0.00 0.00 0.10 0.8 0.00
13 M 0.19 0.10 0.00 0.70 0.0 0.01
14 N 0.90 0.00 0.00 0.10 0.0 0.00
15 O 0.00 0.10 0.40 0.00 0.5 0.00

这是我所做的基础知识:

  1. 融合数据集:Melt_dummy_set <- Melt(dummy_set, id.var = "SampleID")

    融化的数据集如下所示:

    head(melt_dummy_set)
    SampleID variable value
    1 A Measurement1 0.05
    2 B Measurement1 0.00
    3 C Measurement1 0.64
    4 D Measurement1 0.00
    5 E Measurement1 0.00
    6 F Measurement1 0.80
  2. 使用 ggplot() 和 geom_bar() 绘制融化的数据集:

    ggplot(melt_dummy_set, aes(x = SampleID, y = value, fill = variable)) + 
    geom_bar(stat = "identity") +

Original stacked bar chart

如您所见,样本按其列出的原始顺序 (A-O) 绘制。但是,我希望它们按以下顺序绘制:G、H、M、B、K、N、F、C、L、O、D、E、I、J 和 A。

根据其他类似的 Stack Overflow 问题,我发现我需要按照我想要的顺序重新调整/重新建立因素。这是我到目前为止所尝试过的:

#Attempt 1
reordered_melt_dummy_set <- transform(melt_dummy_set, variable = reorder(variable, -value))
ggplot(reordered_melt_dummy_set, aes(x = SampleID, y = value, fill = variable)) +
geom_bar(stat = "identity") +

Attempt 1 results

#Attempt 2
copy_melt_dummy_set <- melt_dummy_set
copy_melt_dummy_set$variable <- factor(copy_melt_dummy_set$variable, levels = c("Measurement4", "Measurement5", "Measurement1", "Measurement2", "Measurement3", "Measurement6"))
ggplot(copy_melt_dummy_set, aes(x = SampleID, y = value, fill = variable)) +
geom_bar(stat = "identity") +

Attempt 2 results

我的第三次尝试导致了多个错误(在代码行后面以“##”表示)

#Attempt 3
copy2_melt_dummy_set <- melt_dummy_set

copy2_melt_dummy_set$SampleID <- factor(copy2_melt_dummy_set$SampleID, levels = copy2_melt_dummy_set[order(-copy2_melt_dummy_set$value), "variable"])
##Error in `levels<-`(`*tmp*`, value = as.character(levels)) : factor level [2] is duplicated

copy2_melt_dummy_set$variable <- factor(copy2_melt_dummy_set$variable, levels = copy2_melt_dummy_set[order(copy2_melt_dummy_set$value), "variable"])
## Error in `levels<-`(`*tmp*`, value = as.character(levels)) : factor level [2] is duplicated

copy2_melt_dummy_set$SampleID <- factor(copy2_melt_dummy_set$SampleID, levels = copy2_melt_dummy_set[order(-copy2_melt_dummy_set$variable), "SampleID"])
## Error in `levels<-`(`*tmp*`, value = as.character(levels)) : factor level [16] is duplicated
## In addition: Warning message: In Ops.factor(copy2_melt_dummy_set$variable) : ‘-’ not meaningful for factors

copy2_melt_dummy_set$SampleID <- factor(copy2_melt_dummy_set$SampleID, levels = copy2_melt_dummy_set[order(-copy2_melt_dummy_set$value), "SampleID"])
## Error in `levels<-`(`*tmp*`, value = as.character(levels)) : factor level [16] is duplicated

copy2_melt_dummy_set$SampleID <- factor(copy2_melt_dummy_set$SampleID, levels = copy2_melt_dummy_set[order(-copy2_melt_dummy_set$value), "value"])
## Error in `levels<-`(`*tmp*`, value = as.character(levels)) : factor level [2] is duplicated
#Attempt 4
copy3_melt_dummy_set <- melt_dummy_set[order(melt_dummy_set$variable, -melt_dummy_set$value), ]
head(copy3_melt_dummy_set)
ggplot(copy3_melt_dummy_set, aes(x = SampleID, y = value, fill = variable)) +
geom_bar(stat = "identity") +

Attempt 4 results

#Attempt 5
ggplot(melt_dummy_set[order(melt_dummy_set$variable, -melt_dummy_set$value), ], aes(x = SampleID, y = value, fill = variable)) +
geom_bar(stat = "identity") +

Attempt 5 results

#Attempt 6
new_melt_dummy_set <- within(melt_dummy_set,
variable <- factor(variable, levels = names(sort(table(variable), decreasing = TRUE))))
ggplot(new_melt_dummy_set, aes(x = SampleID, y = value, fill = variable)) +
geom_bar(stat = "identity") +

Attempt 6 results

#Attempt 7
copy4_melt_dummy_set <- melt_dummy_set
custom_leveling <- unique(copy4_melt_dummy_set$variable)
copy4_melt_dummy_set$variable <- factor(copy4_melt_dummy_set$variable, level = custom_leveling)
ggplot(copy4_melt_dummy_set, aes(x = SampleID, y = value, fill = variable)) +
geom_bar(stat = "identity") +

Attempt 7 results

在所有情况下,我都无法重新组织 x 轴上的实际样本。我觉得可能有一个简单的解决办法,但我不知道我做错了什么。有什么建议吗?

已编辑

为了回应可能的重复评论,我尝试应用 Order Bars in ggplot2 bar graph 中的代码/解决方案他们没有按照我想要的顺序制作情节。请参阅下面的我尝试过的代码:

#First solution
new_melt_dummy_set <- within(melt_dummy_set,
variable <- factor(variable, levels = names(sort(table(variable), decreasing = TRUE))))
ggplot(new_melt_dummy_set, aes(x = SampleID, y = value, fill = variable)) +
geom_bar(stat = "identity")

#Second solution
ggplot(melt_dummy_set, aes(x = reorder(SampleID, variable, function(x)-length(x)), y = value, fill = variable)) + geom_bar(stat = "identity")
ggplot(melt_dummy_set, aes(x = reorder(variable, SampleID, function(x)-length(x)), y = value, fill = variable)) + geom_bar(stat = "identity")

#Third solution
ordered_measurements <- c("Measurement4", "Measurement1", "Measurement5", "Measurement2", "Measurement3", "Measurement6")
ggplot(melt_dummy_set, aes(x = SampleID, y = value, fill = variable)) +
geom_bar(stat = "identity") +
scale_x_discrete(limits = ordered_measurements)

#Fourth solution
ggplot(melt_dummy_set, aes(x = reorder(SampleID, -table(variable)[variable]), y = value, fill = variable)) + geom_bar(stat = "identity")

require(forcats)
ggplot(melt_dummy_set, aes(x = SampleID, fill = fct_infreq(variable), y = value)) + geom_bar(stat = "identity")
ggplot(melt_dummy_set, aes(x = fct_infreq(variable))) + geom_bar(stat = "identity")

#Fifth solution
library(tidyverse)
library(forcats)
melt_dummy_set %>%
mutate(variable = fct_reorder(variable, value, .desc = TRUE)) %>%
ggplot(aes(x = SampleID, y = value, fill = variable)) + geom_bar(stat = 'identity')

#Sixth solution
library(dplyr)
melt_dummy_set %>%
group_by(variable) %>%
summarize(counts = n()) %>%
arrange(-counts) %>%
mutate(SampleID = factor(SampleID, variable)) %>%
ggplot(aes(x = SampleID, y = value, fill = variable)) +
geom_bar(stat = "identity")

melt_dummy_set %>%
group_by(SampleID) %>%
summarize(counts = n()) %>%
arrange(-counts) %>%
mutate(SampleID = factor(SampleID, value)) %>%
ggplot(aes(x = SampleID, y = value, fill = variable)) +
geom_bar(stat = "identity")

#Seventh solution
new_meltedDummy_set <- transform(melt_dummy_set,
variable = ordered(variable, levels = names(sort(-table(variable)))))
ggplot(new_meltedDummy_set, aes(x = SampleID, y = value, fill = variable)) +
geom_bar(stat = "identity")

最佳答案

这就是你的目的吗?我想你很接近。您不需要将 Measurement 变量列转换为因子,而是需要根据 Measurement 值的顺序对 SampleID 列进行排序。这是计算 sample_order 的行中发生的情况:

library(tidyverse)

dummy_set <- tibble(
SampleID = c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O"),
Measurement1 = c(0.05, 0, 0.64, 0, 0, 0.8, 0, 0, 0, 0, 0.25, 0.1, 0.19, 0.9, 0),
Measurement2 = c(0, 0, 0.36, 0.82, 0.6, 0, 0, 0, 0, 0, 0, 0, 0.1, 0, 0.1),
Measurement3 = c(0.95, 0.43, 0, 0.18, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0.4),
Measurement4 = c(0, 0.56, 0, 0, 0.4, 0.2, 1, 1, 0, 0, 0.45, 0.1, 0.7, 0.1, 0),
Measurement5 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.3, 0.8, 0, 0, 0.5),
Measurement6 = c(0, 0.01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.01, 0, 0)
)

sample_order <- dummy_set %>%
arrange(desc(Measurement4), desc(Measurement1), desc(Measurement5), desc(Measurement2), desc(Measurement3), desc(Measurement6)) %>%
pull(SampleID)

melt_dummy_set <- dummy_set %>%
gather(variable, value, -SampleID)

reordered_melt_dummy_set <- melt_dummy_set %>%
mutate(SampleID = factor(SampleID, levels = sample_order))

plot_ordered <- ggplot(reordered_melt_dummy_set, aes(x = SampleID, y = value, fill = variable)) +
geom_bar(stat = "identity") +
scale_y_continuous(expand = c(0,0)) +
theme(axis.ticks.x = element_blank(), panel.grid = element_blank(), axis.line = element_line(color = "black"), panel.border = element_blank(), panel.background = element_blank())

plot_ordered

reprex package于2019年7月26日创建(v0.3.0)

关于r - 堆积条形图 ggplot2-按特定变量从最高值到最低值对样本进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57224334/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com