y轴上类别之间的空间

时间:2019-06-08 20:00:15

标签: r ggplot2 logistic-regression

我正在绘制比值比和置信区间的图,并且想在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-

1 个答案:

答案 0 :(得分:0)

我不确定您是否会对尝试运行它感到满意,并评论您所想要的?

final_matrix<-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")
final_matrix<-data.frame(final_matrix)


ggplot(final_matrix,aes(x=Coefficients,y=Categories,color=Demographics,group=Categories))+
  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") + facet_wrap(Demographics~.,scales="free",ncol=1)

ggplot(final_matrix,aes(x=Coefficients,y=Categories,color=Demographics))+
  geom_vline(aes(xintercept=1),size=.25,linetype="dashed")+
  geom_errorbarh(aes(xmax=CI2.5,xmin=CI97.5),size=.5,height=.2,color="gray50")+
  geom_point(size=1.5)+
  theme(panel.grid.minor=element_blank())+
  labs(y="",x="Odds ratio",title="Association between Coefficients and Categories")+
  facet_wrap(Demographics~.,scales="free_y",ncol=1)