微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

更改基本几何图形中的颜色

如何解决更改基本几何图形中的颜色

大家,我正在尝试为下一个情节更改颜色:

enter image description here

我尝试了其他解决方案:

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

  scale_color_gradientn(colours = my_pal(6))

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

structure(list(Class = structure(c(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.00038007634,0.00013713485,0.0005509038,0.0005509038),value = c("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))

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

解决方法

请尝试以下操作。
请注意,无需定义颜色向量,使用 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()
  )

thanks so much,but this is what I got

,

首先向 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))

输出: enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。