hotcoder24 hotcoder24 - 2 months ago 10
R Question

Scatter plot Text labels in R

I am plotting categorical variable vs continuous variable, AGE vs Smoking habits
Below is the code.

stripchart(Age~Smoke, data = survey_clean_data , pch=16 , col = "blue", method = "jitter" ,main = "AGE VS SMOKE",na.rm = T)


I want to add labels to it like below image,
enter image description here

I tried several options.. but it is getting written on top of of other.

means = c(paste("mean_Age =",roumean(survey_clean_data[Smoke == "Heavy","Age"],na.rm =T)),
paste("mean_Age =",mean(survey_clean_data[Smoke == "Never","Age"],na.rm =T)),
paste("mean_Age =",mean(survey_clean_data[Smoke == "Regul","Age"],na.rm =T)),
paste("mean_Age =",mean(survey_clean_data[Smoke == "Occas","Age"],na.rm =T)))


text(50,survey_clean_data$Smoke,labels = means)


DATA: library(MASS) attach(survey)

Answer

There are a few problems with your code. The main thing is that you are sending text() four labels (the contents of means), but a number of y-coordinates equal to the number of data points, since you are sending it survey_clean_data. R tries to equalize these uneven vectors, resulting in the over plotting.

Instead, you might do (data are artificial since you didn't provide any):

stripchart(Age~Smoke, data = survey_clean_data , pch=16 , col = "blue", method = "jitter" ,main = "AGE VS SMOKE",na.rm = T)

means <- aggregate(Age~Smoke, data = survey_clean_data, FUN = mean) # mean of each category
means$y <- 1:4 # add y-coordinates for each category

with(means, text(50, Smoke, labels = sprintf('Mean Age = %0.1f', Age))) # plot text labels on top of stripchart

Result:

enter image description here