我首先给你我的示例代码:
x <- runif(1000,0, 5)
y <- c(runif(500, 0, 2), runif(500, 3,5))
A <- data.frame("X"=x,"Y"=y[1:500])
B <- data.frame("X"=x,"Y"=y[501:1000])
ggplot() +
stat_bin_hex(data=A, aes(x=X, y=Y), bins=10) +
stat_bin_hex(data=B, aes(x=X, y=Y), bins=10) +
scale_fill_continuous(low="red4", high="#ED1A3A")
Run Code Online (Sandbox Code Playgroud)
现在我希望下六边形遵循不同的比例.即从深绿色到浅绿色.我怎样才能做到这一点?
更新: 正如您从目前为止的答案中所看到的,我问自己是否有一个没有使用alpha标度的解决方案.此外,使用两个没有边距或类似情况的图表不适合我的特定应用.虽然他们都是合法的答案:)
您可以在构建绘图后更改较低值的颜色,而不是尝试在一个绘图中获得两种不同的填充比例。基本思想是拥有两个具有不同填充比例的图,然后将某些细节从一个图复制到另一个图。
# Base plot
p <- ggplot() +
stat_bin_hex(data=A, aes(x=X, y=Y), bins=10) +
stat_bin_hex(data=B, aes(x=X, y=Y), bins=10)
# Produce two plots with different fill colours
p1 <- p + scale_fill_continuous(low="red4", high="#ED1A3A")
p2 <- p + scale_fill_continuous(low="darkgreen", high="lightgreen")
# Get fill colours for second plot and overwrite the corresponding
# values in the first plot
g1 <- ggplot_build(p1)
g2 <- ggplot_build(p2)
g1$data[[1]][,"fill"] <- g2$data[[1]][,"fill"]
# You can draw this now but there is only one legend
grid.draw(ggplot_gtable(g1))
Run Code Online (Sandbox Code Playgroud)
要拥有两个图例,您可以将两个图例中的图例连接在一起
# Bind the legends from the two plots together
g1 <- ggplot_gtable(g1)
g2 <- ggplot_gtable(g2)
g1$grobs[[grep("guide", g1$layout$name )]] <-
rbind(g1$grobs[[grep("guide", g1$layout$name )]],
g2$grobs[[grep("guide", g2$layout$name )]] )
grid.newpage()
grid.draw(g1)
Run Code Online (Sandbox Code Playgroud)
给予(set.seed(10)数据生成之前)
| 归档时间: |
|
| 查看次数: |
199 次 |
| 最近记录: |