矩阵R从数字转换为csv中提供的字符
matrix R convert from numeric to characters provided in csv
我刚开始使用 R 并学习如何使用
森林图 函数。
我正在努力使用以下方法标记森林图的 y 轴:
labeltext = matrix()
当我使用这个时:
labeltext = matrix(c(data$BCT, data$present), nrows=40, ncol=2, byrow=TRUE, dimnames=NULL)
我得到一个数字矩阵,R 忽略了我提供的 scv 文件中的文本。
当我为一列制作矩阵时,它似乎起作用了。
如何使用 csv 文件中的两列标记 y 轴?
我想实现这样的目标:
但是 'reference'、'lact' 和 'diet' 应该是 'BCT' 和 'present'。
我想要一个以 'BCT'(第 1 列)和 'present'(第 2 列)作为 y 轴标签的森林图。 SMD是效果量度,upper_limit和lower_limit是CI。请参阅下面的示例数据
代码如下:
library(forestplot)
labeltext = matrix(c(data$BCT, data$present), nrow = 40, ncol = 2, byrow = TRUE, dimnames = NULL)
is_summary = FALSE
forestplot(labeltext,
graph.pos = 2,
mean = data$SMD,
lower = data$"lower_limit",
upper = data$"upper_limit",
is.summary = is_summary,
xlab = "<-------Comparator Better---------------Main Intervention Better-------->",
line.margin = unit (0.8, "cm"),
lineheight = unit (0.5, "cm"),
hrlz.lines =TRUE,
col = fpColors(box = "darkred", lines = "grey", summary = "black"),
graphwidth = unit (10, "cm"))
# Sample Input Data
data <- structure(list(
BCT = c("Adding objects to the environment (12.5) ", "Adding objects to the environment (12.5) ", "Action planning (1.4)", "Action planning (1.4)", "Body changes (12.6) ", "Body changes (12.6) ", "Behavioural practice/rehearsal (8.1)", "Behavioural practice/rehearsal (8.1)", "Credible source (9.2)", "Credible source (9.2)", "Demonstration of the behaviour (6.1) ", "Demonstration of the behaviour (6.1) ", "Feedback on behaviour (2.2) ", "Feedback on behaviour (2.2) ", "Goal setting (behaviour) (1.1)", "Goal setting (behaviour) (1.1)", "Graded tasks (8.7)", "Graded tasks (8.7)", "Generalisation of target behaviour (8.6) ", "Generalisation of target behaviour (8.6) ", "Information about health consequences (5.1)", "Information about health consequences (5.1)", "Instruction on how to perform the behaviour (4.1)", "Instruction on how to perform the behaviour (4.1)", "Monitoring of behaviour by others without feedback (2.1)", "Monitoring of behaviour by others without feedback (2.1)", "Monitoring of outcomes of behaviour without feedback (2.5)",
"Monitoring of outcomes of behaviour without feedback (2.5)", "Problem solving (1.2) ", "Problem solving (1.2) ", "Social Comparison (6.2)", "Social Comparison (6.2)", "Self-monitoring of behaviour (2.3) ", "Self-monitoring of behaviour (2.3) ", "Self-monitoring of outcome(s) of behaviour (2.4) ", "Self-monitoring of outcome(s) of behaviour (2.4) ", "Social support (emotional) (3.3)", "Social support (emotional) (3.3)", "Social support (unspecified) (3.1) ", "Social support (unspecified) (3.1) "),
present = c("present ", "not present", "present ", "not present", "present ", "not present", "present ", "not present", "present ", "not present", "present", "not present", "present ", "not present", "present ", "not present", "present ", "not present", "present ", "not present", "present ", "not present", "present ", "not present", "present ", "not present", "present ", "not present", "present ", "not present", "present ", "not present", "present ", "not present", "present ", "not present", "present ", "not present", "present ", "not present"),
SMD = c(1.69, 0.42, 0.78, 0.4, 0.36, 0.57, 0.71, 0.4, 2.23, 0.31, 0.32, 0.63, 0.45, 0.58, 0.79, 0.26, 0.55, 0.56, 1.39, 0.4, 0.6, 0.46, 0.7, 0.34, 2.53, 0.47, 0.36, 0.62, 0.2, 0.7, 0.03, 0.66, 0.68, 0.47, 1.14, 0.4, 1.13, 0.44, 0.67, 0.52),
lower_limit = c(-1.49, 0.22, -0.4, 0.06, 0.08, -0.33, -0.34, -0.04, 0.15, 0.2, 0.14, -0.4, -0.25, -0.26, -0.19, 0.11, -0.66, 0.23, -0.68, 0.19, -0.35, 0.11, -0.46, 0.15, 1.36, 0.05, -0.07, -0.2, -0.01, -0.18, -0.42, -0.14, -0.4, 0.06, -0.84, 0.16, -1.47, 0.24, -0.93, 0.27),
upper_limit = c(4.87, 0.62, 1.95, 0.74, 0.65, 1.48, 1.75, 0.84, 4.32, 0.42, 0.51, 1.67, 1.15, 1.43, 1.77, 0.41, 1.77, 0.89, 3.46, 0.6, 1.55, 0.81, 1.87, 0.53, 3.7, 0.89, 0.78, 1.43, 0.41, 1.58, 0.47, 1.46, 1.77, 0.88, 3.12, 0.64, 3.72, 0.65, 2.28, 0.78)),
.Names = c("BCT", "present", "SMD", "lower_limit", "upper_limit"),
row.names = c(NA, -40L), class = "data.frame")
很可能您导入了您的角色数据作为因素。当您使用 c()
和 matrix()
等简化函数重塑因子时,它们会转换为数值,因为矩阵不能包含因子。更仔细的转换是
labeltext = as.matrix(data.frame(data$BCT, data$present))
我刚开始使用 R 并学习如何使用 森林图 函数。
我正在努力使用以下方法标记森林图的 y 轴:
labeltext = matrix()
当我使用这个时:
labeltext = matrix(c(data$BCT, data$present), nrows=40, ncol=2, byrow=TRUE, dimnames=NULL)
我得到一个数字矩阵,R 忽略了我提供的 scv 文件中的文本。
当我为一列制作矩阵时,它似乎起作用了。 如何使用 csv 文件中的两列标记 y 轴?
我想实现这样的目标:
但是 'reference'、'lact' 和 'diet' 应该是 'BCT' 和 'present'。
我想要一个以 'BCT'(第 1 列)和 'present'(第 2 列)作为 y 轴标签的森林图。 SMD是效果量度,upper_limit和lower_limit是CI。请参阅下面的示例数据
代码如下:
library(forestplot)
labeltext = matrix(c(data$BCT, data$present), nrow = 40, ncol = 2, byrow = TRUE, dimnames = NULL)
is_summary = FALSE
forestplot(labeltext,
graph.pos = 2,
mean = data$SMD,
lower = data$"lower_limit",
upper = data$"upper_limit",
is.summary = is_summary,
xlab = "<-------Comparator Better---------------Main Intervention Better-------->",
line.margin = unit (0.8, "cm"),
lineheight = unit (0.5, "cm"),
hrlz.lines =TRUE,
col = fpColors(box = "darkred", lines = "grey", summary = "black"),
graphwidth = unit (10, "cm"))
# Sample Input Data
data <- structure(list(
BCT = c("Adding objects to the environment (12.5) ", "Adding objects to the environment (12.5) ", "Action planning (1.4)", "Action planning (1.4)", "Body changes (12.6) ", "Body changes (12.6) ", "Behavioural practice/rehearsal (8.1)", "Behavioural practice/rehearsal (8.1)", "Credible source (9.2)", "Credible source (9.2)", "Demonstration of the behaviour (6.1) ", "Demonstration of the behaviour (6.1) ", "Feedback on behaviour (2.2) ", "Feedback on behaviour (2.2) ", "Goal setting (behaviour) (1.1)", "Goal setting (behaviour) (1.1)", "Graded tasks (8.7)", "Graded tasks (8.7)", "Generalisation of target behaviour (8.6) ", "Generalisation of target behaviour (8.6) ", "Information about health consequences (5.1)", "Information about health consequences (5.1)", "Instruction on how to perform the behaviour (4.1)", "Instruction on how to perform the behaviour (4.1)", "Monitoring of behaviour by others without feedback (2.1)", "Monitoring of behaviour by others without feedback (2.1)", "Monitoring of outcomes of behaviour without feedback (2.5)",
"Monitoring of outcomes of behaviour without feedback (2.5)", "Problem solving (1.2) ", "Problem solving (1.2) ", "Social Comparison (6.2)", "Social Comparison (6.2)", "Self-monitoring of behaviour (2.3) ", "Self-monitoring of behaviour (2.3) ", "Self-monitoring of outcome(s) of behaviour (2.4) ", "Self-monitoring of outcome(s) of behaviour (2.4) ", "Social support (emotional) (3.3)", "Social support (emotional) (3.3)", "Social support (unspecified) (3.1) ", "Social support (unspecified) (3.1) "),
present = c("present ", "not present", "present ", "not present", "present ", "not present", "present ", "not present", "present ", "not present", "present", "not present", "present ", "not present", "present ", "not present", "present ", "not present", "present ", "not present", "present ", "not present", "present ", "not present", "present ", "not present", "present ", "not present", "present ", "not present", "present ", "not present", "present ", "not present", "present ", "not present", "present ", "not present", "present ", "not present"),
SMD = c(1.69, 0.42, 0.78, 0.4, 0.36, 0.57, 0.71, 0.4, 2.23, 0.31, 0.32, 0.63, 0.45, 0.58, 0.79, 0.26, 0.55, 0.56, 1.39, 0.4, 0.6, 0.46, 0.7, 0.34, 2.53, 0.47, 0.36, 0.62, 0.2, 0.7, 0.03, 0.66, 0.68, 0.47, 1.14, 0.4, 1.13, 0.44, 0.67, 0.52),
lower_limit = c(-1.49, 0.22, -0.4, 0.06, 0.08, -0.33, -0.34, -0.04, 0.15, 0.2, 0.14, -0.4, -0.25, -0.26, -0.19, 0.11, -0.66, 0.23, -0.68, 0.19, -0.35, 0.11, -0.46, 0.15, 1.36, 0.05, -0.07, -0.2, -0.01, -0.18, -0.42, -0.14, -0.4, 0.06, -0.84, 0.16, -1.47, 0.24, -0.93, 0.27),
upper_limit = c(4.87, 0.62, 1.95, 0.74, 0.65, 1.48, 1.75, 0.84, 4.32, 0.42, 0.51, 1.67, 1.15, 1.43, 1.77, 0.41, 1.77, 0.89, 3.46, 0.6, 1.55, 0.81, 1.87, 0.53, 3.7, 0.89, 0.78, 1.43, 0.41, 1.58, 0.47, 1.46, 1.77, 0.88, 3.12, 0.64, 3.72, 0.65, 2.28, 0.78)),
.Names = c("BCT", "present", "SMD", "lower_limit", "upper_limit"),
row.names = c(NA, -40L), class = "data.frame")
很可能您导入了您的角色数据作为因素。当您使用 c()
和 matrix()
等简化函数重塑因子时,它们会转换为数值,因为矩阵不能包含因子。更仔细的转换是
labeltext = as.matrix(data.frame(data$BCT, data$present))