extend limit for qualitative variable in ggplot
Hi everyone and thanks for your consideration,
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.
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())
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.
Thanks for sharing your genius.
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.
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)
链接地址: http://www.djcxy.com/p/24934.html
上一篇: 绘制3个与前一列相关的分类变量
下一篇: 扩展ggplot中定性变量的限制