Labels of points are not showing on the plot

I am running a code using ggplot2 package in tidyverse. Everything worked fine except the label argument because it did not add the abbreviations to every single point in the plot

library(tidyverse)
library(dslabs)
data(murders)

---- Conflicts ------------------------------------------ tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag() masks stats::lag() /

I tried to increase the size of the plot because I read that sometimes such issues occur when RStudio does not have enough space to apply the features.

murders %>%
ggplot(aes(population, total, label = abb, color = region)) +
geom_point()

System Information:

  • RStudio Edition: (Desktop or Server)
  • RStudio Version: 1.2.5033
  • OS Version: Windows 8
  • R Version: 3.6.1
  • sessionInfo():
    library(tidyverse)
    -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
    v ggplot2 3.2.1 v purrr 0.3.3
    v tibble 2.1.3 v dplyr 0.8.4
    v tidyr 1.0.2 v stringr 1.4.0
    v readr 1.3.1 v forcats 0.4.0
    -- Conflicts ------------------------------------------ tidyverse_conflicts() --
    x dplyr::filter() masks stats::filter()
    x dplyr::lag() masks stats::lag()

library(dslabs)

data(murders)

murders %>%

  • ggplot(aes(population, total, label = abb, color = region)) +
  • geom_point()

Referred here from support.rstudio.com

The problem is, that geom_point() does not include the label.
You need to add geom_label() or geom_text.

murders %>%
  ggplot(aes(population, total, label = abb, color = region)) +
  geom_point() + 
  geom_text(check_overlap = TRUE)

You also might find the ggrepel-package useful for this:
https://cran.r-project.org/web/packages/ggrepel/vignettes/ggrepel.html

3 Likes

Hello Matthias. Thank you very much for your reply.
I am sorry I did not get back to you before but I was discovering RStudio and GitHub.

Matthias can you help me know why tidyverse is not functioning? I ran the code above (after a fresh installation of R and RStudio), but I got an error message: could not find function "%>%"

I installed dplyr alone and ran the code, and I got thiserror message: could not find function "ggplot"

Hi FarahD,
When you load the tidyverse using library(tidyverse) several packages are loaded at once. You should see a message similar to this

-- Attaching packages --------------------------------------- tidyverse 1.3.0 --
v ggplot2 3.2.1     v purrr   0.3.3
v tibble  2.1.3     v dplyr   0.8.3
v tidyr   1.0.0     v stringr 1.4.0
v readr   1.3.1     v forcats 0.4.0

This should make all the functions available that you need to run the code given earlier.
BTW - if your new installation of R involved a version change, you will need to re-install the packages: use the RStudio menu or enter the following in the terminal:
install.packages("tidyverse")

HTH