嘗試這種情況:
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)引入縮放着色,這樣我使用現有研發啤酒規模,例如,「藍色」,顏色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
這真是太神奇了,非常感謝。只有一個問題 - 如果我想爲標籤着色以及點怎麼做?謝謝@HAVB –
@HenryCann在'geom_text'行添加'aes(color = whateverVariable)'。檢查這裏的例子:http://docs.ggplot2.org/current/geom_text.html – HAVB
謝謝,我已經能夠添加顏色的標籤,但不是作爲一個規模?我可以將scale_colour_gradient參數添加到geom_text作爲參數嗎?歡呼@HAVB –