在 ggplot2 的 log10 刻度轴上将 y 轴扩展为负数

Expand y axis to negative numbers on a log10 scale axis in ggplot2

我正在绘制带有 2 组标签的点和线的组合。我想在点的上方和下方绘制标签,如下所示。同时为了更好地阅读,我在 y 轴上使用了 log10 刻度,一切都很好,除了一些标签在图表区域之外,我已经使用并尝试了许多帖子中建议的每种方法,看看是否我是否得到任何有利的结果。我正在寻找以下任一解决方案:

1- 将 y 轴向负方向展开,以便可以看到标签。请注意,ylimlimits=c(x,y) 不适用于 log 比例或 sqrt 比例,如果数字为负数

2- 欺骗 geom_text 使标签可见,而不管 y 限制。请注意,我已经尝试 vjust="inward" 并且它工作正常,但是我必须使用 geom_text_repel 来移动标签并使其难以阅读,所以我仍然喜欢将标签放在顶部和底部

感谢任何帮助!

这是生成数据框的代码:

df1_InSAP_Only <- structure(list(Year_Month = c(
    "2016_06", "2016_06", "2016_07", 
    "2016_07", "2016_08", "2016_08", "2016_09", "2016_09", "2016_09", 
    "2016_09", "2016_10", "2016_10", "2016_10", "2016_10", "2016_11", 
    "2016_11", "2016_12", "2016_12", "2017_01", "2017_01", "2017_01", 
    "2017_02", "2017_02", "2017_02", "2017_02", "2017_03", "2017_03", 
    "2017_03", "2017_03", "2017_03", "2017_03", "2017_04", "2017_04", 
    "2017_04", "2017_04", "2017_04", "2017_05", "2017_05", "2017_05", 
    "2017_05", "2017_05", "2017_05", "2017_06", "2017_06", "2017_06", 
    "2017_06", "2017_06", "2017_06", "2017_07", "2017_07"), 
    Business = c("A", 
     "E", "A", "B", "B", "E", "F", "A", "H", "B", "A", "D", "B", "E", 
     "B", "E", "F", "B", "F", "B", "E", "A", "B", "C", "E", "F", "A", 
     "G", "D", "B", "E", "F", "A", "G", "B", "E", "F", "A", "D", "B", 
     "C", "E", "F", "A", "D", "B", "C", "E", "F", "A"),
    `MMR Count` = c(2L, 
      1L, 1L, 7L, 2L, 1L, 1L, 3L, 1L, 5L, 1L, 1L, 4L, 1L, 8L, 4L, 1L, 
      4L, 2L, 2L, 2L, 3L, 8L, 1L, 2L, 1L, 7L, 1L, 4L, 9L, 2L, 4L, 10L, 
      2L, 15L, 7L, 4L, 27L, 2L, 14L, 1L, 6L, 9L, 31L, 5L, 14L, 1L, 
      4L, 5L, 21L),
     `Duration Average` = c(37, 20, 9, 8, 2, 5, 1, 1, 
      1, 14, 1, 19, 8, 1, 21, 77, 1, 18, 8, 1, 1, 194, 9, 14, 19, 1, 
      10, 1, 6, 9, 18, 4, 12, 170, 7, 35, 9, 10, 7, 12, 3, 15, 5, 9, 
      10, 10, 18, 11, 16, 14)), .Names = c("Year_Month", "Business", 
      "MMR Count", "Duration Average"), row.names = c(NA, 50L), class = "data.frame")

这是生成绘图的代码:

library(ggplot2)
ggplot(df1_InSAP_Only,
        aes(x=Year_Month,
            y=`Duration Average`,
            group=Business,
            color=Business,
            size=`MMR Count`)) +
  geom_line(aes(group=Business),stat="identity", size=1, alpha=0.7) +
  geom_point(aes(colour=Business, alpha=0.7)) +
  facet_wrap(~ Business, ncol=2) +
  scale_y_log10( limits=c(-100,1000),breaks=c(0,1,10,100,1000)) +   
  scale_alpha_continuous(range = c(0.5,1), guide='none') + #remove the legend for alpha
  geom_text(data=. %>% dplyr::group_by(Business),
            aes(label=`Duration Average`,vjust=-2),
            size=3,
            position = position_dodge(width=0.9)) +
  geom_text(data=. %>% dplyr::group_by(Business),
            aes(label=`MMR Count`,vjust=3),
            size=3,
            position = position_dodge(width=0.9),
            color="brown")

剧情如下:

您不能使记录的 y 尺度变为负数 - 负数的对数未定义。让它更接近 0。这是你的图表

scale_y_log10(limits=c(.1, 1000),breaks=c(1, 10, 100, 1000))

如果您想要更多(取决于最终图的大小、文本的大小、vjust 的数量),转到 0.05 或 0.01...

我还强烈建议为您的 x 轴数据使用 Date 格式,看看这些轴标签有多好(以及垂直网格线越少,绘图看起来越干净)。

df1_InSAP_Only$date = as.Date(paste0(df1_InSAP_Only$Year_Month, "_01"), format = "%Y_%m_%d")

 # use date column on x-axis
 # reduce vjust amounts
 # get rid of meaningless group_by() statements
 # get rid of unused position dodges
ggplot(df1_InSAP_Only,
        aes(x=date,
            y=`Duration Average`,
            group=Business,
            color=Business,
            size=`MMR Count`)) +
  geom_line(aes(group=Business),stat="identity", size=1, alpha=0.7) +
  geom_point(aes(colour=Business, alpha=0.7)) +
  facet_wrap(~ Business, ncol=2) +
  scale_y_log10( limits=c(.1,1000),breaks=c(1,10,100,1000)) +   
  scale_alpha_continuous(range = c(0.5,1), guide='none') + #remove the legend for alpha
  geom_text(aes(label=`Duration Average`,vjust=-1),
            size=3) +
  geom_text(aes(label=`MMR Count`,vjust=2),
            size=3,
            color="brown")