How to avoid overlapping legends for multiple geom_point layers in ggplot2

I’m trying to create a chart with 2 geom_points and a geom_text_repel layer. When I display my legend, the geom_points overlap. The main data points should be represented as empty circles, while the "region"-specific points should be filled circles. Additionally, the points corresponding to regions are not displayed correctly in the legend. library(ggplot2) library(ggrepel) library(stringr)… Read More How to avoid overlapping legends for multiple geom_point layers in ggplot2

How to position the legend at the bottom after adjusting plot.margin in ggplot2?

In ggplot2, I would like to increase the bottom plot margin (using the plot.margin argument of theme()) and position the legend at the bottom of the plot window. Consider the following example code: library("ggplot2") d <- data.frame(x = c(8,6,5,1,8,9,6,4), y = c(6,5,4,3,7,1,6,4), v = rep(c("a","b"), each = 4)) p <- ggplot(d, aes(x, y, colour =… Read More How to position the legend at the bottom after adjusting plot.margin in ggplot2?

remove part of color legend while keeping the colors in ggplot

I would like to remove part of a color legend while keeping the manual color I set, and I do not manage to do that. Here is an example: library(data.table) library(ggplot2) # create dummy data df <- data.table(x = rep(1:10,10),year = rep(2011:2020,each = 10)) df[,y := rnorm(.N,year[1] – 2011,.5),year] unique_years <- df$year %>% unique() #… Read More remove part of color legend while keeping the colors in ggplot

ggplot – ordering legends with guides changes continuous legend to discrete

I’m running ggplot2 v3.4.1. I created this 2 legend plot that by default it is placing the year2 size legend below the cty color legend. However, I would like the size legend to be on top. library(tidyverse) mpg$year2 = factor(mpg$year) values = c(2,4); names(values) = c("1999", "2008") p = mpg %>% ggplot(aes(x = cty, y… Read More ggplot – ordering legends with guides changes continuous legend to discrete

How to change colors when using scale_fill_discrete in R?

I have the data below: time=c(200,218,237,237,237,237,237,246,246,246,257,257,257,272,272,272,294,294,294) location=c("A","A","D","C","A","B","B","D","C","B","D","C","B","D","C","B","D","C","B") value=c(0,774,0,0,2178,0,2178,0,1494,2644,1326,1504,4188,3558,1385,5013,12860,829,3483) dataA=data.frame(time,location,value) and I made a graph. ggplot(data=dataA, aes(x=time, y=value))+ geom_area(aes(group=location, fill=location), position="stack", linetype=1, size=0.5 ,colour="black") + scale_fill_discrete(breaks=c("A","B","C","D"), labels=c("Main_town","B","C","D"))+ theme_classic(base_size=18, base_family="serif")+ theme(legend.position="right", axis.line=element_line(linewidth=0.5, colour="black"))+ windows(width=5.5, height=5) I changed one of the legend label from A to main_town using scale_fill_discrete(). Then color is automatically generated. I want to change… Read More How to change colors when using scale_fill_discrete in R?