3
votes

Good afternoon,

This is my 1st question here and every attempt is being made to be thorough.

I am working with a large data set (casualtiesdf) in R and I am trying to produce a Boxplot, using ggplot2, with the variable Age_of Casualty by the Casualty_Severity variable. The problem is that R thinks that Casualty_Severity variable is integer. Casualty_Severity in the data is listed by numbers 1, 2,3.

Below you can see that I've tried to rename the integer into the named factor to which is corresponds and then converted the integer into a factor.

casualtiesdf$Casualty_Severity[casualtiesdf$Casualty_Severity == 1] "Fatal" 
casualtiesdf$Casualty_Severity[casualtiesdf$Casualty_Severity == 2]"Serious"
casualtiesdf$Casualty_Severity[casualtiesdf$Casualty_Severity == 3] "Slight"
casualtiesdf$Casualty_Severity <- as.factor(casualtiesdf$Casualty_Severity)

When I try doing the Boxplot, however...

> ggplot(data = casualtiesdf, aes(x = Age_of_Casualty,
+ y = casualtiesdf$Casualty_Severity)) +
+ geom_boxplot()

I get: "Warning message:position_dodge requires non-overlapping x intervals" I typed this message into Google and stackflow seems to advise putting the categorical variable in the x axes (yes I'm still very confused with my x's and y's...) so I tried:

ggplot(data = casualtiesdf, aes(x = Casualtiesdf$Casualty_Severity, 
                            y = Age_of_Casualty + 
                            geom_boxplot()

and get error message "Error: object 'Age_of_Casualty' not found"

I then went for thinking that maybe I have to put the as.factor in the plot code:

ggplot(data = casualtiesdf, aes(x = casualtiesdf$Casualty_Severity  
as.factor(casualtiesdf$Casualty_Severity))) y = Age_of_Casualty) +
geom_boxplot()

and get error message "unexpected symbol in: geom_boxplot() ggplot"

Any help with this is greatly appreciated!

2
Welcome to R community. Here's some tips: when asking questions, probably the most important thing is to provide reproducible data to refer to. Having no data generally makes the following code fairly useless. - statespace
And another thing to keep in mind: something like df <- data.frame(a = sample(1:3, 20, replace = T), b = runif(20, 1, 10)) would perfectly represent your problem. Actual data set or size for that matter don't really mean anything in the context of actual problem. - statespace

2 Answers

2
votes

Is Age_of_Casualty also part of the dataframe as well? if not, you might consider to merge or separate assignment to create a Age_of_Casualty column in the df as well.

I created a dummy dataframe, with two variables

casualtiesdf <- data.frame(Casualty_Severity=c(1,2,1,1,2,3,1,3), Age_of_Casualty = c(31,32,32,33,33,33,35,35)) I then created another varialbe, to store the casualty_severity as factor

casualtiesdf$Casualty_Severity_factor <- factor(x = casualtiesdf$Casualty_Severity, 
                                            levels = c(1,2,3),
                                            labels = c("Fatal","Serious","Slight"))

With that, I can then do the box plot, with the casualty_severity as X-axis

library("ggplot2")
ggplot(data = casualtiesdf, 
       aes(x= Casualty_Severity_factor, y =  Age_of_Casualty)) +
  geom_boxplot()

This should give you some plot like thisenter image description here

1
votes

So it's expected to me that in your third example R is reporting that you have a syntax error: unexpected symbol in: geom_boxplot() means "I have no idea what to do with that ...))) y = business.

Your first example R mistakenly assigns Age_of_Casualty as the X - this is really the variable whose distribution you want to analyze (it should be the Y variable).

So you're right, you need to establish Casualty_Severity as a Factor and make sure to ascribe the two variables to X and Y correctly. Something like this:

# Creating dummy data 
AC.rand <- sample(15:90, 500, replace = T)
CS.rand <- sample(1:3, 500, replace = T)

# Combine them into a dataframe, define the "Severity" variable as a Factor
casualtiesdf <- data.frame(Casualty_Severity = factor(CS.rand), Age_of_Casualty = AC.rand)

# Define the Levels for the "Severity" variable - not necessary
levels(casualtiesdf$Casualty_Severity)=c("Fatal", "Serious", "Slight")

g <- ggplot(data = casualtiesdf, aes(x = Casualty_Severity, y = Age_of_Casualty))
g <- g + geom_boxplot()

When I mocked up 500 rows of data I get something like: enter image description here

I'm an SO noob, too, so let's learn together! :)