Chris
Chris

Reputation: 2071

Decrease the size of a graph in ggplot - R

I have two questions for this graph I did:

enter image description here

The first, is how do I decrease the graph size?, I have tried with coord_fixed:

ggplot(x, aes(fill=is_pass, y=percent_diff, x=difficulty_level)) + 
    geom_bar(position="dodge", stat="identity") +
    coord_fixed(ratio = 0.05)

But it just change the dimension's ratio of the graph, not the size. I'm looking for a simple answer, something like:

ggplot(x, aes(fill=is_pass, y=percent_diff, x=difficulty_level)) + 
    geom_bar(position="dodge", stat="identity") +
    size(width=5, length=4) # or something like this

The second question, is that is_pass is defined as a factor with just two classes 0 and 1. However, ggplots takes is_pass as numeric and doesn't plot it as classes 0 and 1 as you can see in the graph. Why?

Upvotes: 1

Views: 30209

Answers (4)

Maaike Steenhuis
Maaike Steenhuis

Reputation: 21

It has been a long time since this question has been asked. I had a similar issue. I wanted to have the graph itself smaller, but not any of the text in the graph! I'm not sure if this is what you were looking for. But for me, adjusting the fig.height in the chunk helped:

{r fig.height = 10, fig.width = 5}

Upvotes: 2

G5W
G5W

Reputation: 37641

It may meet your need to set the size of the graphics window before you call ggplot.

dev.new(width=5, height=4)

You may need to experiment to get the size that you want.

Here is a complete example.

library(ggplot2)
df <- data.frame(dose=c("D0.5", "D1", "D2"),
                len=c(4.2, 10, 29.5))
head(df)

ggplot(data=df, aes(x=dose, y=len)) +
  geom_bar(stat="identity")

dev.new(width=5, height=4)
ggplot(data=df, aes(x=dose, y=len)) +
  geom_bar(stat="identity")

Two barplots

You can see that the second plot is much smaller.

Upvotes: 0

FMM
FMM

Reputation: 2005

As for the size, you could try maybe setting margins in your theme, something like:

theme(plot.margin = unit(c(1,1,1,1),"cm"))

margins work as t, r, b, l.

Upvotes: 4

Katherine.M
Katherine.M

Reputation: 36

You can adjust the size of bar by adding width in geom_bar() like this: geom_bar(..., width=0.1)

And I don't get what second question is exactly referring to. As is_pass has factor level, it seems to quite make sense that factor value is featured by two different colors.

Upvotes: 1

Related Questions