GGplot 对点使用连续颜色,但对文本使用离散颜色

问题描述 投票:0回答:1

我有一个问题,我想制作一个 geom_point 图形并让点根据变量点改变颜色。但我想要旁边有一个文本框,说明它们是选择红色还是绿色。

我尝试了以下代码。

    ggplot(dataset2, aes(x = x_value, y = y_value, size = Points)) +
    geom_text_repel(aes(label = Name, color = Chosen), size = 5) +
    geom_point(aes(color = as.numeric(Student_votes))) +  
    scale_color_continuous(name = "Points") +
    theme_bw() 

这给出了提供给连续刻度的离散值的误差

这是一个数据集来显示我想要的内容

dataset2 <- data.frame(
  x_value = c(1, 2, 3, 4, 5),
  y_value = c(10, 15, 8, 12, 20),
  Points = c(50, 30, 45, 60, 40),
  Name = c("A", "B", "C", "D", "E"),
  Chosen = c("Yes", "No", "Yes", "No", "Yes"),
  Student_votes = c(15, 20, 10, 25, 18))

因此,积分可能会根据积分值而变化,但“选择”应为绿色(表示“是”)和红色(表示“否”)。

这可能吗?

r ggplot2
1个回答
0
投票

您可以使用

ggnewscale
套件:

library(ggrepel)
library(ggnewscale)

ggplot(dataset2, aes(x = x_value, y = y_value, size = Points)) +
  geom_point(aes(color = as.numeric(Student_votes))) +  
  scale_color_continuous(name = "Points") +
  new_scale_color() +
  geom_text_repel(aes(label = Name, color = Chosen), size = 5) +
  scale_color_manual(values = c("red3", "green4")) +
  theme_bw() 

© www.soinside.com 2019 - 2024. All rights reserved.