根据Y值ggplot的颜色散点图

时间:2017-03-31 13:51:41

标签: r ggplot2 scatter-plot

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)将所有Y值着色超过75点红色?

2)引入缩放着色,以便使用现有的R啤酒制造商,例如“蓝调”,根据其价值为Y值着色?

我尝试将每个y值分配给1到10的点,然后使用参数size = factor(Z),但这不起作用。

谢谢。

1 个答案:

答案 0 :(得分:1)

试试这个:

1)将所有Y值着色超过75点红色

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啤酒制造商,例如“蓝调”,根据其价值为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