Home > front end >  Highlight a single data point in a bubble plot
Highlight a single data point in a bubble plot

Time:01-23

I'm trying to tidy up my plot. There are 3 things i'm struggling with (I'm a bit novice!)

  1. In the top right quadrant, I want to enlarge the text label for the purple bubble so the word 'Hamble' really stands out. I've had several failed attempts at using subset but can't get it to work.
  2. From my code, the red lines should be dashed and they're both showing as a solid line
  3. I want to change the font size of the axis titles but i only seem to be able to do in on the axis labels.

Here's my code:

install.packages("ggrepel")
library(ggrepel)
install.packages("gghighlight")
library(gghighlight)
options(ggrepel.max.overlaps = Inf)
data %>%
  arrange(desc(PM10.2024)) %>%
  mutate(Name = factor(Name, Name)) %>%
  ggplot(aes(x=PM10.2024, y=PM2.5.2024, size = PM2.5.2024, fill=Classification))  
  geom_point(alpha=0.6, shape=21, color="black")  
  scale_size(range = c(10,40), name=bquote(PM[2.5]))   
  scale_fill_viridis(discrete=TRUE, guide=FALSE, option="D")  
  theme_ipsum()  
  xlab(expression(Concentration~PM[10]))  
  ylab(expression(Concentration~PM[2.5]))  
  theme(legend.position = "none")  
  theme(axis.text.x = element_text(size=12, angle = 0, vjust = 0.5, hjust=1))  
  theme(axis.text.y = element_text(size=12, angle = 0, vjust = 0.5, hjust=1))  
  geom_hline(aes(yintercept=7.9, linetype="dashed", color="red",)) 
  geom_vline(aes(xintercept = 12.9, colour="red", linetype="dashed"))   
  geom_text_repel(aes(label = Name), fontface=1, size = 3.9, family = "Calibri")  #adds bubble labels
  scale_y_continuous(limits = c(5, 13)) 
  scale_x_continuous(limits = c(5, 20))

enter image description here

Here is the data from put:

structure(list(Quarry = c("Roke Manor, Romsey", "Grunden, Frith End", "Blashford, Ringwood", "Masters Quarry", "Summerleaze, Uxbridge", "Moreton C Cullimore Gravel, Swindon", "Aylesford Quarry ", "Berkswell Quarry & Recovery Site ", "Bramshill Quarry & Concrete Plant ", "Cromwell Quarry ", "Datchet Quarry and Recovery Site ", "Denge Quarry ", "Dove Holes Quarry, Asphalt Plant & Dry Silo Mortar ", "East Leake Quarry ", "Halkyn Quarry and Asphalt Plant ", "Hamer Warren Quarry and Recovery Site ", "Hatfield Concrete Plant & Quarry ", "Hints Quarry ", "Kingsmead Concrete Plant and Quarry ", "Rugeley Quarry ", "Ryall Concrete Plant and Quarry ", "Sandgate Quarry ", "Spaunton Quarry ", "Swinderby Quarry ", "West Heath Quarry ", "Willington Quarry ", "Hamble"), Operator = c("Raymond Brown", "Grundon", "Tarmac", "NMSB", "Summerleaze", "Moreton", "CEMEX", "CEMEX", "CEMEX", "CEMEX", "CEMEX", "CEMEX", "CEMEX", "CEMEX", "CEMEX", "CEMEX", "CEMEX", "CEMEX", "CEMEX", "CEMEX", "CEMEX", "CEMEX", "CEMEX", "CEMEX", "CEMEX", "CEMEX", "CEMEX"), X.1x1 = c(432500L, 481500L, 414500L, 387500L, 504500L, 412500L, 573500L, 422500L, 478500L, 479500L, 499500L, 608500L, 408500L, 456500L, 320500L, 413500L, 518500L, 415500L, 502500L, 401500L, 386500L, 512500L, 471500L, 489500L, 478500L, 428500L, 447500L), Y.1x1 = c(122500L, 139500L, 107500L, 88500L, 184500L, 196500L, 159500L, 281500L, 159500L, 361500L, 177500L, 119500L, 377500L, 324500L, 372500L, 108500L, 208500L, 303500L, 175500L, 318500L, 240500L, 112500L, 486500L, 362500L, 122500L, 328500L, 110500L), Classification = c("Rural", "Rural", "Rural", "Rural", "Urban", "Rural", "Urban", "Rural", "Rural", "Rural", "Urban", "Rural", "Rural", "Rural", "Rural", "Rural", "Urban", "Rural", "Urban", "Rural", "Rural", "Rural", "Rural", "Rural", "Rural", "Rural", "Hamble"), UK.Urban.mean.in.2021 = c(7.9, 7.9, 7.9, 7.9, 7.9, 7.9, 7.9, 7.9, 7.9, 7.9, 7.9, 7.9, 7.9, 7.9, 7.9, 7.9, 7.9, 7.9, 7.9, 7.9, 7.9, 7.9, 7.9, 7.9, 7.9, 7.9, NA ), PM10.2023 = c(12.3, 12.1, 11.8, 12.3, 15.2, 13.1, 15.2, 12.2, 14, 15.7, 16.7, 11.4, 9.1, 14.2, 10.9, 11.5, 14.5, 13.4, 14, 10.2, 12.5, 13.8, 12.1, 14.4, 12.7, 13.5, NA), PM10.2024 = c(12.2, 12, 11.6, 12.2, 15, 12.9, 15, 12, 13.8, 15.6, 16.6, 11.3, 9.1, 14, 10.8, 11.4, 14.3, 13.3, 13.8, 10.1, 12.4, 13.6, 12, 14.3, 12.5, 13.3, 15.2), PM10.2025 = c(12, 11.8, 11.4, 12, 14.8, 12.8, 14.8, 11.9, 13.6, 15.4, 16.4, 11.1, 9, 13.9, 10.7, 11.2, 14.1, 13.2, 13.6, 10, 12.3, 13.4, 11.9, 14.1, 12.4, 13.2, NA), PM2.5.2023 = c(8.1, 8, 7.7, 7, 10.2, 8, 9.7, 7.7, 8.7, 8.4, 11.2, 7.5, 5.6, 8, 6.7, 7.6, 9.3, 8.2, 9.6, 6.6, 8, 8.3, 6.7, 8.1, 8, 8.2, NA), PM2.5.2024 = c(7.9, 7.9, 7.6, 6.9, 10.1, 7.9, 9.6, 7.6, 8.6, 8.3, 11.1, 7.4, 5.6, 7.9, 6.6, 7.5, 9.2, 8.1, 9.4, 6.5, 7.9, 8.2, 6.6, 8, 7.8, 8.1, 10.1), PM2.5.2025 = c(7.8, 7.8, 7.5, 6.8, 9.9, 7.8, 9.4, 7.5, 8.5, 8.2, 10.9, 7.2, 5.5, 7.8, 6.5, 7.3, 9, 8, 9.3, 6.4, 7.8, 8, 6.5, 7.9, 7.7, 8, NA), Name = c("Roke Manor", "Frith End", "Blashford", "Masters", "Summerleaze", "Moreton", "Aylesford Quarry ", "Berkswell", "Bramshill", "Cromwell", "Datchet", "Denge", "Dove Holes", "East Leake", "Halkyn Quarry", "Hamer Warren", "Hatfield", "Hints", "Kingsmead", "Rugeley", "Ryall", "Sandgate", "Spaunton", "Swinderby", "West Heath", "Willington", "Hamble"), X = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), X.1 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), class = "data.frame", row.names = c(NA, -27L))

CodePudding user response:

You could split the DF into one part with and another part without the data from Hamble. And then plot the two DF separately.

That is how I did it below. First DF is filtered without Hamble and the other one contains just the Hamble part of the original. (I do not think that arrange is needed here.)

ggplot is then called with df_woHamble (without Hamble). Just after geom_point another geom_point is called with data coming from df_Hamble. This has the effect that the last geom_point is plotted on top fo the others.

The same concept is applied to geom_label_repel.Here I have in addition to the large font size a red color chosen.

What the dashed lines in is concerned, you just have to place Linotype outside of aes.

library(tidyverse)
library(ggrepel)
library(gghighlight)
library(viridis)

options(ggrepel.max.overlaps = Inf)

## without Hamble
df_woHamble <- df |>
  #arrange(desc(PM10.2024))  |> 
  mutate(Name = factor(Name, Name)) |> 
  filter(Name != "Hamble")
## with Hamble
df_Hamble <- df |>
  #arrange(desc(PM10.2024))  |>
  mutate(Name = factor(Name, Name)) |>
  filter(Name == "Hamble")

ggplot(df_woHamble, aes(x = PM10.2024, y = PM2.5.2024, size = PM2.5.2024, fill = Classification))  
  geom_point(alpha = 0.6, shape = 21, color = "black")  
  geom_point(data = df_Hamble, alpha = 0.6, shape = 21, color = "black")  
  scale_size(range = c(10, 40), name = bquote(PM[2.5]))  
  geom_text_repel(aes(label = Name), size =3, fontface = 1, family = "Calibri")  
  geom_text_repel(data = df_Hamble, 
    aes(label = Name), size = 5,fontface = 1, color = "red", family = "Calibri")  
  scale_y_continuous(limits = c(5, 13))  
  scale_x_continuous(limits = c(5, 20))  
  scale_fill_viridis(discrete = TRUE, guide = FALSE, option = "D")  
  theme_ipsum()  
  xlab(expression(Concentration ~ PM[10]))  
  ylab(expression(Concentration ~ PM[2.5]))  
  theme(legend.position = "none")  
  theme(axis.text.x = element_text(size = 12, angle = 0, vjust = 0.5, hjust = 1))  
  theme(axis.text.y = element_text(size = 12, angle = 0, vjust = 0.5, hjust = 1))  
  geom_hline(aes(yintercept = 7.9), linetype = "dashed", color = "red", )  
  geom_vline(aes(xintercept = 12.9), colour = "red", linetype = "dashed")

  • Related