【问题标题】:extend limit for qualitative variable in ggplot扩展ggplot中定性变量的限制
【发布时间】:2017-05-26 01:31:34
【问题描述】:

大家好,感谢您的考虑,

目标是在有很多很多 x 轴标签的情况下提供更多空间。注意我不在乎标签本身是否在图上可视化(我在下面的代码中将它们排除在外)。我想要改变的是,当典型的 geom_point 图中有约 1000 个 x 轴标签和 1000 个数据点时,与第一个和最后几个 x 轴标签相关联的最左边和最右边的点被压碎了绘图区域的边缘。我想填充一些空间,这样这些点就不会被挤压。

我想知道在xixii 不是数字而是字符串的情况下,是否有办法改变scale-x-discrete(limits=c(xi,xii)) 类型的命令。一对让这一点(希望)清楚的例子:

在第一种情况下,最右边的点和灰色阴影图的边缘之间的间距很大,因此该点不会直接流到绘图区域的边缘。我想要这种效果,在最后一点和绘图边缘之间有一些空间。

library(ggplot2)
set.seed(10)
lessVals = sample(1:100000, 10, replace = T)
lessIDs = as.character(sample(1:100000, 10, replace = T))
df.less <- data.frame(lessIDs, lessVals)
df.less$lessIDs <- as.character(df.less$lessIDs)
lessDat <- ggplot(df.less)
lessDat + geom_point(aes(lessIDs, lessVals)) + theme(axis.text.x = 
element_blank())

但是,在以下情况下,有数千个 x 轴点,虽然可视化标识本身的标签是无关紧要的,但我想避免让最左边和最右边的点被挤压到边缘绘图区。请注意,我不在乎图中的点被挤压在一起 - 这是不可避免的,并且可以通过 alpha 参数或其他东西来解决过度绘图。我想要解决的是让绘图边缘上的点(最好是左侧和右侧)在水平面之间有一些缓冲区,左侧是 y 轴刻度,右侧是此刻什么都没有(但可能是,比如说,一个传说)。

manyIDs = as.character(sample(1:100000, 1000, replace = T))
manyVals = sample(1:100000, 1000, replace = T)
df.many <- data.frame(manyIDs, manyVals)
df.many$manyIDs <- as.character(df.many$manyIDs)
manyDat <- ggplot(df.many)
manyDat + geom_point(aes(manyIDs, manyVals)) + theme(axis.text.x = 
element_blank())

我很想知道究竟可以做些什么来为这些点的水平边缘提供一点缓冲。

感谢分享你的天才。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    因为您的 x 变量是字符,所以 ggplot2 创建一个“离散”x 轴。当类别数量相当少 (2 - 25) 时,离散轴的默认绘图限制很有意义。您可以使用scale_x_discreteexpand 参数来手动调整绘图限制。视觉外观将取决于点的大小和图形设备,因此您可能需要进行相应调整。

    例如,scale_x_discrete(expand=c(0.1, 0)) 会将绘图的每一侧扩展 10% 的数据范围。而scale_x_discrete(expand=c(0, 2)) 将每边扩展 2(无论 x 是什么单位)。另见http://ggplot2.tidyverse.org/reference/scale_discrete.html

    p1 <- ggplot(df.less, aes(x=lessIDs, y=lessVals)) +
          geom_point() + 
          theme(axis.text.x=element_blank()) +
          labs(title="Default x-axis limits")
    
    # Added x-axis space with expand.
    p2 <- ggplot(df.less, aes(x=lessIDs, y=lessVals)) +
          geom_point() + 
          theme(axis.text.x=element_blank()) +
          scale_x_discrete(expand=c(0, 2)) +
          labs(title="expand=c(0, 2)")
    
    p3 <- ggplot(df.many, aes(x=manyIDs, y=manyVals)) +
          geom_point() + 
          theme(axis.text.x=element_blank()) +
          theme(panel.grid.major.x=element_blank()) +
          theme(axis.ticks.x=element_blank()) +
          labs(title="Default x-axis limits")
    
    # Added x-axis space with expand.
    p4 <- ggplot(df.many, aes(x=manyIDs, y=manyVals)) +
          geom_point() + 
          theme(axis.text.x=element_blank()) +
          theme(panel.grid.major.x=element_blank()) +
          theme(axis.ticks.x=element_blank()) +
          scale_x_discrete(expand=c(0.1, 0)) +
          labs(title="expand=c(0.1, 0)")
    
    library(gridExtra)
    
    ggsave("plots.png", plot=arrangeGrob(p1, p2, p3, p4, nrow=2), 
           height=4, width=6, dpi=150)
    

    【讨论】:

    • 非常感谢@bdemarest
    猜你喜欢
    • 1970-01-01
    • 2016-02-18
    • 1970-01-01
    • 2016-09-06
    • 1970-01-01
    • 1970-01-01
    • 2011-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多