1

I am new to R and am using lattice to create a clustered bar graph. I wrote the following code.

nature <- read.table(text = "Nature_of_recreation, Familiarity_with_the_partner, Count
Danced, Not familiar at all, 21
Danced, Not very familiar, 22
Danced, Very familiar, 22
Sketched, Not familiar at all, 6
Sketched, Not very familiar, 17
Sketched, Very familiar, 16
Dined, Not familiar at all, 16
Dined, Not very familiar, 14
Dined, Very familiar, 10
Swimmed, Not familiar at all, 6
Swimmed, Not very familiar, 15
Swimmed, Very familiar, 11
Played, Not familiar at all, 1
Played, Not very familiar, 6
Played, Very familiar, 7
Dreamed, Not familiar at all, 5
Dreamed, Not very familiar, 17
Dreamed, Very familiar, 10",
                     header = TRUE, sep = ",")
colors = c("lightsalmon3", "lightgoldenrod2", "cadetblue4")
lattice::barchart(data = nature, origin = 0, Count ~ Nature_of_recreation,
                  groups = Familiarity_with_the_partner, 
                  xlab = list(label = "Nature of recreation", font = 2, cex = 1),
                  ylab= list (label = "Number of students", font = 2, cex = 1),
                  labels = TRUE, auto.key = list(space="top", columns= 3),
                  par.settings = list(superpose.polygon = list(col = colors)))

The output looks all good, except:

  1. It has a white space in between bars and the X-axis.
  2. It does not have values displayed on top of each bar.

barchart

Can anyone please help me fix these issues?

user20650
  • 24,654
  • 5
  • 56
  • 91
  • does this help? https://stackoverflow.com/questions/22945651/how-to-remove-space-between-axis-area-plot-in-ggplot2 – CPak Jul 20 '17 at 19:41
  • https://stackoverflow.com/questions/10475790/obtaining-midpoint-values-of-grouped-bars-in-lattice-barchart-function, and for the space use `ylim(0, 25)` – user20650 Jul 20 '17 at 20:12
  • Thank you @user20650. I am confused on where to insert `ylim(0,25)` within the code I pasted above? Can you please help me? – White Clubs Jul 21 '17 at 02:08
  • 1
    `ylim` is an argument of `barchart`, yo can enter it anywhere, so for example, try `origin = 0, ylim=c(0,25), ...` – user20650 Jul 21 '17 at 06:30
  • 1
    Thank you @user20650. It solved the problem. – White Clubs Jul 21 '17 at 09:41
  • @user20650 Thank you for your comments. I was able to remove the white space. But, I am still having problem with adding values at top of the bars and sorting bars in order of magnitude. I have posted a different question on this here: https://stackoverflow.com/questions/45251117/r-lattice-clustered-barchart-sort-bars-and-display-values-at-top-of-bars. Can you please help me figure out the solution? – White Clubs Jul 22 '17 at 06:38

0 Answers0