我尝试了其他解决方案:
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))
所有帮助都很重要,我只想更改主图和图例的颜色,非常感谢