Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have a single xlabel and ylabel for multiple plots on the same page

Tags:

r

ggplot2

I am using the code given below to plot multiple graphs on the same page using ggplot. Instead of each graph having its own xlabel, ylabel, title and legend description, I want just one such for each page. I tried to find a solution to that, but to no avail. Can someone please let me know, or point me to a relevant thread? Thanks.

enter image description here

plots <- dlply(dacc, .(SIC), function(x){
      ggplot(x, aes(x = PER)) + 
      geom_line(aes(y = MEAN, group = NLEAD, color = NLEADMEAN)) +
      geom_line(aes(y = MED, group = NLEAD, color = NLEADMED)) +
      xlab("(Total Asset Percentile Cutoff To Define Big Firms)") +
      ylab("Mean Absolute Discretioary Accrual For Different Groups") +
      ggtitle(paste("Mean ADA Across Groups For SIC = ", unique(x$SIC))) +
      ylim(0, ymax/2)
    })

  ml <- do.call(marrangeGrob, c(plots, list(nrow = 2, ncol = 2)));
  ggsave("my_plots.pdf", ml, height = 7, width = 13, units = "in");

Edit:

ggplot(dacc, aes(x = PER)) + 
  geom_line(aes(y = MEAN, group = NLEAD, color = NLEADMEAN)) +
  geom_line(aes(y = MED, group = NLEAD, color = NLEADMED)) +
  xlab("(Total Asset Percentile Cutoff To Define Big Firms)") +
  ylab("Mean Absolute Discretioary Accrual For Different Groups") +
  ggtitle(paste("Mean ADA Across Groups For SIC = ", unique(x$SIC))) +
  ylim(0, ymax/2) +
  facet_wrap(~SIC,nrow=2)

This definitely seems better, but I am facing multiple problems with this one. (1) On the terminal I am able to see the graphs, which presents only xlab and ylab. But how do I save the graph? (2) I have around 50 SIC, so 50 grpahs, so I assume that when I save, let say with nrow = 10, I should be able to see xlab, and ylab on each page.

like image 911
Sumit Avatar asked Jan 25 '26 03:01

Sumit


1 Answers

As commented , you should use faceting feature here. For example:

facet_wrap(~SIC,nrow=2)

You code becomes something like this :

  ggplot(dacc, aes(x = PER)) + 
  geom_line(aes(y = MEAN, group = NLEAD, color = NLEADMEAN)) +
  geom_line(aes(y = MED, group = NLEAD, color = NLEADMED)) +
  xlab("(Total Asset Percentile Cutoff To Define Big Firms)") +
  ylab("Mean Absolute Discretioary Accrual For Different Groups") +
  ggtitle(paste("Mean ADA Across Groups For SIC = ", unique(x$SIC))) +
  ylim(0, ymax/2) +
  facet_wrap(~SIC,nrow=2)
like image 156
agstudy Avatar answered Jan 27 '26 21:01

agstudy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!