ggplot2 position_dodge高度不起作用

时间:2015-10-06 07:38:43

标签: r ggplot2

我有以下数据:

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"))

以下代码生成以下图表,其中position_dodge正常工作。

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

但是,我想在y轴上factor(METHOD_RED),在x轴上想auc。因此,在以下代码中,我互换了xy,并将width中的position_dodge替换为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))

但是,此代码给出了以下图表,其中position_dodge无法正常工作。

Position_dodge does not work as I had hoped

有谁知道为什么会这样,我怎么能绕过这个问题?请注意,使用coord_flip对我来说不是一个选项,因为它会对我想在代码中使用的分面产生负面影响。例如,请参阅this questionthis Github issue

3 个答案:

答案 0 :(得分:0)

您问题的可能解决方案可能是使用position_jitter并使用width 0height 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))

给出:

enter image description here

答案 1 :(得分:0)

您可以使用连续y比例并手动将y位置映射到不同的数据组。

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

答案 2 :(得分:0)

我遇到了同样的问题,这是第一个在Google上弹出的问题,所以我想如果其他人遇到相同的问题,我会发布答案:

软件包xs:schema为某些位置函数提供了垂直版本,在这种情况下,ggstance

position_dodgev()

enter image description here