0

I made facet graphs using facet_grid. It looks just like I want, except that the y-axis looks jammed in the graph, the data is in percentage and it goes from (3%-95%). is there a way to make it look better?

enter image description here

plot <- ggplot(data=mydata, mapping=aes(x=year, y=value)) +
  geom_bar(stat="identity", aes(color=coralType))

I tried using :

plot + facet_grid(coralType ~ location, scales="free")

and

plot + facet_grid(coralType ~ location, scales="free_x") 
plot + facet_grid(coralType ~ location, scales="free_y")

I also tried ylim=c(3, 100) ylim=range(3:100)

none of that worked.

Here is my data:

structure(list(location = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("site01", "site02", 
"site03", "site04", "site05", "site06", "site07", "site08"), class = "factor"), 
    coralType = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L), .Label = c("blue corals", "hard corals", 
    "sea fans", "sea pens", "soft corals"), class = "factor"), 
    longitude = c(143.515, 143.515, 143.515, 143.515, 143.515, 
    143.515, 143.515, 143.515, 143.515, 143.515, 143.515, 143.515, 
    143.515, 143.515, 143.515), latitude = c(-11.843, -11.843, 
    -11.843, -11.843, -11.843, -11.843, -11.843, -11.843, -11.843, 
    -11.843, -11.843, -11.843, -11.843, -11.843, -11.843), year = c(2010L, 
    2011L, 2012L, 2013L, 2014L, 2015L, 2016L, 2017L, 2011L, 2012L, 
    2013L, 2014L, 2015L, 2016L, 2017L), value = c(30, 30, 41, 
    43, 50, 54, 57, 58, 10, 11, 30, 31, 31, 32, 34)), row.names = c(NA, 
15L), class = "data.frame")
jay.sf
  • 60,139
  • 8
  • 53
  • 110
Myaccount
  • 79
  • 7
  • 1
    I'm guessing that your y axis is a character or factor, when it should be numeric. You need to post full code and data to allow people to diagnose the problem and answer appropriately. Have a look at https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – dww Mar 31 '19 at 03:10
  • You are right. I checked for the "value" type and it is character. Should converting it to numeric solve my issue? – Myaccount Mar 31 '19 at 03:24
  • It depends on your analysis and data. It's not a rule of thumb although in most cases people convert to numeric. Could you provide sample data with `dput(head(mydataframe,15))` ?Also include all the code used. – NelsonGon Mar 31 '19 at 03:46
  • Also when using `ylim` you should use it inside `coord_cartesian`. – NelsonGon Mar 31 '19 at 03:47
  • 1
    The difference is that if your data is `numeric` you can use the `scale_y_continuous` functions to show a logical selection of breaks that leave the scale readable. If they're `character` or `factor`, then they're plotted as a categorical variable, where each possible value needs its own label – divibisan Apr 01 '19 at 16:27

1 Answers1

2

With the data supplied by the OP, the plot produced by

ggplot(mydata) + 
  aes(x = year, y = value, fill = coralType) +
  geom_col() + 
  facet_grid(coralType ~ location)

enter image description here

looks fine because value is numeric.

Note that the fill aesthetic is used instead of the colour aesthetic. Furthermore, geom_col() is used as shortcut for geom_bar(stat = "identity").


I can reproduce the issue when plotting value as character (which is turned into a factor by ggplot2):

max_value <- 60
ggplot(mydata) + 
  aes(x = year, y = sprintf("%.2f %%", 100 * value / max_value),
      fill = coralType) +
  geom_col() + 
  facet_grid(coralType ~ location)

enter image description here

This does not appear as cluttered as OP's screenshot due to the limited number of data points.


If the OP wants to show percentages on the y-axis instead of absolute values, scale_y_continuous(labels = scales::percent) can be used with numeric values:

max_value <- 60
ggplot(mydata) + 
  aes(x = year, y = value / max_value, fill = coralType) +
  geom_col() + 
  facet_grid(coralType ~ location) + 
  scale_y_continuous(labels = scales::percent)

enter image description here

Data

mydata <-
structure(list(location = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("site01", "site02", 
"site03", "site04", "site05", "site06", "site07", "site08"), class = "factor"), 
    coralType = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L), .Label = c("blue corals", "hard corals", 
    "sea fans", "sea pens", "soft corals"), class = "factor"), 
    longitude = c(143.515, 143.515, 143.515, 143.515, 143.515, 
    143.515, 143.515, 143.515, 143.515, 143.515, 143.515, 143.515, 
    143.515, 143.515, 143.515), latitude = c(-11.843, -11.843, 
    -11.843, -11.843, -11.843, -11.843, -11.843, -11.843, -11.843, 
    -11.843, -11.843, -11.843, -11.843, -11.843, -11.843), year = c(2010L, 
    2011L, 2012L, 2013L, 2014L, 2015L, 2016L, 2017L, 2011L, 2012L, 
    2013L, 2014L, 2015L, 2016L, 2017L), value = c(30, 30, 41, 
    43, 50, 54, 57, 58, 10, 11, 30, 31, 31, 32, 34)), row.names = c(NA, 
15L), class = "data.frame")
Community
  • 1
  • 1
Uwe
  • 41,420
  • 11
  • 90
  • 134
  • Thank you Uwe. But the he data will be jammed again on the y axis when I add the rest of the variables (I have 8 sites and 5 types). Any advice to avoid it? I tried running your code it gives me the same as my previous result, I think because you ran your code only on a sample of the data. – Myaccount Mar 31 '19 at 07:39
  • What does `str(mydata$value)` return for your full dataset?. – Uwe Mar 31 '19 at 07:45