0

I'm trying to plot a simple boxplot where I get 15 boxplots for respondents age of 15 "Cadernos" (15 surveys, surveys A, B, C .. to O). But that's not working as expected. I have already tried to switch places to the "Cadernos" (surveys) and "Idade" (age) variables. Any idea? What I expected was 15 boxplots in the vertical axis.

The code I'm using is the follow:

library(ggplot2)

select_base %>% 
  ggplot(aes(Idade,Caderno)) +
  geom_boxplot()  

the plot I get is the following:

enter image description here

5
  • I don't think that code generated the plot: the x and y variables are reversed in the code versus the plot. Commented Dec 7, 2021 at 2:17
  • 1
    Also it looks like Idade is a variable of type character or factor, instead of numeric. What does str(select_base) show you? Commented Dec 7, 2021 at 2:22
  • @neilfws You're right, the code that generrates that plot has those variables switched, I slipped-up. About the type of varible, str(select_base) returns: 'data.frame': 2876 obs. of 2 variables: $ Caderno: chr "N" "N" "M" "M" ... $ Idade : chr "15" "15" "15" "15" ... So, yes, they're characters, would that be affecting the code? Tks in advance Commented Dec 7, 2021 at 3:05
  • Yes, you need the y-variable to be of type numeric. You may be able to convert the column using as.numeric(), but it's better to figure out why R read it in as a character in the first place - for example, does the data file contain something other than numbers in that column. Commented Dec 7, 2021 at 3:19
  • @neilfws thank you! Commented Dec 7, 2021 at 17:21

1 Answer 1

1

I'm not sure that you did not provide your data, but you may try

select_base %>% 
  ggplot(aes(x = Caderno, y = Idade, group = Caderno)) +
  geom_boxplot()  

For example using data dummy it will be like plot below

dummy <- data.frame(
  x = rnorm(50),
  y = rep(c("a","b","c","d","e"),10)
)
dummy %>%
  ggplot(aes(x = y,y = x,  group = y)) +
  geom_boxplot()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.