0

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:

enter image description here

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.

user20650
  • 24,654
  • 5
  • 56
  • 91
  • 1
    For the counts, did you try the solution given at the link [in the comments](https://stackoverflow.com/questions/45222720/r-lattice-how-to-remove-white-space-between-bars-and-x-axis#comment77414934_45222720) at your previous question. If you did and it didnt work, can you show this in your question please, and indicate why it did not work. – user20650 Jul 22 '17 at 13:51

1 Answers1

1

Your columns Nature_of_sexual_abuse and Familiarity_with_the_perpetrator are factors which are categorical variables. Data of type factor always have levels in R which are the unique character values existing in your data. These level values are used as the labels when you plot the data.

For example, in your data the levels of column Nature_of_sexual_abuse have the unique values "Made a sex video", "Forced sex behaviors", "Looked at private parts", "Touched private parts", "Shown pornography" and "Talked in a sexual way".

If you want to reorder your data in the plot you need to reorder the level values of your categorical data. Insert this reordering before creating the plot and it should work:

library(lattice)


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)




nature$Nature_of_sexual_abuse <- factor(nature$Nature_of_sexual_abuse, 
                                    levels=c("Made a sex video", 
                                             "Forced sex behaviors",
                                             "Looked at private parts",
                                            "Touched private parts",
                                             "Shown pornography",
                                             "Talked in a sexual way"))
nature$Familiarity_with_the_perpetrator <- 
factor(nature$Familiarity_with_the_perpetrator, levels=c("Very familiar", 
"Not very familiar","Not familiar at all"))



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))
       )

Depending on the order of your levels, the order of your data in the plot will change.

I hope this will help you on the issue of ordering your data.

Elena
  • 121
  • 8
  • thank you. I tried with the code you provided before running the `barchart` function. But, it didn't help. The chart appears completely blank. – White Clubs Jul 22 '17 at 14:09
  • When I run the code it works perfectly. 1) Be sure that you have load your package correctly 2) Create dataframe _nature_ with your data 3) Reorder the factor levels with the code I provided above 4) Create variable colors 5) Create bar chart as you already did – Elena Jul 22 '17 at 14:43
  • @WhiteClubs ; before running Elena's reordering (which works) have a look at `nature$Familiarity_with_the_perpetrator`, you may have white space, hence the levels vector wont match the variable. If so remove the white space (thats why i added the strip.white argumetn in read.table) – user20650 Jul 22 '17 at 14:58
  • I only know it with ggplot2. Then it would be: `ggplot(nature, aes(x=Nature_of_sexual_abuse, y=Count, fill=Familiarity_with_the_perpetrator), fill=colors) + geom_bar(stat="identity", position=position_dodge(), colour="black") + scale_fill_manual(values=c("lightsalmon3", "lightgoldenrod2", "cadetblue4"))+ geom_text(aes(label=Count), position = position_dodge(width=1), vjust=-1)+ theme(legend.position="top", legend.title=element_text(color="white"))+ labs(x = "Nature of sexual abuse", y = "Number of students")` instead of `barchart( data = nature, ....)` – Elena Jul 25 '17 at 13:56