扩展ggplot中定性变量的极限 [英] extend limit for qualitative variable in ggplot

查看:89
本文介绍了扩展ggplot中定性变量的极限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,感谢您的考虑,

Hi everyone and thanks for your consideration,

目标是在存在很多x轴标签的情况下提供更多空间.请注意,我不在乎标签本身是否在图中可见(我在下面的代码中已将它们排除在外).我要更改的事实是,当在典型的geom_point图中有〜1000个x轴标签和1000个数据点时,与这前几个x轴标签和最后几个x轴标签相关联的最左边和最右边的点都被压碎了绘图区域的边缘.我想保留一些空间,以免这些点受到挤压.

The goal is to provide more space in situations where there are many, many x-axis labels. Note I don't care whether or not the labels themselves are visualized on the plot (I've excluded them in the code below). What I want to change is the fact that when there are ~1000 x-axis labels and 1000 data points in a typical geom_point plot, the left and right-most points associated with those first and last few x-axis labels are crushed up against the edges of the plotting area. I'd like to pad some space so those points don't get so squeezed.

我想知道在xixii不是数字而是字符串的情况下,是否可以更改命令的scale-x-discrete(limits=c(xi,xii))类型.一对示例可以使这一点(希望如此)明确:

I wonder if there is a way to alter the scale-x-discrete(limits=c(xi,xii)) type of command in a situation where xi and xii are not numeric, rather, are character strings. A pair of examples to make this (hopefully) clear:

在第一种情况下,最右边的点和灰色阴影图的边缘之间的间距很大,因此该点不会沿绘图区域的边缘流血.我希望在最后一点和绘图边缘之间有一定距离的情况下产生这种效果.

In this first situation the spacing between the right-most point and the edge of the gray-shaded plot is substantial, such that the point doesn't bleed right up against the edge of the plotting area. I want this effect where there is some space between the last point and the edge of the plot.

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轴刻度,而右侧是此刻一无所有(但可以说是一个传奇).

However, in the following circumstance there are thousands x-axis points, and while visualizing the label of the identity itself is irrelevant, I'd like to avoid having the left and right-most points be squished up to the edges of the plotting area. Note I do not care that the points within the plot are squished together - that's unavoidable and over-plotting could be resolved with an alpha argument or something else. What I want to fix is having the points on the edges of the plot (left and right preferably) to have some buffer between the horizontal planes where on the left side is the y-axis scale, and on the right side is at the moment nothing (but could be, say, a legend).

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

我很想找出可以做些什么来为这些点的水平边缘提供一些缓冲.

I'd love to find out what exactly could be done to give a bit of buffer to the horizontal edges of those points.

感谢您分享您的天才.

推荐答案

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

Because your x-variable is character, ggplot2 creates a 'discrete' x-axis. The default plot limits for a discrete axis make a lot of sense when the number of categories is fairly small (2 - 25). You can use the expand argument to scale_x_discrete to manually adjust the plotting limits. The visual appearance will depend on the size of the points and the graphics device, so you may need to adjust accordingly.

例如,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

For example, scale_x_discrete(expand=c(0.1, 0)) will expand each side of the plot 10% of the data range. While scale_x_discrete(expand=c(0, 2)) will expand each side by 2 (of whatever units x is). Also see 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)

这篇关于扩展ggplot中定性变量的极限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆