簡體   English   中英

如何在不同的幾何體中具有相同的美學(顏色)的不同尺度?

[英]How to have different scales the same aesthetic (color) in different geoms?

這段代碼顯然不起作用(它對兩個分類變量使用相同的圖例和配色方案。

require(ggplot2)

dt <- ggplot2::diamonds ; dt <- dt [1:20,];dt 
ggplot(dt) + 
  geom_point(aes(depth,carat, col=cut)) + 
  geom_point(aes(depth,carat, col=clarity)) +
  scale_colour_brewer("greens", name="cut") +  
  scale_colour_brewer("Reds", name="cut") +
  guides(colour= guide_legend("CUT")) + 
  guides(colour = guide_legend("CLARITY"))

繪制此圖的正確方法是什么?

沒有正確的方法可以做到這一點。 Ggplot 不打算以這種方式使用,因為您試圖將兩個變量映射到相同的比例。 但是,您可以通過劫持填充比例來為您完成這項工作,從而在一定程度上規避 ggplot 的限制:

ggplot(dt) +
  geom_point(aes(depth, carat, fill = cut), shape = 21, colour = "transparent") +
  geom_point(aes(depth, carat, colour = clarity)) +
  scale_colour_brewer(palette = "Greens", name = "cut") +
  scale_fill_brewer(palette = "Reds", name = "clarity")

在此處輸入圖片說明

訣竅是使用具有填充的形狀並使用該填充來映射您的變量。 缺點是這個技巧不能擴展到任何數量的變量。 有幾個包在那里,可以達到你想要的東西,即ggnewscalerelayer

ggnewscale 包的示例:

library(ggnewscale)

ggplot(dt) +
  geom_point(aes(depth, carat, colour = cut)) +
  scale_colour_brewer(palette = "Greens", name = "cut") +
  new_scale_color() +
  geom_point(aes(depth, carat, colour = clarity)) +
  scale_colour_brewer(palette = "Reds", name = "clarity")

在此處輸入圖片說明

對於中繼器變體:

library(relayer)

ggplot(dt) +
  rename_geom_aes(geom_point(aes(depth, carat, cut = cut)), new_aes = c("colour" = "cut")) +
  rename_geom_aes(geom_point(aes(depth, carat, clarity = clarity)), new_aes = c("colour" = "clarity")) +
  scale_colour_brewer(palette = "Greens", aesthetics = "cut") +
  scale_colour_brewer(palette = "Reds", aesthetics = "clarity")
Warning: Ignoring unknown aesthetics: cut
Warning: Ignoring unknown aesthetics: clarity

在此處輸入圖片說明

希望這有幫助!

編輯:顯然在上面的圖中,只有一種顏色顯示在點上,因為您在彼此之上繪制了相同的 x 和 y 坐標。 我覺得我需要指出這一點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM