如何将两个不同比例的颜色渐变与ggplot合并 [英] How merge two different scale color gradient with ggplot
问题描述
通过使用R,是否可以将2个ggplot放在一起(即,在同一图上),但是具有不同的颜色渐变条形?我的代码,例如
By using R, is it possible to place 2 ggplot together (i.e., on the same plot) but with different bar of color gradient? My code, e.g.,
library(ggplot2)
ggplot(df1, aes(duration, slopes, col = color)) +
geom_point(size = 3) +
scale_color_gradient(low = "black", high = "red")
ggplot(df2, aes(duration, slopes, col = color)) +
geom_point(size = 3) +
scale_color_gradient(low = "blue", high = "green")
产生以下两张图片
我希望能够将它们整合在一起,在一个图中用红色和黑色的条形图和蓝色和绿色的另一条形图.
I wish instead to be able to integrate them together in one plot with a bar for red and black and another bar for blue and green.
推荐答案
是的,如果您使用 ggnewscale
包,则可以:
Yes you could if you use the ggnewscale
package:
a <- sample(nrow(iris), 75)
df1 <- iris[a,]
df2 <- iris[-a,]
library(ggnewscale)
ggplot(mapping = aes(Sepal.Width, Sepal.Length)) +
geom_point(data = df1, aes(colour = Petal.Length)) +
scale_colour_gradientn(colours = c("red", "black")) +
# Important: define a colour/fill scale before calling a new_scale_* function
new_scale_colour() +
geom_point(data = df2, aes(colour = Petal.Width)) +
scale_colour_gradientn(colours = c("blue", "white"))
替代方法是 relayer 包,或者是 scale_colour_multi
/ scale_listed
来自 ggh4x (完整免责声明:我写了ggh4x).
Alternatives are the relayer package, or the scale_colour_multi
/scale_listed
from ggh4x (full disclaimer: I wrote ggh4x).
以下是替代方法:
library(ggh4x)
# ggh4x scale_colour_multi (for gradientn-like scales)
ggplot(mapping = aes(Sepal.Width, Sepal.Length)) +
geom_point(data = df1, aes(length = Petal.Length)) +
geom_point(data = df2, aes(width = Petal.Width)) +
scale_colour_multi(colours = list(c("red", "black"), c("blue", "white")),
aesthetics = c("length", "width"))
# ggh4x scale_listed (for any non-position scale (in theory))
ggplot(mapping = aes(Sepal.Width, Sepal.Length)) +
geom_point(data = df1, aes(length = Petal.Length)) +
geom_point(data = df2, aes(width = Petal.Width)) +
scale_listed(list(
scale_colour_gradientn(colours = c("red", "black"), aesthetics = "length"),
scale_colour_gradientn(colours = c("blue", "white"), aesthetics = "width")
), replaces = c("colour", "colour"))
library(relayer)
# relayer
ggplot(mapping = aes(Sepal.Width, Sepal.Length)) +
rename_geom_aes(geom_point(data = df1, aes(length = Petal.Length)),
new_aes = c("colour" = "length")) +
rename_geom_aes(geom_point(data = df2, aes(width = Petal.Width)),
new_aes = c("colour" = "width")) +
scale_colour_gradientn(colours = c("red", "black"), aesthetics = "length",
guide = guide_colourbar(available_aes = "length")) +
scale_colour_gradientn(colours = c("blue", "white"), aesthetics = "width",
guide = guide_colourbar(available_aes = "width"))
所有其他选择都会发出有关未知美学的警告,但这与生成的图无关紧要.这只是ggplot的 layer()
函数中的一行代码,会产生此警告,如果不重新编码每个 geom
包装器或ggnewscale确实做到了,重命名了旧的美学而不是提供了新的美学.这些图看起来都差不多,因此我认为不必再次发布它们.
All the alternatives give warnings about unknown aesthetics, but this doesn't matter for the resulting plots. It is just a line of code in ggplot's layer()
function that produces this warning and you can't go around this without either re-coding every geom
wrapper or, as ggnewscale does, renaming the old aesthetic instead of providing a new aesthetic. The plots all look near-identical, so I figured I wouldn't have to post them again.
这篇关于如何将两个不同比例的颜色渐变与ggplot合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!