我创建了这个情节: -
使用此代码: -
ggplot(data = gdt, aes(x = area)) +
geom_histogram(bins = 10, colour = "black", fill = "grey50") +
facet_wrap( ~ fires, scales = "free") +
labs(x = "Area of Burnt Land (ha)", y = "Fires") +
ggtitle("Count of Destructive Fires in Portugal (2007)")
我想为每个子图单独更改刻度线的间隔发生或位置。换句话说,我试图在条形相遇的每个点处实现一个刻度/网格线的放置。我已经尝试使用scale_continuous和scale_x_discrete无效。如果我将标记设置为适合其中一个子图,则会对其他子图产生负面影响。有没有办法做到这一点??
答案 0 :(得分:0)
您的问题分为两部分:
主要想法是使用binwidth
中的boundary
和geom_histogram
参数以及scale_x_continuous
的组合,以使网格线与条形图对齐;然后使用grid.arrange
包中的gridExtra
将图表排列在同一页面上。
一些假数据:
library(ggplot2)
library(gridExtra)
d <- data.frame(x = c(runif(100, 0, 1),
runif(500, 1, 10),
runif(100, 10, 50),
runif(100, 50, 1500)))
cuts <- c(0, 1, 10, 50, 1500)
d$type <- cut(d$x, cuts)
创建ggplot
个列表,每个ggplot
代表数据的子集:
g <- lapply(split(d, d$type), function(dd) {
i <- as.numeric(dd$type)[1]
bw <- (cuts[i + 1] - cuts[i]) / 10
ggplot(dd, aes(x = x)) +
geom_histogram(bins = 10, colour = "black", fill = "grey50",
binwidth = bw, boundary = cuts[i]) +
scale_x_continuous(breaks = seq(cuts[i], cuts[i + 1], bw)) +
labs(y = "", x = "") +
facet_wrap(~ type, scales = "free") # just to create panel
})
在页面上排列子图,并添加标签:
params <- list(top = "Count of Destructive Fires in Portugal (2007)",
left = "Fires",
bottom = "Area of Burnt Land (ha)")
do.call(grid.arrange, c(g, params))