1

大家,我试图改变下一个情节的颜色: 在此处输入图像描述

我尝试了其他解决方案:

my_pal <- colorRampPalette(c("yellow","firebrick2"))

  scale_color_gradientn(colours = my_pal(6))

或在 geom_point 添加 color = ... ,但我的代码有问题这是我的数据示例:

structure(list(Class = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L), .Label = c("Amphibia", "Reptilia", "Diverse"), scores = structure(c(Amphibia = 0.0171454425174863, 

Diverse = 0.0557016619533333, Reptilia = 0.0306895793776627), .Dim = 3L, .Dimnames = list(
    c("Amphibia", "Diverse", "Reptilia"))), class = "factor"), 
cost_bil = c(0.00061474244, 0.00061474244, 0.00333970653, 
0.00333970653, 0.00038007634, 0.00038007634, 0.00013713485, 
0.00013713485, 0.0005509038, 0.0005509038), value = c("Observed", 
"High", "Observed", "High", "Observed", "High", "Observed", 
"High", "Observed", "High")), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame"))

我只选择了 10 列,所以如果可能的话结果不会相同,我使用的代码是:

p9 <-ggplot(expanded_long, aes(x = Class, y = value)) +
 geom_point(aes(size = cost_bil)) +
 facet_grid(name ~ ., switch = "y", scales = "free_y") +
 scale_size_continuous(range = c(1, 20)) +
 labs(x = "Taxonomic Class", y = NULL, size = "US$ billions", color = "US$ billions") +
 theme(
    panel.spacing.y = unit(0, "pt"),
    strip.placement = "outside",
    strip.background.y = element_blank()
  ) + 
  guides(size=guide_legend(reverse = TRUE)) + 
  theme_classic() + 
  scale_size(range=c(5,20))

所有帮助都很重要,我只想更改主图和图例的颜色,非常感谢

4

2 回答 2

1

首先geom_point()添加 color=cost_bil,然后添加guides(colour = guide_legend(reverse=F))

在这里,您有具有相同模式的示例图:

ggplot(head(midwest,100), aes(x=area, y=poptotal)) + 
  geom_point(aes(size=area,color=area)) + 
  scale_colour_continuous(low = '#32CD32', high = '#ff4040') +
  guides(colour = guide_legend(reverse=F))

输出: 在此处输入图像描述

于 2021-07-18T20:03:21.977 回答
1

试试下面的。
请注意,无需使用scale_colour_gradient的参数定义颜色向量lowhigh颜色将自动映射到连续变量cost_bil

library(ggplot2)


ggplot(expanded_long, aes(x = Class, y = value)) +
  geom_point(aes(size = cost_bil, colour = cost_bil)) +
  scale_size_continuous("US$ billions", range = c(1, 20)) +
  scale_colour_gradient("US$ billions", low = "yellow", high = "firebrick2")+
  labs(x = "Taxonomic Class", y = NULL, 
       size = "US$ billions", colour = "US$ billions") +
  guides(size = guide_legend(reverse = TRUE),
         colour = guide_legend(reverse = TRUE)) + 
  facet_grid(name ~ ., switch = "y", scales = "free_y") +
  theme_classic() + 
  theme(
    panel.spacing.y = unit(0, "pt"),
    strip.placement = "outside",
    strip.background.y = element_blank()
  )

非常感谢,但这就是我得到的

于 2021-07-18T19:56:51.537 回答