2017-03-31 186 views
1

My scatterplot 我不是最好的R,但我試圖將顏色引入這個情節。顏色散點圖根據Y值ggplot

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

嘗試這種情況:

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 
+0

這真是太神奇了,非常感謝。只有一個問題 - 如果我想爲標籤着色以及點怎麼做?謝謝@HAVB –

+0

@HenryCann在'geom_text'行添加'aes(color = whateverVariable)'。檢查這裏的例子:http://docs.ggplot2.org/current/geom_text.html – HAVB

+0

謝謝,我已經能夠添加顏色的標籤,但不是作爲一個規模?我可以將scale_colour_gradient參數添加到geom_text作爲參數嗎?歡呼@HAVB –