How to change labels of x axis for factor data

Hi all my data looks like this

myDf = data.frame(SEX = as.factor(c(1,0,0,1,1)))

P <- ggplot(data = myDf, aes(x = SEX, fill = SEX)) +
  geom_bar(stat = "count",show.legend = TRUE) + 
  stat_count(geom = "text", colour = "blue", size = 7,
             aes(label = ..count..),position=position_stack(vjust=0.5)) 
P

when i do this i have got the x axis scale labels as 0 and 1. I want to change 0 to male and 1 to female on x axis, with out changing it in dataframe. Can someone help me with this ?

Thanks

You need something like this?

myDf <- data.frame(SEX = as.factor(c(1, 0, 0, 1, 1)))

P <- ggplot(data = myDf, aes(x = SEX, fill = SEX)) +
  geom_bar(stat = "count", show.legend = TRUE) +
  stat_count(geom = "text", colour = "blue", size = 7,
             aes(label = ..count..), position = position_stack(vjust = 0.5))

# Change the x-axis labels
P + scale_x_discrete(labels = c("Male", "Female"))

Thank you very much for your response,
this is not exactly what i wanted

I want code to assisgn "0" to " MALE" and "1" to "FEMALE".

My problem with this code is scale_x_discrete(labels = c("Male", "Female"))

I can assign ("Female",'Male") as well , which will give 0 to female and will be interpreted wrongly.

Maybe is well create a new column with a this classification, next make the plot:

myDf <- myDf %>% 
  mutate(GENDER = ifelse(SEX == 0, "Male", "Female"))

# SEX GENDER
# 1   1 Female
# 2   0   Male
# 3   0   Male
# 4   1 Female
# 5   1 Female

This is one possible way, i am looking for the ways to convert in plot itself, with out distrubing the dataframe.

Thank you very much for your timely responses

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.