【问题标题】:How to avoid expand = c(0,0) to crop off axis ticks labels如何避免 expand = c(0,0) 裁剪离轴刻度标签
【发布时间】:2020-01-08 09:14:00
【问题描述】:

我正在构建一个克利夫兰点图,显示 0.0 和 1.0 之间的值。

我已经接近了我想要的情节,但是一个小细节让我很困扰。因为我使用expand = c(0,0),所以x轴上1.00中的最后一个0被截断了。

我尝试过更改各种设置,但没有成功。

不幸的是,这个 question 与这篇文章的标题相似,但没有帮助。

你能帮我在不切断 x 轴 1.00 中最后一个零的情况下保持绘图的边界吗?

代表:

library(tidyverse)

df <- tibble(
  Tastyness = c(0.6, 0.7, 0.9, 0.95, 0.98),
  Fruit = c("Bananas", "Apples", "Oranges", "Mango", "Peach")
)


ggplot(df, aes(x = Tastyness, y = Fruit)) +
  geom_point(size = 4) +
  theme_bw() +
  scale_x_continuous(
    limits = c(0.0, 1.0),
    expand = c(0, 0),
    breaks = c(0, 0.5, 0.75, 0.9, 1.00)
  )

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    一种适用于情节边缘的解决方案:

    ggplot(df, aes(x = Tastyness, y = Fruit)) +
      geom_point(size = 4) +
      theme_bw() +
      scale_x_continuous(
        limits = c(0.0, 1.0),
        expand = c(0, 0),
        breaks = c(0, 0.5, 0.75, 0.9, 1.00)
      ) +
      theme(plot.margin=unit(c(.2,.5,.2,.2),"cm"))
    

    【讨论】:

      【解决方案2】:

      也许改变标签,让 0 和 1 没有小数:

      ggplot(df, aes(x = Tastyness, y = Fruit)) +
        geom_point(size = 4) +
        theme_bw() +
        scale_x_continuous(
          limits = c(0.0, 1),
          expand = c(0, 0),
          breaks = c(0, 0.5, 0.75, 0.9, 1.00),
          labels = c(0, 0.5, 0.75, 0.9, 1))
      

      或者水平移动标签:

      ggplot(df, aes(x = Tastyness, y = Fruit)) +
        geom_point(size = 4) +
        theme_bw() +
        scale_x_continuous(
          limits = c(0.0, 1),
          expand = c(0, 0),
          breaks = c(0, 0.5, 0.75, 0.9, 1.00)) +
        theme(axis.text.x = element_text(hjust = 1))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-03-30
        • 1970-01-01
        • 1970-01-01
        • 2021-10-29
        • 1970-01-01
        • 2012-03-14
        • 1970-01-01
        相关资源
        最近更新 更多