The boxplot R gives me does not have the 3 groups on the X-axis labeled as "Urban", "Suburban", and "Rural" and I am not familiar enough with boxplots in R to do that. Can anyone help? All code is attached below for anyone to copy n paste. For the "Rurality" vector the number 1 indicates its and Urban county, 2 indicates Suburban, and 3 indicates Rural.
Additionally, is there a way to change the colors of the boxes in the boxplot to be different from each other?
Rurality<- c(1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
2.0,
2.0,
2.0,
2.0,
2.0,
2.0,
2.0,
2.0,
2.0,
2.0,
2.0,
2.0,
2.0,
2.0,
2.0,
2.0,
2.0,
2.0,
2.0,
2.0,
2.0,
2.0,
2.0,
2.0,
2.0,
2.0,
2.0,
2.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0,
3.0)
CR<- c(895.3,
692.5,
834.6,
571.0,
618.8,
727.9,
286.8,
391.5,
440.9,
442.5,
343.3,
545.3,
205.9,
207.5,
259.7,
495.2,
689.8,
502.2,
434.9,
325.6,
185.3,
399.4,
218.7,
274.5,
461.4,
367.5,
337.0,
308.5,
334.2,
309.2,
236.6,
278.3,
349.0,
425.3,
225.7,
369.1,
297.7,
277.7,
425.7,
344.3,
287.5,
190.0,
182.6,
302.8,
262.8,
290.0,
339.5,
204.0,
333.4,
266.9,
276.4,
210.4,
299.7,
247.1,
237.4,
217.0,
247.4,
234.2,
305.2,
170.0,
339.5,
237.7,
215.0,
166.4,
283.4,
173.0,
185.1,
217.2,
237.0,
178.6,
280.3,
272.4,
133.8,
245.6,
367.3,
246.3,
338.5,
187.7,
167.2,
296.4,
262.4,
259.3,
153.6,
189.7,
241.9,
88.7,
153.8,
175.8)
data = data.frame(Rurality, CR)
data$Factor_Rurality <- as.factor(data$Rurality)
ggplot(data = data, aes(y = CR, x = Factor_Rurality))+
geom_boxplot(width = 0.65)+
stat_boxplot(geom = "errorbar", width = 0.2)+
geom_point()+
labs(y = "Chlamydia Rate",
title = "Chlamydia Rate in Urban, Suburban, and Rural Ohio Counties",
x= "County Types")+
theme(plot.title = element_text(hjust = 0.5))+
theme(plot.title = element_text(face = "bold"))
You can first change
as.factor()tofactor()and then add thelevelsandlabelsattributes:To add colors, you just need to specify the aesthetics: