0

Im trying to make a chart which shows the values of each point next to the point itself.

My current code looks like this:

ggplot()+ 
    geom_point(data = datacombined, aes(x = datacombined$`Poulation group`, y = datacombined$`Percent of population in cicilian labor force`, color = datacombined$Location),size=4) +
    scale_color_manual(values = c("US" = "darkblue", "OH" = "red"))+
  ylim(0,100)+
  theme_bw()+
  ylab("Percentage of population in civilian labour force") + xlab("Population group")

Which gives me this plot: plot currently

I would like to add values next to the points in the plot so for example the red point on the left should say 67.7. I would also like to change the title on the side which shows the location of each statistic.

I tried to make the plot as described, but do not know how to name each point with values.

stefan
  • 90,330
  • 6
  • 25
  • 51
tonjekm
  • 21
  • 1
  • You could add your labels via a `geom_text`. For more help we need [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data to run your code. – stefan Jan 12 '23 at 22:54

1 Answers1

0

You could use geom_text with nudge_x adjusted as needed. Note that you only need to put unquoted column names inside aes, not the dataframe$column syntax.

library(ggplot2)

ggplot(datacombined, aes(x = `Poulation group`, 
                         y = `Percent of population in cicilian labor force`)) + 
  geom_point(aes(color = Location), size = 4) +
  geom_text(aes(label = after_stat(y)), nudge_x = 0.1) +
  scale_color_manual(values = c(US = "darkblue", OH = "red")) +
  ylim(0, 100) +
  theme_bw()+
  ylab("Percentage of population in civilian labour force") + 
  xlab("Population group")


Data used (inferred from plot and code, typos preserved)

datacombined <- data.frame(
  `Poulation group` = rep(c("Men", "Men and women, 18 to 19 years old", 
                             "Women"), each = 2),
  `Percent of population in cicilian labor force` = c(65, 67, 47, 54, 35, 55),
  Location = rep(c("OH", "US"), 3), check.names = FALSE)

Created on 2023-01-12 with reprex v2.0.2

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87