Log scale

Set the logscale argument to true to use a log scale on the vertical axis. If your estimates and standard errors are on the log scale (e.g. log hazard ratios), then set exponentiate to true. This will plot exp(estimates) and use a log scale for the axis (if logscale is not set).

plot <- make_shape_plot(ckbplotr_shape_data[ckbplotr_shape_data$is_female == 0,],
                        col.x        = "rf",
                        col.estimate = "est",
                        col.stderr   = "se",
                        col.n        = "n",
                        xlims        = c(15, 50),
                        ylims        = c(0.7, 3),
                        ybreaks      = c(0.7, 1, 1.5, 2, 3),
                        scalepoints  = TRUE,
                        title        = NULL,
                        logscale     = TRUE)

Using groups

The col.group argument can be supplied to plot results for different groups (using shades of grey for the fill colour). (Set ciunder to TRUE so that the confidence interval lines are hidden behind behind the point estimate squares.) Use the legend.position to set the legend position.

plot <- make_shape_plot(ckbplotr_shape_data,
                        col.x        = "rf",
                        col.estimate = "est",
                        col.stderr   = "se",
                        col.n        = "n",
                        col.group    = "is_female",
                        xlims        = c(15,50),
                        ylims        = c(0.5, 3),
                        scalepoints  = TRUE,
                        title        = NULL,
                        ciunder      = TRUE,
                        legend.position = "bottom")

Labelling groups

To label points directly, you could add a geom_text() to the plot:

data_to_plot <- ckbplotr_shape_data %>%
  dplyr::mutate(is_female = factor(is_female,
                                   levels = c(0, 1), 
                                   labels = c("Men", "Women")))

shape_plot <- make_shape_plot(data_to_plot,
                col.x        = "rf",
                col.estimate = "est",
                col.stderr   = "se",
                col.n        = "n",
                col.group    = "is_female",
                xlims        = c(15,50),
                ylims        = c(0.5, 3),
                scalepoints  = TRUE,
                title        = NULL,
                ciunder      = TRUE,
                legend.position = "none",
                printplot    = FALSE)

shape_plot$plot +
  geom_text(aes(label = is_female),
            hjust = 0,
            nudge_x = 2.5,
            data = ~ dplyr::group_by(., is_female) %>% dplyr::filter(rf == max(rf)))

Adding lines

The lines argument will add lines (linear fit through estimates on plotted scale, weighted by inverse variance) for each group.

plot <- make_shape_plot(ckbplotr_shape_data,
                        col.x        = "rf",
                        col.estimate = "est",
                        col.stderr   = "se",
                        col.n        = "n",
                        col.group    = "is_female",
                        xlims        = c(15,50),
                        ylims        = c(0.5, 3),
                        scalepoints  = TRUE,
                        title        = NULL,
                        ciunder      = TRUE,
                        lines        = TRUE)

Setting aesthetics

The shape and fill colour of points, and colour of points and confidence interval lines can be set overall or on a per-point basis. This is done by setting arguments shape, colour, cicolour, fill, and ciunder to appropriate values, or to the name of a column containing values for each point.

The argument/columns, what they control, and the type:

argument controls type
shape plotting character for points integer
colour colour of points character
cicolour colour of CI lines character
fill fill colour of points character
ciunder if the CI line should be plotted before the point logical

(Note that the approach for using columns to specify colours and shapes in this package does not make use of the automatic scales available in ggplot. We recommend you take a look at the ggplot2 documentation for better examples if you want to write ggplot2 code.)

Using values

If the argument doesn’t match the name of a column in the data, then the value will be used for all points.

plot <- make_shape_plot(ckbplotr_shape_data,
                        col.x        = "rf",
                        col.estimate = "est",
                        col.stderr   = "se",
                        col.n        = "n",
                        xlims        = c(15,50),
                        ylims        = c(0.5, 3),
                        scalepoints  = TRUE,
                        title        = NULL,
                        ciunder      = TRUE,
                        shape        = 23,
                        colour       = "black",
                        fill         = "red",
                        cicolour     = "blue")

Using columns

If the argument matches a column name, then the values in the column will be used.

ckbplotr_shape_data$fillcol <- "black"
ckbplotr_shape_data[ckbplotr_shape_data$is_female == 1,]$fillcol <- "orange"

plot <- make_shape_plot(ckbplotr_shape_data,
                        col.x        = "rf",
                        col.estimate = "est",
                        col.stderr   = "se",
                        col.n        = "n",
                        xlims        = c(15,50),
                        ylims        = c(0.5, 3),
                        scalepoints  = TRUE,
                        title        = NULL,
                        ciunder      = TRUE,
                        colour       = "fillcol",
                        fill         = "fillcol")

Specify a column for cicolour is useful for changing the colour of confidence interval lines that would otherwise be hidden.

ckbplotr_shape_data$cicol <- "black"
ckbplotr_shape_data[2,]$cicol <- "white"

plot <- make_shape_plot(ckbplotr_shape_data,
                        col.x        = "rf",
                        col.estimate = "est",
                        col.stderr   = "se",
                        col.n        = "n",
                        xlims        = c(15,50),
                        ylims        = c(0.5, 3),
                        scalepoints  = TRUE,
                        title        = NULL,
                        pointsize    = 7,
                        cicolour     = "cicol")

Note that the size of boxes depends on pointsize, but the true length of confidence interval lines depends on the size at which the plot is displayed or saved. So, first save your plot as an image then go back to change the colour of confidence interval lines as needed.

Categorical risk factor

The risk factor can be a factor. In this case, the x-axis coordinates are 1, 2, 3, .. so suitable x-axis limits are 0.5 and number of categories plus 0.5. You may need to add position arguments so that points, intervals and text do not overlap:

ckbplotr_shape_data$rf <- c(  "A",   "B",   "C",   "D", "A",   "B",   "C",  "D")

plot <- make_shape_plot(ckbplotr_shape_data,
                        col.x        = "rf",
                        col.estimate = "est",
                        col.stderr   = "se",
                        col.n        = "n",
                        col.group    = "is_female",
                        xlims        = c(0.5, 4.5),
                        ylims        = c(0.5, 3),
                        scalepoints  = TRUE,
                        title        = NULL,
                        ciunder      = TRUE,
                        addarg = list(point     = "position = position_dodge(width = 0.5)",
                                      ci        = "position = position_dodge(width = 0.5)",
                                      n         = "position = position_dodge(width = 0.5)",
                                      estimates = "position = position_dodge(width = 0.5)"))

Stroke

The stroke argument sets the stroke aesthetic for plotted shapes. See https://ggplot2.tidyverse.org/articles/ggplot2-specs.html for more details. The stroke size adds to total size of a shape, so unless stroke = 0 the scaling of size by inverse variance will be slightly inaccurate (but there are probably more important things to worry about).