2

I have the following data:

simres_auc2 <- structure(list(MINDGDP = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L), PSIZE = c(5L, 5L, 5L, 5L, 10L, 10L, 
10L, 10L, 20L, 20L, 20L, 20L, 50L, 50L, 50L, 50L, 5L, 5L, 5L, 
5L, 10L, 10L, 10L, 10L, 20L, 20L, 20L, 20L, 50L, 50L, 50L, 50L, 
5L, 5L, 5L, 5L, 10L, 10L, 10L, 10L, 20L, 20L, 20L, 20L, 50L, 
50L, 50L, 50L), simno = c(13L, 13L, 13L, 13L, 16L, 16L, 16L, 
16L, 19L, 19L, 19L, 19L, 22L, 22L, 22L, 22L, 13L, 13L, 13L, 13L, 
16L, 16L, 16L, 16L, 19L, 19L, 19L, 19L, 22L, 22L, 22L, 22L, 13L, 
13L, 13L, 13L, 16L, 16L, 16L, 16L, 19L, 19L, 19L, 19L, 22L, 22L, 
22L, 22L), METHOD_RED = c("EVA (alpha = 0.001)", "EVA (alpha = 0.005)", 
"EVA (alpha = 0.01)", "EVA (alpha = 0.05)", "EVA (alpha = 0.001)", 
"EVA (alpha = 0.005)", "EVA (alpha = 0.01)", "EVA (alpha = 0.05)", 
"EVA (alpha = 0.001)", "EVA (alpha = 0.005)", "EVA (alpha = 0.01)", 
"EVA (alpha = 0.05)", "EVA (alpha = 0.001)", "EVA (alpha = 0.005)", 
"EVA (alpha = 0.01)", "EVA (alpha = 0.05)", "EVA (alpha = 0.001)", 
"EVA (alpha = 0.005)", "EVA (alpha = 0.01)", "EVA (alpha = 0.05)", 
"EVA (alpha = 0.001)", "EVA (alpha = 0.005)", "EVA (alpha = 0.01)", 
"EVA (alpha = 0.05)", "EVA (alpha = 0.001)", "EVA (alpha = 0.005)", 
"EVA (alpha = 0.01)", "EVA (alpha = 0.05)", "EVA (alpha = 0.001)", 
"EVA (alpha = 0.005)", "EVA (alpha = 0.01)", "EVA (alpha = 0.05)", 
"EVA (alpha = 0.001)", "EVA (alpha = 0.005)", "EVA (alpha = 0.01)", 
"EVA (alpha = 0.05)", "EVA (alpha = 0.001)", "EVA (alpha = 0.005)", 
"EVA (alpha = 0.01)", "EVA (alpha = 0.05)", "EVA (alpha = 0.001)", 
"EVA (alpha = 0.005)", "EVA (alpha = 0.01)", "EVA (alpha = 0.05)", 
"EVA (alpha = 0.001)", "EVA (alpha = 0.005)", "EVA (alpha = 0.01)", 
"EVA (alpha = 0.05)"), auc = c(0.5, 0.440423333333333, 0.73412, 
0.570526, 0.5, 0.465404, 0.695695333333333, 0.536143333333333, 
0.5, 0.482674, 0.673217333333333, 0.517231333333333, 0.5, 0.478126666666667, 
0.661129333333333, 0.530846, 0.5, 0.4520975, 0.742583, 0.577082, 
0.5, 0.4546035, 0.694907, 0.550087, 0.5, 0.4706495, 0.6585825, 
0.544709, 0.5, 0.473219, 0.659395, 0.546985, 0.5, 0.45364, 0.754459333333333, 
0.58385, 0.5, 0.442713333333333, 0.699316, 0.563635333333333, 
0.5, 0.486780666666667, 0.678044666666667, 0.554051333333333, 
0.5, 0.462297333333333, 0.651185333333333, 0.544234666666667)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -48L), .Names = c("MINDGDP", 
"PSIZE", "simno", "METHOD_RED", "auc"))

The following code generates the following plot, where position_dodge is working correctly.

ggplot2::ggplot(data = simres_auc2,
                          aes_string(x = "factor(METHOD_RED)",
                                     y = "auc")) + 
ggplot2::geom_point(aes_string(shape = "factor(MINDGDP)",
                               group = "factor(MINDGDP)",
                               colour = paste0("factor(PSIZE)")),
                    position = position_dodge(width = 0.25))

Position_dodge works correctly

However, I want factor(METHOD_RED) on the y-axis, and auc on the x-axis. Consequently, in the following code, I have interchanged x and y, and replaced width in position_dodge with height.

ggplot2::ggplot(data = simres_auc2,
                        aes_string(y = "factor(METHOD_RED)",
                                   x = "auc")) + 
  ggplot2::geom_point(aes_string(shape = "factor(MINDGDP)",
                             group = "factor(MINDGDP)",
                             colour = paste0("factor(PSIZE)")),
                  position = position_dodge(height = 0.25))

However, this code gives the following plot, in which position_dodge is not working as I had hoped.

Position_dodge does not work as I had hoped

Does anyone know why this is the case, and how I can circumvent the issue? Please note that using coord_flip is not an option for me, as it adversely affects faceting that I want to use in the code. See, for example, this question and this Github issue.

Community
  • 1
  • 1
  • I don't think you can easily avoid `coord_flip` here. If that "adversely affects faceting" you should give a full example that illustrates this. – Roland Oct 06 '15 at 07:47
  • Please see the links I added. –  Oct 06 '15 at 07:48
  • 1
    I'd use `coord_flip` and construct my plot using `gridExtra::grid.arrange`. – Roland Oct 06 '15 at 07:50
  • I've had issues with `grid.arrange` and `arrangeGrobs` when I've tried to save plots, so I try to avoid them. Also, I'd like to know _why_ `position_dodge` is functioning in this way. –  Oct 06 '15 at 08:01
  • What kind of issues did you have with `grid.arrange`? I've never had any. Regarding the *why*, someone will have to delve into the code. Since I find ggplot2's code structure a nightmare that won't be me. – Roland Oct 06 '15 at 08:04
  • Unfortunately I don't think `position_dodge` is going to work how you want it to. See [this github issue](https://github.com/hadley/ggplot2/issues/1119) if you haven't already. – aosmith Oct 06 '15 at 14:57

3 Answers3

2

I've been having the same issue and this is the first question that pops up on google, so I thought I'd post my answer in case someone else is having the same problem:

The package ggstance provides vertical versions for several of the position-functions, in this case position_dodgev()

library(ggstance)

ggplot2::ggplot(data = simres_auc2,
                aes_string(y = "factor(METHOD_RED)",
                           x = "auc")) +
  ggplot2::geom_point(
    aes_string(
      shape = "factor(MINDGDP)",
      group = "factor(MINDGDP)",
      colour = paste0("factor(PSIZE)")
    ),
    position = position_dodgev(height = 0.25)
  )

enter image description here

f.lechleitner
  • 3,554
  • 1
  • 17
  • 35
1

You can use continuous y scale and manually map y positions to different groups of data.

RANGE <- .5

ggplot(data = simres_auc2, aes(y = as.integer(factor(METHOD_RED)), x = auc)) + 
  geom_point(aes(y = as.integer(factor(METHOD_RED)) + 
                   RANGE *(-.5+(as.integer(factor(MINDGDP))-1)/(length(unique(MINDGDP))-1)), 
                 shape = factor(MINDGDP), group = factor(MINDGDP), 
                 colour = factor(PSIZE, levels = sort(unique(PSIZE))) ), size = 4 ) +
  scale_y_continuous(labels = function(x) levels(factor(simres_auc2$METHOD_RED))[x]) +
  guides(color = guide_legend(title = "PSIZE"), shape = guide_legend(title = "MINDGDP"))

enter image description here

inscaven
  • 2,514
  • 19
  • 29
0

A possible solution to your problem might be using position_jitter and using a width of 0 and a height of 0.25:

ggplot(data = simres_auc2, aes(y = factor(METHOD_RED), x = auc)) + 
  geom_point(aes(shape = factor(MINDGDP), group = factor(MINDGDP), colour = factor(PSIZE)),
             position = position_jitter(width = 0, height = 0.25))

which gives:

enter image description here

Jaap
  • 81,064
  • 34
  • 182
  • 193
  • Please re-read my question: "Please note that using `coord_flip` is not an option for me, as it adversely affects faceting that I want to use in the code." –  Oct 06 '15 at 07:46
  • @AJP123 See my update answer. Is this what you are looking for? – Jaap Oct 06 '15 at 08:17
  • No. I want to use `position_dodge`, because I want to dodge the shapes in the plot according to their group. –  Oct 06 '15 at 08:23