对气泡网格图使用渐变颜色填充

时间:2020-02-10 22:41:58

标签: r ggplot2

我已经创建了一个气泡网格图,但是我一辈子都无法更改填充的颜色。我想基于这些值使用彩虹渐变。下面是我的代码,我在输出中附加了图片

setwd("C:/Users/Schelly/Desktop/Projects/Jens_tables_and_figures_2020/Bubble_chart")
library(tidyverse)
library(reshape2)
pc <- read.csv("Para_Bubble_data2.csv", header = TRUE)
head(pc)
pcm<-melt(pc, id = c("Sample"))
pcm$Sample <- factor(pcm$Sample,levels=unique(pcm$Sample))
xx = ggplot(pcm, aes(x = Sample, y = variable)) + 
  geom_point(aes(size = value, fill = value), alpha = 0.75, shape = 21) +
  scale_colour_gradientn(colours=rainbow(4))+
  scale_size_continuous(limits = c(0.000001, 1), range = c(1,17), breaks = c(.01,.10,.50,.75)) + 
  labs( x= "", y = "", size = "Relative Abundance (%)", fill = "")
xx

Output of code

1 个答案:

答案 0 :(得分:1)

如果要使用aes(colour = value),则需要指定scale_color_gradientn

library(ggplot2)

df <- data.frame(x = factor(rep(1:5, each = 6)), 
                 y = factor(rep(1:6, 5)), val = sample(30))

ggplot(df, aes(x = x, y = y, size = val, colour = val)) +
  geom_point() + 
  scale_color_gradientn(colours = c("red", "yellow", "blue"))

enter image description here

如果要使用填充(以保留其他轮廓颜色),则需要使用scale_fill_gradientn

ggplot(df, aes(x = x, y = y, size = val)) +
  geom_point(aes(size = val, fill = val), alpha = 0.75, shape = 21) +
  scale_fill_gradientn(colours = rainbow(4))+
  labs( x= "", y = "", size = "Relative Abundance (%)", fill = "")

enter image description here

相关问题