0
gfg_plot <- ggplot(NULL, aes(x = Area, y = Richness, fill = District )) +
geom_point(data=District1, col="blue") +
geom_point(data=District2, col="black")+
geom_point(data=District3, col="Purple")+
geom_point(data=District4, col="Green")+
geom_point(data=District5, col="Orange")+
geom_point(data=District6, col="Cyan")+
geom_point(data=District7, col="coral")+
geom_point(data=District8, col="red")+
geom_point(data=District9, col="skyblue")+
theme_few() +
xlab("Area")+
ylab("Average richness")+
labs(title = "PingshanSAR_plot")+
scale_x_log10(breaks = trans_breaks("log10", function(x) 10^x),
labels = trans_format("log10", math_format(10^.x))) +
scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),
labels = trans_format("log10", math_format(10^.x))) +
theme(plot.title = element_text(size = 15),
legend.title = element_blank(),
legend.text = element_text(color = NULL))

plot(gfg_plot)

The outcome: main part of the plot looks just fine, but in the legend, all the points are in light blue. How can I make the colour of each legend into the colour I use for each dataset?

I also tried to add this in above, but it is not working.

scale_fill_manual(name = "District",
                     values = c("District1" = "blue", "District8" = "red", 
"District2"= "black","District3" = "Purple", "District6" = "Cyan" ,
"District4" = "Green","District5"= "Orange", "District7"="coral", 
"District9"= "skyblue"))+

I would like to find a way to change the colour of my legend and use the same colour as each dataset use without merging or manipulate the data.

Plot that I generate through above code Plot that generated from above code

stefan
  • 90,330
  • 6
  • 25
  • 51
  • If you want a legend you have to map on aesthetics, i.e. move `color` inside `aes()`, i.e. use e.g. `geom_point(data=District1, aes(color ="District1"))`. And after doing so, your `scale_color_manual` should work fine. However, a much easier approach would be to use e.g. `bind_rows` to create one dataset of your nine. – stefan Aug 15 '23 at 11:28
  • Thank you for your help, I come across a problem that when I put colour in aes like `geom_point(data=District1, aes(color ="District1"))`, the colour on the map is not the one I put in `scale_color_manual`, just default gradient colour and legend's name become "colour" instead of "District". Do you know where I can fix this? – Wanqing Tai Aug 17 '23 at 03:05
  • I also tried to bind the row to see how it works `df_SAR_All <- rbind(District1,District2,District3,District4,District5,District6,District7,District8,District9)` `gfgplot <- ggplot(data = df_SAR_All) + geom_point(aes(x = GridArea_km2 , y = Ave_richness, colour = District)) +` and with all the rest after `theme_few()`, but the output is the same that plot use default gradient colour rather than `scale_color_manual` colour I set – Wanqing Tai Aug 17 '23 at 03:11
  • For your reference , my original data set like following: `District1 <- data.frame( Area = c(10,20,30,40,50,60), Richness = c(100,200,300,350,400,430), District =c("District1","District1","District1","District1","District1","District1" ))` All the dataset are in same structure but in different length. – Wanqing Tai Aug 17 '23 at 03:33
  • Solved the problem, the problem is I should use `scale_colour_manual` instead of `scale_fill_manual`. It's working fine now. – Wanqing Tai Aug 22 '23 at 10:19

0 Answers0