ggplot-仅绘制颜色栏

时间:2019-02-26 17:35:13

标签: r ggplot2 colorbar

如果可能的话,我只希望在窗口中心绘制颜色条,就像这样:

enter image description here

一个无法删除点,轴和背景的示例。

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

df <- data.frame(x = c(1,2,3,4,5,6), y = c(7,4,9,2,6,7))

ggplot(data = df, aes(x = x, y = y, colour = y)) + 
  geom_point() +
  scale_color_viridis()

1 个答案:

答案 0 :(得分:1)

ggpubr软件包具有满足此需求的功能。

df <- data.frame(x = c(1,2,3,4,5,6), y = c(7,4,9,2,6,7))

p <- ggplot(data = df, aes(x = x, y = y, colour = y)) + 
  geom_point() +
  scale_color_viridis() +
  theme_minimal()

# ggpubr does this for you
library(ggpubr)
leg <- get_legend(p)
as_ggplot(leg)

enter image description here