R ggplot viridis更改颜色渐变

时间:2020-08-07 11:03:25

标签: r ggplot2 viridis

您好,我正在使用R ggplot绘制散点图,以便使用virdis进行着色,我想拆分颜色图例以在当前具有1000,2000,3000,4000的图例范围之间获得更多的对比度。代码如下所示

library(dplyr)
library(ggplot2)
library("viridis")

df <- tibble(gene   = sample.int(5000),aceth  = rnorm(5000),acvitd = rnorm(5000))
df$log_mean=log(df$gene)
p=ggplot(df, aes(aceth, acvitd))+
  geom_point(aes(color =gene)) +
  theme(legend.position = "top")+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"))

p=p+ theme(legend.position = "top") +
     scale_color_viridis(option = "A",
     name = "mean",
     guide = guide_colourbar(direction = "horizontal",
     barheight = unit(4, units = "mm"),
     barwidth = unit(100, units = "mm"),
     draw.ulim = F,
     title.hjust = 0.5,
     label.hjust = 0.5, title.position = "top"))

我希望我的图看起来像

enter image description here

有人可以建议我

1 个答案:

答案 0 :(得分:0)

也许您正在寻找scale_colour_vridis_b来对颜色进行分类?

df <- tibble(gene   = sample.int(5000),aceth  = rnorm(5000),acvitd = rnorm(5000))
df$log_mean=log(df$gene)
p=ggplot(df, aes(aceth, acvitd))+
  geom_point(aes(color = - (aceth + acvitd))) +
  theme(legend.position = "top")+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"))

p+ theme(legend.position = "top") +
     scale_colour_viridis_b(option = "A", values = 0:10 /10, breaks = 0:5 - 2.5)

enter image description here