【问题标题】:avoid ggplot2 to partially cut axis text避免 ggplot2 部分切割轴文本
【发布时间】:2018-05-06 16:22:11
【问题描述】:

我正在尝试生成一个带有刻度的水平图。

df = data.frame(quality = c("low", "medium", "high", "perfect"),
                n = c(0.1, 11, 0.32, 87.45))

require(ggplot2)
require(dplyr)
size = 20
df %>% 
        ggplot() +
        geom_bar(aes(x = quality, y = n),
                 stat = "identity", fill = "gray70",
                 position = "dodge") +
        geom_text(aes(x = quality, y = n,
                      label = paste0(round(n, 2), "%")),
                  position = position_dodge(width = 0.9),
                  hjust = -0.2,
                  size = 10, color = "gray50") +
        coord_flip() +
        ggtitle("") +
        xlab("gps_quality\n") +
        #scale_x_continuous(limits = c(0, 101)) +
        theme_classic() +
        theme(axis.title = element_text(size = size, color = "gray70"),
              axis.text.x = element_blank(),
              axis.title.x = element_blank(),
              axis.ticks = element_blank(),
              axis.line = element_blank(),
              axis.title.y = element_blank(),
              axis.text.y = element_text(size = size,
                                         color = ifelse(c(0,1,2,3) %in% c(2, 3), "tomato1", "gray40")))

不幸的是,一个条比另一个长得多,而 ggplot 部分降低了它的价值。

有什么想法吗?

我已经尝试过scale_y_continuous(expand = c(0, 0),但它在刻度文本和条形之间增加了很多间隙。

【问题讨论】:

  • 好像在{ggplot2}的最后一个dev版本中,可以在coord_cartesian中添加clip="off"。也许这可以帮助:twitter.com/ClausWilke/status/991542952802619392?s=19
  • 您能否详细说明如何将其应用于我的问题?
  • 我不太确定。我刚刚在 Twitter 上看到了这一点,我认为这可能适用于你的情况。我还没有探索。

标签: r ggplot2


【解决方案1】:

你需要:

### Need development version of ggplot2 for `clip = "off"`
# Ref: https://github.com/tidyverse/ggplot2/pull/2539

# install.packages("ggplot2", dependencies = TRUE)

library(magrittr)
library(ggplot2)

df = data.frame(quality = c("low", "medium", "high", "perfect"),
                n = c(0.1, 11, 0.32, 87.45))

size = 20

plt1 <- df %>% 
  ggplot() +
  geom_bar(aes(x = quality, y = n),
           stat = "identity", fill = "gray70",
           position = "dodge") +
  geom_text(aes(x = quality, y = n,
                label = paste0(round(n, 2), "%")),
            position = position_dodge(width = 0.9),
            hjust = -0.2,
            size = 10, color = "gray50") +

  # This is needed
  coord_flip(clip = "off") +

  ggtitle("") +
  xlab("gps_quality\n") +
  # scale_x_continuous(limits = c(0, 101)) +
  theme_classic() +
  theme(axis.title = element_text(size = size, color = "gray70"),
        axis.text.x = element_blank(),
        axis.title.x = element_blank(),
        axis.ticks = element_blank(),
        axis.line = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_text(size = size,
                                   color = ifelse(c(0,1,2,3) %in% c(2, 3),
                                   "tomato1", "gray40")))

plt1 + theme(plot.margin = margin(2, 4, 2, 2, "cm"))

reprex package (v0.2.0) 于 2018 年 5 月 6 日创建。

【讨论】:

    【解决方案2】:

    在我的交互式屏幕设备上,我需要做的就是扩大查看面板的大小。如果要打印到具有图像格式的文件,则首先将结果分配给名称,然后将它们 print 分配给具有足够大的纵横比以接受它的文件设备(并记得关闭它。)

    plt <- df %>% 
             ggplot() +   
             ...............same as yours
    
     png(height=480, width=2000)
     print(plt);dev.off()
    

    宽度= 3000,我终于可以看到百分号了。

    【讨论】:

    • 但是你看,在你的设备中它也被剪掉了,因为你看到的是 87 而不是 87.45%
    • 好的,然后把设备弄大一点。
    • 我已经尝试使其尽可能大,但问题仍然存在。还有其他想法吗?
    • 我猜这需要比“一点点”多一点。
    • 是的,我通过使用 expand = c(0, 0) “解决”了这个问题,然后使用绘图手动修复了绘图,但这不是一个非常数据科学的方法 XD
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    相关资源
    最近更新 更多