'Using colours in ggplot

Using the following code generates this plot

ggplot(df_44, aes(x = latest_tli_date, y = farmer_created_at, fill = ward_id)) +
  geom_point()+ scale_color_brewer(palette = "BuPu")

I need the 4 wards to have different colours. What am I doing wrong?

enter image description here



Solution 1:[1]

try using the fill argument in the geom_point function

Edit: I just tried an example code, the fill and col arguments is going to color it with on a continuous scale because the data is continuous.

If you factor your value you want to color, it should color it discretely.

ggplot(df_44, aes(x = latest_tli_date, y = farmer_created_at)) +
  geom_point(aes(col = factor(ward_id))

Solution 2:[2]

Use colour aesthetics: If ward_id is numeric then you might want to replace by colour = factor(ward_id):

Have a look here: https://ggplot2.tidyverse.org/reference/scale_brewer.html

library(ggplot2)

ggplot(df_44, aes(x = latest_tli_date, y = farmer_created_at, colour = ward_id)) +
  geom_point()+ scale_color_brewer(palette = "BuPu")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nad Pat
Solution 2 TarJae