I was trying to do a scatter plot of a table, while only labeling 2 specific points. The table looks something like this
"Country","Continent","GDP","HCI"
Afghanistan,Asia,1991,0.40
Albania,Europe,19566,0.63
Algeria,Africa,13682,0.53
Angola,Africa,7077,0.36
Antigua and Barbuda,Americas,25449,0.60
Argentina,Americas,26506,0.60
...
With the following code:
ctry_inf <- read.csv("Paises_PIB_ICH.csv")
ctry_inf_selec <- ctry_inf[ctry_inf$Continent == "Asia" | ctry_inf$Continent == "Africa", ]
ggplot(ctry_inf_selec, aes(x=GDP, y=HCI)) + geom_point() +
scale_x_continuous(trans='log10') +
geom_text(aes(label =
ifelse(ctry_inf$Country == "Namibia" | ctry_inf$Country == "United Arab Emirates",
as.character(Country), "")))
I get this error:
> ggplot(ctry_inf_selec, aes(x=GDP, y=HCI)) + geom_point() +
+ scale_x_continuous(trans='log10') +
+ geom_text(aes(label = ifelse(ctry_inf$Continent == "Asia", as.character(Country), "")))
Error in `geom_text()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 2nd layer.
Caused by error in `check_aesthetics()`:
! Aesthetics must be either length 1 or the same as the data (91)
✖ Fix the following mappings: `label`
Run `rlang::last_trace()` to see where the error occurred.
error encountered
How do I make it work? I cannot use any other extra libraries other than ggplot2.
I tried several ways to do labeling and all of them result in errors.
New contributor
Luís Frederico Santos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.