【发布时间】:2019-06-08 20:00:15
【问题描述】:
我正在绘制优势比和置信区间图,并希望在 y 轴上的不同类别变量之间留出空间,但我无法做到这一点。
这是我尝试过的一些代码
final_matrix = read.table("matrix", sep = "\t", header = T)
thecolnames = c('Coefficients', 'CI2.5', 'CI97.5', 'Demographics', 'Categories')
colnames(final_matrix) = thecolnames
final_matrix$Categories = factor(final_matrix$Categories, levels = final_matrix$Categories)
ggplot(final_matrix, aes(x = final_matrix$Coefficients, y = final_matrix$Categories, color=Demographics)) +
geom_vline(aes(xintercept = 1), size = .25, linetype = "dashed") +
geom_errorbarh(aes(xmax = final_matrix$CI2.5, xmin = final_matrix$CI97.5), size = .5, height =
.2, color = "gray50") +
geom_point(size = 1.5) +
theme(panel.grid.minor = element_blank()) +
ylab("") +
xlab("Odds ratio") +
ggtitle("Association between Coefficients and Categories)
最终矩阵如下:
structure(list(Coefficients = c(1, 1.030507438, 1.044036099,
1, 0.9733293067, 1, 2.33127416, 2.402926091, 2.422669367, 2.395563322,
1, 1.005581977, 1.006818225, 1, 1.021092986, 1.019769848, 0.9455416249,
1.010520081, 1, 0.9912335776, 0.988674891, 1.006406292, 1, 1.002857188,
0.9963459983, 1.005356995, 1, 0.9990484426, 1, 1.025390984, 1,
1.017962091, 1, 0.9805242864), CI2.5 = c(1, 0.9913713797, 0.9998913413,
1, 0.9298311216, 1, 2.189771461, 2.268230465, 2.284251227, 2.257308777,
1, 0.9630167448, 0.9481349102, 1, 0.9800985942, 0.9841991871,
0.9056186125, 0.9576109398, 1, 0.9562607091, 0.9532485042, 0.9664533176,
1, 0.9477868449, 0.9509813538, 0.9519718969, 1, 0.9724519214,
1, 0.9631787377, 1, 0.9547918105, 1, 0.9186602875), CI97.5 = c(1,
1.071188458, 1.090129827, 1, 1.018862369, 1, 2.481920741, 2.545620424,
2.569475192, 2.542285613, 1, 1.050028588, 1.069133651, 1, 1.063802043,
1.056626094, 0.9872245912, 1.066352515, 1, 1.027485492, 1.025417859,
1.048010913, 1, 1.061127346, 1.043874672, 1.061735847, 1, 1.026372378,
1, 1.091621553, 1, 1.0853118, 1, 1.046554302), Demographics = structure(c(5L,
5L, 5L, 2L, 2L, 6L, 6L, 6L, 6L, 6L, 1L, 1L, 1L, 4L, 4L, 4L, 4L,
4L, 3L, 3L, 3L, 3L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L), .Label = c("Age", "Disease", "Education", "Employment", "Financial",
"Health", "Insurance", "Race"), class = "factor"), Categories = structure(1:34, .Label = c("Extremely Difficult",
"Somewhat Difficult", "Not Difficult", "No Disease", "Disease",
"Overall Health (<=3)", "Overall Health (4)", "Overall Health (5)",
"Overall Health (6)", "Overall Health (7)", "18-29", "30-64",
"65+", "Full-Time", "Part-Time", "Unemployed/Homemaker ", "Disability",
"Retired", "Less than HS", "HS Grad", "Some College / Vocational",
"College Degree / PhD", "Private", "Other Public", "Medicaid",
"Medicare", "Non-Hispanic", "Hispanic", "Non-White", "White ",
"Non-Black", "Black", "Non-Other", "Other"), class = "factor")), row.names = c(NA,
-34L), class = "data.frame")
我得到的是类似的东西
a --o---
b -o-
c --o--
d -o-
而 a 和 b 可能属于一个“人口统计”类别,而 c 和 d 可能属于另一个“人口统计类别”。虽然我可以让它们变成不同的颜色,但我正在努力将它们分隔成类似的东西:
Demographic 1
a --o---
b -o-
Demographic 2
c --o--
d -o-
【问题讨论】:
-
最终矩阵的外观如何?有什么方法可以模拟您的数据,以便有人可以运行它并调试它?类别中有非英文字符吗?
-
最后的矩阵添加谢谢!
-
假设您有一个标准数据框,例如 mtcars。你可以提供它的输出,有人可以复制它并直接运行到 R 中尝试 dput(mtcars) 看看会发生什么输出可以被人们用来运行你的代码并调试它
-
制作一个 dput(final_matrix) 并在此处复制并粘贴输出
-
这行得通吗?抱歉,我是 Stack Overflow 的新手,非常感谢您的指导。
标签: r ggplot2 logistic-regression