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!


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