我创建了一个用于创建条形图的功能ggplot
.
在我的图中,我想在刻度线的位置用白色水平条覆盖图,如下图所示
p <- ggplot(iris, aes(x = Species, y = Sepal.Width)) +
geom_bar(stat = 'identity')
# By inspection I found the y-tick postions to be c(50,100,150)
p + geom_hline(aes(yintercept = seq(50,150,50)), colour = 'white')
Run Code Online (Sandbox Code Playgroud)
但是,我希望能够更改数据,因此我不能像示例中那样使用静态位置.例如,我可能会在上面的示例中更改Sepal.With
为Sepal.Height
.
你能告诉我怎么做:
ggplot
用于刻度位置的函数,以便我可以使用它来定位我的线.所以我可以做点什么
tickpositions <- ggplot_tickpostion_fun(iris$Sepal.Width)
p + scale_y_continuous(breaks = tickpositions) +
geom_hline(aes(yintercept = tickpositions), colour = 'white')
Run Code Online (Sandbox Code Playgroud) 我制作了一个类似于下面代码所描述的图,从而生成了发布的图像。我不知道如何将整个背景设置为与定义子图时使用的相同的“grey80”颜色。IE。我想用相同的颜色为绘图之间和图例两侧的白色区域着色。这是否有可能实现,也许是通过一些奇特的 gridgrob-magic?
这个问题类似于更改 grid.arrange 输出的背景颜色,但如果可能的话,我想要一个不使用 png() 函数的解决方案
library(ggplot2)
library(gridExtra)
library(ggthemes)
library(grid)
p1 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width,
colour = Species)) +
ggtitle('Sepal') +
geom_point() + theme_bw() +
theme(rect = element_rect(fill = 'grey80'))
p2 <- ggplot(iris, aes(x = Petal.Length, y = Petal.Width,
colour = Species)) +
ggtitle('Petal') +
geom_point() + theme_bw() +
theme(rect = element_rect(fill = 'grey80'))
grid_arrange_shared_legend <- function(...) {
plots <- list(...)
g <- ggplotGrob(plots[[1]] + theme(legend.position = "bottom"))$grobs
legend <- g[[which(sapply(g, function(x) x$name) …
Run Code Online (Sandbox Code Playgroud)