I am working on a child sexual abuse research. For this, I want to prepare a clustered bar chart using lattice
. I used the following code.
My data:
nature <- read.table(text = "Nature_of_sexual_abuse, Familiarity_with_the_perpetrator, Count
Talked in a sexual way, Not familiar at all, 21
Talked in a sexual way, Not very familiar, 22
Talked in a sexual way, Very familiar, 22
Shown pornography, Not familiar at all, 6
Shown pornography, Not very familiar, 17
Shown pornography, Very familiar, 16
Looked at private parts, Not familiar at all, 16
Looked at private parts, Not very familiar, 14
Looked at private parts, Very familiar, 10
Touched private parts, Not familiar at all, 6
Touched private parts, Not very familiar, 15
Touched private parts, Very familiar, 11
Made a sex video, Not familiar at all, 1
Made a sex video, Not very familiar, 6
Made a sex video, Very familiar, 7
Forced sex behaviors, Not familiar at all, 5
Forced sex behaviors, Not very familiar, 17
Forced sex behaviors, Very familiar, 10",
header = TRUE,
sep = ",", strip.white = TRUE)
My plot:
library(lattice)
colors = c("lightsalmon3", "lightgoldenrod2", "cadetblue4")
barchart(
data = nature,
origin = 0,
Count ~ Nature_of_sexual_abuse,
groups = Familiarity_with_the_perpetrator,
xlab = list (
label = "Nature of sexual abuse",
font = 2,
cex = 1),
ylab= list (
label = "Number of students",
font = 2,
cex = 1),
ylim=c(0,25),
labels = TRUE,
auto.key = list(space="top", columns= 3),
par.settings = list(superpose.polygon = list(col = colors)))
The chart currently looks like this:
However, I want it to sort in the following order of Nature_of_sexual_abuse
variable: "Made a sex video", "Forced sex behaviors", "Looked at private parts", "Touched private parts", "Shown pornography" and "Talked in a sexual way".
Within each cluster, the graph has to be sorted in the order of Familiarity_with_the_perpetrator
variable: "Very familiar", "Not very familiar" and "Not familiar at all".
I tried feeding the data in order I want it to be displayed. But, it appears that lattice
automatically sorts groups on alphabetical order.
I also want the values of each bar to appear at the top of bars. Can someone please help me on grouping it on the order I want?
As you can see, I am a newbie to R. So, any help will be appreciated a lot.