首页 > 解决方案 > 如何在 R 中选择性地为直方图条着色?

问题描述

我做了 10,000 个模型的随机实现,并想绘制一些输出。长话短说,该情节显示了两个物种在给定运行中在系统内共存的时间。这是一个例子: 在此处输入图像描述

现在,我想对直方图条进行着色,以指定哪些物种在系统中持续时间更长。具体来说,我希望能够在给定的模拟中指定物种 A 是否比物种 B 寿命更长,物种 B 是否比物种 A 寿命更长,或者两个物种是否同时“灭绝”。

因此,我为这三个结果中的每一个创建了三个向量,其中包含运行数(共 10,000 个)。例如:

# Pi wins
PiWins_1 <- which(Winner_1[1:10000, 2] %in% TRUE)
head(PiWins_1)
# [1]  1  2  6  7  9 12

# Pj wins
PjWins_1 <- which(Winner_1[1:10000, 3] %in% TRUE)
head(PjWins_1)
# [1]  3  4  5  8 10 11

# Ties
Ties_1 <- which(Winner_1[1:10000, 4] %in% TRUE)
head(Ties_1)
# [1]  20  24  29  40 110 132

现在我想弄清楚如何使用这些(或其他方法)相应地为 10,000 个直方图条中的每一个着色?

这是到目前为止的 ggplot 脚本:

Histogram_1 <- ggplot(temp_df_1, aes(x=persistance_vec_1))+
  geom_histogram(binwidth = 1, fill = "darkseagreen2")+
  geom_vline(aes(xintercept = mean_pv_1, colour = "Mean # of Overlapping Time Pts in Stoch Runs"))+
  geom_vline(aes(xintercept = cmean_pv_1, colour = "Conditional Mean # of Overlapping Time Pts in Stoch Runs (Dashed)"), linetype=2)+
  geom_vline(aes(xintercept = median_pv_1, colour = "Median # of Overlapping Time Pts in Stoch Runs"))+
  geom_vline(aes(xintercept = cmedian_pv_1, colour = "Conditional Median # of Overlapping Time Pts in Stoch Runs (Dashed)"), linetype=2)+
  geom_vline(aes(xintercept = DO_1, colour = "Overlap in Det Sim"))+
  labs(title="Det vs Stoch Overlap; Intro @ 1, 10000 sims",
       x="# of Time Pts Pi and Pj Co-occur",
       y="# of Sims")+
  scale_colour_manual(name="Legend",
                      values=c("Mean # of Overlapping Time Pts in Stoch Runs" = "navyblue",
                               "Conditional Mean # of Overlapping Time Pts in Stoch Runs (Dashed)" = "navyblue",
                               "Median # of Overlapping Time Pts in Stoch Runs" = "red2",
                               "Conditional Median # of Overlapping Time Pts in Stoch Runs (Dashed)" = "red2",
                               "Overlap in Det Sim" = "orange"))+
  theme_minimal()
Histogram_1

如果我应该提供更多信息,请告诉我!非常感谢您的任何建议:)

标签: rggplot2

解决方案


好的,所以我认为您所要求的只是如何用不同的颜色为条形上色。这是一个代表:

x =  c(1:10)
y = c(11:15, 15:11)
z = sample(c("A", "B", "C"), 10, replace = TRUE)
df = data.frame(x = x, y = y, z = z)

然后是情节 -如果你想使用 ggplot,你可以只使用fill参数 in 。geom_col()

library(ggplot2)
df$z = as.factor(df$z)
ggplot(data = df) +
  geom_col(aes(x = x, y = y, fill = z), colour = "black")

在此处输入图像描述

这会做你想做的事吗?

或者,如果您真的想使用geom_histogram(),这里有一个带有不同表示的选项:

# make the numeric data
x = sample(c(1:10), 50, replace = TRUE)
# initialize empty vector for categorical variables
z = vector(mode = "character", length = 50)
# im making 3 groups that i've randomly decided to group
for(i in 1:length(x)){
  z[i] = ifelse(x[i] %in% c(1,3,5,7,9), # if x is an odd number, z=A
         "A",
         ifelse(x[i] %in% c(2,4,6), # if x in (2,4,6), z=B, if not, z=C
                "B", "C"))
}
#turn into dataframe
df = data.frame(x = x, z = z)

然后情节使用相同的想法:

df$z = as.factor(df$z)
ggplot(data = df) +
  geom_histogram(aes(x = x, fill = z), colour = "black")

在此处输入图像描述


推荐阅读