根据Y值绘制散点图ggplot

Colour Scatterplot according to Y value ggplot

My scatterplot 我不是最擅长 R 的人,但我正在尝试将颜色引入到这个情节中。

SecondPlot <- ggplot(sur11, aes(x=standec, y=compnoanti)) +
geom_point(col ="black", size = 0.5) +
geom_text(label=sur11$concatena, hjust = 0, nudge_x = 0.3, size = 2.5)
SecondPlot
SecondPlot + geom_abline(intercept = 50, slope = 0, size = 0.2)

我怎样才能:

1) 将所有超过 75 点的 Y 值涂成红色?

2) 引入比例着色,以便我使用现有的 R brewer 比例,例如 "Blues",根据它们的值对 Y 值着色?

我曾尝试将每个 y 值分配给 1 到 10 之间的一个点,然后使用参数 size = factor(Z) 但这没有用。

谢谢。

试试这个:

1) 将所有超过 75 点的 Y 值涂成红色

SecondPlot <- ggplot(sur11, aes(x=standec, y=compnoanti)) +
  geom_point(col = ifelse(sur11$compnoanti > 75, "red", "black"), size = 0.5) +
  geom_text(label=sur11$concatena, hjust = 0, nudge_x = 0.3, size = 2.5)
SecondPlot

2) 引入比例着色,以便我使用现有的 R brewer 比例,例如 "Blues",根据它们的值对 Y 值着色?

#Interpolate Brewer palette
library(RColorBrewer)
colourCount = length(unique(sur11$compnoanti))
getPalette = colorRampPalette(brewer.pal(9, "Blues"))


SecondPlot <- ggplot(mtcars, aes(x=standec, y=compnoanti)) +
  geom_point(aes(col = compnoanti), size = 0.5) +
  geom_text(label=sur11$concatena, hjust = 0, nudge_x = 0.3, size = 2.5) +
  scale_colour_gradientn(colours=getPalette(colourCount))
SecondPlot

由于您没有提供可重现的示例,我使用通用数据集对其进行了测试。这是通用版本:

# Color points above a certain value red
SecondPlot <- ggplot(mtcars, aes(x=mpg, y=disp)) +
  geom_point(col = ifelse(mtcars$disp > 120, "red", "black"), size = 0.5) +
  geom_text(label=rownames(mtcars), hjust = 0, nudge_x = 0.3, size = 2.5)
SecondPlot


#Interpolate Brewer palette
library(RColorBrewer)
colourCount = length(unique(mtcars$disp))
getPalette = colorRampPalette(brewer.pal(9, "Blues"))


SecondPlot <- ggplot(mtcars, aes(x=mpg, y=disp)) +
  geom_point(aes(col = disp), size = 0.5) +
  geom_text(label=rownames(mtcars), hjust = 0, nudge_x = 0.3, size = 2.5) +
  scale_colour_gradientn(colours=getPalette(colourCount))
SecondPlot