气泡图中气泡的自定义颜色

时间:2017-11-22 18:02:34

标签: r ggplot2

我有以下示例数据:

library(ggplot2)

count <- c(100,150,112)
cat <- c("A", "B", "C")
sentiment <- c(-0.3, 0.2, 0.8)
duration <- c(5.6, 8.2,5.2)
silence <- c(0.2, 0.05,0.1)

df <- data.frame(count, cat, sentiment, duration, silence)

我使用该数据创建以下气泡图

p6 <- ggplot(df, aes(x = duration, y = silence, size = count, colour = sentiment)) + geom_point()
p7 <- p6 + scale_size_continuous(range = c(5, 8))
p7 + scale_x_continuous(limits = c(2, 10)) + scale_y_continuous(limits = c(0,0.25)) + theme(legend.position="none")

这一切都很好,但问题是我想要从绿色(情感= 1)到红色(情绪= -1)的颜色点。

有关如何做到的任何想法?

1 个答案:

答案 0 :(得分:0)

分别将添加scale_color_continuous红色绿色

+ scale_color_continuous(low = "red", high = "green")

给出:

enter image description here