R语言 生成林图时ggplot出错,导致NULL

xkrw2x1b  于 2022-12-25  发布在  其他
关注(0)|答案(1)|浏览(278)

尝试使用ggplot2在R中创建森林图,但遇到一些错误。

    • 数据**:
structure(list(explanatory_variables = structure(1:15, levels = c("Age", 
"Disease duration", "Smoking", "BMI", "HLA-B27", "Uveitis", "TNFi start year", 
"csDMARD", "CRP", "Psoriasis", "Arthritis", "NSAID", "Enthesitis", 
"TNFi type", "IBD"), class = "factor"), unadj_coef = c(0.93, 
0.92, 1.13, 1, 1.02, 1.32, 0.93, 0.83, 0.76, 1.42, 1.77, 0.04, 
1.26, 0.93, 1.3), adj_coef = c(0.91, 0.91, 1.13, 1, 1.02, 1.33, 
0.92, 0.83, 0.76, 1.42, 1.77, 0.04, 1.26, 0.93, 1.3), pct_change = c(-1.8, 
-0.9, 0.6, -0.5, 0.4, 0.3, -0.2, -0.2, 0.1, 0.1, 0.1, -0.1, -0.1, 
0, 0)), row.names = c(NA, -15L), class = "data.frame")

到目前为止我尝试过的代码

# Sort data.frame by pct change
basdai_data <- basdai_data[order(abs(basdai_data$pct_change), decreasing = TRUE),]
basdai_data

# Reorder levels of explanatory variable
basdai_data$explanatory_variables  <- factor(basdai_data$explanatory_variables , levels = as.character(basdai_data$explanatory_variables ))
breaks <- as.character(levels(basdai_data$explanatory_variables))
breaks
basdai_data$explanatory_variables

# Create forest plot
ggplot(basdai_data, aes(x = pct_change, y = explanatory_variables)) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  scale_y_discrete(breaks = breaks, labels = breaks) +
  scale_x_continuous(breaks = seq(-3, 3, 1)) +
  labs(x = "Percentage change", y = "Explanatory variable") +
  theme_pubr() +
  theme(text = element_text(size = 15, family = "Calibri"),
        axis.title = element_text(size = 20))

这是我的错误:

"Error in ans[ypos] <- rep(yes, length.out = len)[ypos] : replacement has length zero
In addition: Warning message:
In rep(yes, length.out = len) : 'x' is NULL so the result will be NULL"

我的期望是一个森林图,y轴的左边是解释变量的名称,x轴是百分比变化,0是一条垂直虚线,然而,我收到的却是上面的错误。
我的问题其实有两个:我如何修正错误,作为奖励,是否可以在森林图之前添加4列数据("explantic_variable"、"unadjusted coef."、"adjusted coef."、"pct. change")?这将是完美的。数据也存在于数据集中。

    • 已编辑**:我想向Allan显示我的新数据、帧和代码,因为我收到错误"Error:离散值提供给连续规模"时,将他的新代码应用到p2。我添加了一个额外的列称为"交互"。p1工作正常,但p2不是。
#Analysis of level 3 data
basdai_level3 <- structure(list(explanatory_variables = c("Age", "TNFi type", 
                                                          "TNFi start year", "CRP", "Smoking", "Disease duration", "HLA-B27", 
                                                          "NSAID", "csDMARD", "BMI", "Uveitis", "Psoriasis", "IBD", "Arthritis", 
                                                          "Enthesitis"), ID = c(11753, 11753, 11753, 8661, 9991, 8063, 
                                                                                7074, 5204, 6776, 6585, 5635, 5541, 5601, 3955, 4903), 
                                countries = c(15, 15, 15, 13, 14, 11, 14, 10, 15, 13, 10, 10, 10, 9, 9), 
                                unadj_coef = c(0.67, 0.67, 0.67, 0.38, 0.84, 0.59, 0.78, -0.2, 0.66, 0.75, 1.1, 1.2, 1.08, 1.56, 1.03), 
                                adj_coef = c(0.61, 0.67, 0.66, 0.38, 0.86, 0.56, 0.79, -0.2, 0.65, 0.74, 1.12, 1.2, 1.08, 1.57, 1.03), 
                                change = c(-0.06, 0, 0, 0, 0.02, -0.03, 0.02, 0, -0.01, -0.01, 0.02, 0, 0, 0, 0), 
                                pct_change = c(-8.9, 0.4, -0.4, 1.2, 2.3, -4.7, 2.3, -0.4, -1.3, -1.4, 1.4, 0.4, -0.1, 0.2, -0.4), 
                                unadj_coef_pvalue = c(0.037, 0.037, 0.037, 0.319, 0.016, 0.138, 0.054, 0.689, 0.125, 0.072, 0.017, 0.01, 0.02, 0.004, 0.039), 
                                adj_coef_pvalue = c(0.058, 0.037, 0.038, 0.314, 0.013, 0.158, 0.048, 0.69, 0.13, 0.076, 0.016, 0.01, 0.02, 0.003, 0.039), 
                                interaction = c(NA, NA, NA, NA, NA, NA, -2.46, NA, NA, NA, NA, 5.07, NA, NA, NA)), row.names = c(NA, 15L), class = "data.frame")

#Sort data
basdai_level3 <- basdai_level3[order((basdai_level3$pct_change), decreasing = TRUE),]
basdai_level3

# Reorder levels of explanatory variable
basdai_level3$explanatory_variables  <- factor(basdai_level3$explanatory_variables , levels = as.character(basdai_level3$explanatory_variables ))
breaks <- as.character(levels(basdai_level3$explanatory_variables))

p2 <- ggplot(basdai_level3, aes(x = 1, y = explanatory_variables)) +
  geom_hline(yintercept = 2 * 1:8 - 1, linewidth = 13, color = "gray92") +
  geom_text(aes(label = explanatory_variables), hjust = 0) +
  geom_text(aes(x = 3, label = unadj_coef)) +
  geom_text(aes(x = 4, label = adj_coef)) +
  geom_text(aes(x = 5, label = pct_change)) +
  geom_text(aes(x = 6, label = interaction)) +
  scale_x_continuous(NULL, breaks = c(1, 3, 4, 5, 6), limits = c(1, 7),
                     labels = c('Variable', 'Unadj. coef.', 'Adj. coef.',
                                'Change (%)', 'Interaction'), position = 'top') +
  theme_void() +
  theme(axis.text.x.top = element_text(hjust = c(0, 0.5, 0.5, 0.5),
                                       face = 2,  family = "Calibri"))
yeotifhr

yeotifhr1#

您的百分比更改未按指定顺序排列,因为您是按百分比更改的 * 绝对 * 值排序的。您只需执行以下操作:

basdai_data <- basdai_data[order(basdai_data$pct_change, decreasing = TRUE),]

出现此错误只是因为未包含任何数据层。如果添加geom_point,则结果为:

ggplot(basdai_data, aes(x = pct_change, y = explanatory_variables)) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  geom_point() +
  scale_y_discrete(breaks = breaks, labels = breaks) +
  scale_x_continuous(breaks = seq(-3, 3, 1)) +
  labs(x = "Percentage change", y = "Explanatory variable") +
  theme_pubr() +
  theme(text = element_text(size = 15, family = "Calibri"),
        axis.title = element_text(size = 20))

要在图的左侧获取表,您可以执行以下操作:

p1 <- ggplot(basdai_data, aes(x = pct_change, y = explanatory_variables)) +
  geom_hline(yintercept = 2 * 1:8 - 1, linewidth = 13, color = "gray92") +
  geom_vline(xintercept = 0, linetype = "dashed") +
  geom_point() +
  scale_y_discrete(breaks = breaks, labels = breaks) +
  scale_x_continuous(breaks = seq(-3, 3, 1)) +
  labs(x = "Percentage change", y = "Explanatory variable") +
  theme_pubr() +
  theme(text = element_text(size = 15, family = "Calibri"),
        axis.title = element_text(size = 20),
        axis.line.y = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank())

p2 <- ggplot(basdai_data, aes(x = 1, y = explanatory_variables)) +
  geom_hline(yintercept = 2 * 1:8 - 1, linewidth = 13, color = "gray92") +
  geom_text(aes(label = explanatory_variables), hjust = 0) +
  geom_text(aes(x = 3, label = unadj_coef)) +
  geom_text(aes(x = 4, label = adj_coef)) +
  geom_text(aes(x = 5, label = pct_change)) +
  scale_y_discrete(breaks = breaks, labels = breaks) +
  scale_x_continuous(NULL, breaks = c(1, 3, 4, 5), limits = c(1, 5.5),
                     labels = c('Variable', 'Unadjusted coef', 'Adj. coef',
                                '% change'), position = 'top') +
  theme_void() +
  theme(axis.text.x.top = element_text(hjust = c(0, 0.5, 0.5, 0.5),
                                       face = 2))

library(patchwork)

p2 + p1

相关问题