39

Below is a plot that I want to include in a paper. The problem is the width of my plot which is to small (that make x-axix not readable at all)

Here is the ggplot2 code myCode.r :

require("ggplot2")

all <- read.csv(file="benchmark/bench.query.csv", head=TRUE, sep=";")

w <- subset(all, query %in% c("sort.q1", "sort.q2", "sort.q3", "sort.q4", "sort.q5"))

w$rtime <- as.numeric(sub(",", ".", w$rtime, fixed=TRUE))

p <- ggplot(data=w, aes(x=query, y=rtime, colour=triplestore, shape=triplestore))
p <- p + scale_shape_manual(values = 0:length(unique(w$triplestore)))
p <- p + geom_point(size=4)
p <- p + geom_line(size=1,aes(group=triplestore))
p <- p + labs(x = "Requêtes", y = "Temps d'exécution (log10(ms))")
p <- p + scale_fill_continuous(guide = guide_legend(title = NULL))
p <- p + facet_grid(trace~type)
p <- p + theme_bw()

ggsave(file="bench_query_sort.pdf")

print (p)

I've look around to see how to enlarge the plot, but I found nothing.

Any idea about what to add/delete/modify in my code ?

plot

Henrik
  • 65,555
  • 14
  • 143
  • 159
Fopa Léon Constantin
  • 11,863
  • 8
  • 48
  • 82
  • 3
    Jaap's answer solves the enlarging question. Additionally, you might want to rotate the x tickmarks: `p = p + theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))` – rozsasarpi Apr 12 '15 at 11:12
  • See also: [Set the size of ggsave exactly](https://stackoverflow.com/questions/44711236/set-the-size-of-ggsave-exactly) – Henrik Jun 21 '20 at 13:56

3 Answers3

43

Inside a Jupyter notebook I found the following helpful:

# Make plots wider 
options(repr.plot.width=15, repr.plot.height=8)
user96622
  • 559
  • 4
  • 7
32

Probably the easiest way to do this, is by using the graphics devices (png, jpeg, bmp, tiff). You can set the exact width and height of an image as follows:

png(filename="bench_query_sort.png", width=600, height=600)

ggplot(data=w, aes(x=query, y=rtime, colour=triplestore, shape=triplestore)) + 
  scale_shape_manual(values = 0:length(unique(w$triplestore))) + 
  geom_point(size=4) + 
  geom_line(size=1,aes(group=triplestore)) + 
  labs(x = "Requêtes", y = "Temps d'exécution (log10(ms))") + 
  scale_fill_continuous(guide = guide_legend(title = NULL)) + 
  facet_grid(trace~type) + 
  theme_bw()    

dev.off()

The width and height are in pixels. This is especailly useful when preparing images for publishing on the internet. For more info, see the help-page with ?png.

Alternatively, you can also use ggsave to get the exact dimensions you want. You can set the dimensions with:

ggsave(file="bench_query_sort.pdf", width=4, height=4, dpi=300)

The width and height are in inches, with dpi you can set the quality of the image.

Jaap
  • 81,064
  • 34
  • 182
  • 193
  • 5
    Very nice answer. I do something very similar with `quartz(height=11, width=8.5)` to get a new plot window, followed by the `ggplot()` stuff, followed by `quartz.save('fname.pdf')`. The end result is the same and I never have to remember those pesky `dev.off()` commands. – Curt F. Apr 13 '15 at 02:18
18

If you are using RMD(R Markdown) this would be the easiest way to define width and height.

```{r fig.align="center", echo = FALSE,fig.width = 14}
<write the code for your plot here>
```

Note: options() not worked for me so I used this method

INDRAJITH EKANAYAKE
  • 3,894
  • 11
  • 41
  • 63