在定制我的赔率(ggplot)时需要帮助!

时间:2019-04-01 05:36:32

标签: r ggplot2

我被分配在R中创建一个比值ggplot的赔率。下面给出了我应该创建的图。

Given plot

我的工作是找出可以在R中创建精确绘图的代码。我已经完成了大部分工作。这是我的工作。

My work

在进入我的代码之前,非常重要的一点是我没有为boxOdds,boxCILow和boxCIHigh使用正确的值,因为我没有弄清楚正确的值。我想先找出ggplot的代码,以便找到后立即输入正确的值。

这是我使用的代码

library(ggplot2)

boxLabels = c("Females/Males", "Student-Centered Prac. (+1)", "Instructor Quality (+1)", "Undecided / STM", 
              "non-STEM / STM", "Pre-med / STM", "Engineering / STM", "Std. test percentile (+10)", 
              "No previous calc / HS calc", "College calc / HS calc")

df <- data.frame(yAxis = length(boxLabels):1,
                 boxOdds = 
                   c(2.23189, 1.315737, 1.22866, 0.8197413, 0.9802449, 0.9786673, 0.6559005, 0.5929812, 0.6923759, 1.3958275),
                 boxCILow = 
                   c(.7543566,1.016,.9674772,.6463458,.9643047,.864922,.4965308,.3572142, 0.4523759, 1.2023275),
                 boxCIHigh = 
                   c(6.603418,1.703902,1.560353,1.039654,.9964486,1.107371,.8664225,.9843584, 0.9323759, 1.5893275) 
)


(p <- ggplot(df, aes(x = boxOdds, y = boxLabels)) + 
    geom_vline(aes(xintercept = 1), size = 0.75, linetype = 'dashed') +
    geom_errorbarh(aes(xmax = boxCIHigh, xmin = boxCILow), size = .5, height = 
                     0, color = 'gray50') + 
    geom_point(size = 3.5, color = 'orange') +
    theme_bw() +
    theme(panel.grid.minor = element_blank()) +
    scale_x_continuous(breaks = seq(0,7,1) ) +
    ylab('') +
    xlab('Odds Ratio') + 
    annotate(geom = 'text', y =1.1, x = 3.5, label ='', 
             size = 3.5, hjust = 0) + ggtitle('Estimated Odds of Switching') + 
    theme(plot.title = element_text(hjust = 0.5, size = 30), 
          axis.title.x = (element_text(size = 15))) + 
    theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank())
)
p

我被困在哪里

  1. 删除每行CI的开头和结尾的小垂直线)。我不确定它叫什么,所以我很难查找它。 已解决

  2. 我还坚持用不同的颜色为特定的行着色。

  3. 我停留的最后一部分是为y轴分配每个变量的正确顺序。正如您在我的代码(“ boxLabels”部分)中所看到的那样,我已将所有变量按给定图的顺序放置,但似乎R不在乎该顺序。因此,位于最上方的变量是“不确定/ STM”,而不是“雌性/雄性”。

  4. 如何将空间从0减小到1? 已解决

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

首先,您可能想要ggstance::geom_pointrangeh。其次,您可以在开始时使用yAxis定义颜色。要对一些因素进行分组,请创建一个新变量group。第三点与您的数据有关,您可以在其中分配因子标签。第四,按照 @beetroot 的建议删除coord_trans

分配因子标签

dat$yAxis <- factor(dat$yAxis, levels=10:1, labels=rev(boxLabels))

创建组

dat$group <- 1
dat$group[which(dat$yAxis %in% c("Females/Males", "Undecided / STM", "non-STEM / STM",
                        "Pre-med / STM"))] <- 2
dat$group[which(dat$yAxis %in% c("Student-Centered Prac. (+1)",
                               "No previous calc / HS calc", 
                               "College calc / HS calc"))] <- 3

颜色

colors <- c("#860fc2", "#fc691d", "black")

情节

library(ggplot2)
library(ggstance)
ggplot(dat, aes(x=boxOdds, y=yAxis, color=as.factor(group))) +
  geom_vline(aes(xintercept=1), size=0.75, linetype='dashed') +
  geom_pointrangeh(aes(xmax=boxCIHigh, xmin=boxCILow), size=.5, 
                   show.legend=FALSE) +
  geom_point(size=3.5, show.legend=FALSE) +
  theme_bw() +
  scale_color_manual(values=colors)+
  theme(panel.grid.minor=element_blank()) +
  scale_x_continuous(breaks=seq(0,7,1), limits=c(0, max(dat[2:4]))) +
  ylab('') +
  xlab('Odds Ratio') +
  annotate(geom='text', y =1.1, x=3.5, label ='', 
           size=3.5, hjust=0) + ggtitle('Estimated Odds of Switching') + 
  theme(plot.title=element_text(hjust=.5, size=20)) +
  theme(panel.grid.minor=element_blank(), panel.grid.major=element_blank())

给予

enter image description here


数据

dat <- structure(list(yAxis = 10:1, boxOdds = c(2.23189, 1.315737, 1.22866, 
0.8197413, 0.9802449, 0.9786673, 0.6559005, 0.5929812, 0.6923759, 
1.3958275), boxCILow = c(0.7543566, 1.016, 0.9674772, 0.6463458, 
0.9643047, 0.864922, 0.4965308, 0.3572142, 0.4523759, 1.2023275
), boxCIHigh = c(6.603418, 1.703902, 1.560353, 1.039654, 0.9964486, 
1.107371, 0.8664225, 0.9843584, 0.9323759, 1.5893275)), class = "data.frame", row.names = c(NA, 
-10L))
相关问题