I am trying to plot London postcodes with ggplot, and I would like the value of colour of each point to vary based on a 3rd variable. The code I have for the initial plot is:
gg <- ggplot() + geom_point(data=joined_data, aes(x=V3, y=V4), color = '#800080', size=0.2, alpha=0.5)
print(gg)
this is all set to a single colour though, how can I set the colour value to vary based on the 3rd variable?
Assuming V5 is your 3rd variable (It must be a column in joined_data.
ggplot(joined_data, aes(x = V3, y = V4, color = V5)) + geom_point()
You must set the colour
aesthetic first, then use a continuous color scale to choose the gradient of colors.
I have used the built-in data set iris
, transformed.
library(ggplot2)
ggplot(df1, aes(Sepal.Length, Sepal.Width, colour = clr)) +
geom_point(size = 1, alpha=0.5) +
scale_color_gradient(low = "#800080", high = "#FF00FF")
Data.
The ave
instruction creates a color column clr
, depending on the maxima of Sepal.Length
by Species
.
df1 <- iris
df1$clr <- ave(df1$Sepal.Length, df1$Species, FUN = function(x){
runif(length(x), 0, max(x))
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.