R - Changing ggplot plot size in jupyter - r

Using R in a jupyter notebook, first I set the plot size universally. Second, I would like to plot one single plot with a different size.
## load ggplot2 library
library("ggplot2")
## set universal plot size:
options(repr.plot.width=6, repr.plot.height=4)
## plot figure. This figure will be 6 X 4
ggplot(iris, aes(x = Sepal.Length, y= Sepal.Width)) + geom_point()
## plot another figure. This figure I would like to be 10X8
ggplot(iris, aes(x = Sepal.Length, y= Sepal.Width)) + geom_point() + HOW DO i CHANGE THE SIZE?
As you can see, I would like to change the second plot (and only the second plot) to be a 10X8. How do I do this?
Sorry for a potentially dumb question, as plot sizing is typically not an issue in Rstudio.

Here you go:
library(repr)
options(repr.plot.width=10, repr.plot.height=8)
ggplot(iris, aes(x = Sepal.Length, y= Sepal.Width)) +
geom_point()

I've found another solution which allows to set plot size even when you make plots inside function or a loop:
pl <- ggplot(iris, aes(x = Sepal.Length, y= Sepal.Width)) + geom_point()
print(pl, vp=grid::viewport(width=unit(10, 'inch'), height=unit(8, 'inch')))

If options is the only mechanism available to change figure size, then you'd do something like this to set & restore the options to whatever they were:
saved <- options(repr.plot.width=10, repr.plot.height=8)
ggplot(iris, aes(x = Sepal.Length, y= Sepal.Width)) + geom_point()
options(saved)

A solution that doesn't involve using external packages is this.
fig <- function(width, heigth){
options(repr.plot.width = width, repr.plot.height = heigth)
}
Just put fig(10, 4), for example, in the cell that generates the plot, and the plot will be scaled accordingly
Source: https://www.kaggle.com/getting-started/105201

Related

Producing two lines of complex y axis titles using atop, \n, etc in ggplot in R

I am trying to produce two lines in a y axis label in ggplot in R, where the second line has some relatively complex text that seems to make it difficult to print on two lines.
On a single line, I have gotten this script to print just fine using bquote:
Figure 1:
ggplot(data = iris, aes(x = Sepal.Width, y = Sepal.Length)) + geom_point() +
ylab(bquote('Efflux ('*mu~ 'mol' ~CO[2]~ m^-2~s^-1*')'))
And, with relatively simple text, I can use the \n feature within ylab to produce two lines:
Figure 2:
ggplot(data = iris, aes(x = Sepal.Width, y = Sepal.Length)) + geom_point() +
ylab("Efflux \n Carbon Dioxide")
However, trying to use the atop feature within bquote, I have struggled to figure out how to use commas, quotations marks etc. to properly print these two lines of y axis labels:
ggplot(data = iris, aes(x = Sepal.Width, y = Sepal.Length)) + geom_point() +
ylab(bquote(atop("Efflux", ('*mu~ 'mol' ~CO[2]~ m^-2~s^-1*'))))
This prints an error, which I know has to do with placement of quotation marks. Does anyone have a lead on how to print this more complex text over two lines?
You could try using element_markdown from the ggtext package. This allows you to use markdown (or html) to produce the line breaks, symbols, subscripts and superscripts you need without resorting to bquote , which struggles with multi-line inputs. You can even show the units in italics, as in this example:
library(ggplot2)
library(ggtext)
ggplot(data = iris, aes(x = Sepal.Width, y = Sepal.Length)) +
geom_point() +
ylab("Efflux<br><i>(μmol CO<sub>2</sub> m<sup>-2</sup>s<sup>-1</sup>)</i>") +
theme(axis.title.y = element_markdown())

Draw two plots in R with ggplot and par

I start to study R. I'm starting with Iris dataset in the package datasets. To draw som graph I need to use the ggplot2 package. How can I split the Plots window and draw two graphs?
I try with the following code, but only one graph is showed.
iris=datasets::iris
par(mfrow=c(2,1))
ggplot(iris, aes(x=Sepal.Length,y=Sepal.Width,color=Species))+ geom_point(size=3)
ggplot(iris, aes(x=Petal.Length,y=Petal.Width,color=Species))+ geom_point(size=3)
use win.graph() to split the window into two.
Since you have not provided dataset, if you want to create a side by side plot try based on my example below
Try this:
library(cowplot)
iris1 <- ggplot(iris, aes(x = Species, y = Sepal.Length)) +
geom_boxplot() + theme_bw()
iris2 <- ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
geom_density(alpha = 0.7) + theme_bw() +
theme(legend.position = c(0.8, 0.8))
plot_grid(iris1, iris2, labels = "AUTO")
As ggplot2 is based on grid graphics system instead of base plot, par does not effective in adjusting ggplot2 plots, and the latest version of ggplot2 has already supported the arrangement of different plots, and you can set tags for each of them:
iris=datasets::iris
ggplot(iris, aes(x=Sepal.Length,y=Sepal.Width,color=Species))+ geom_point(size=3) + labs(tag = "A") -> p1
ggplot(iris, aes(x=Petal.Length,y=Petal.Width,color=Species))+ geom_point(size=3) + labs(tag = "B") -> p2
p1 + p2
For more sophisticated arrangement, you can use patchwork package to arrange them

modifying ggplot objects after creation

Is there a preferred way to modify ggplot objects after creation?
For example I recommend my students to save the r object together with the pdf file for later changes...
library(ggplot2)
graph <-
ggplot(mtcars, aes(x=mpg, y=qsec, fill=cyl)) +
geom_point() +
geom_text(aes(label=rownames(mtcars))) +
xlab('miles per galon') +
ggtitle('my title')
ggsave('test.pdf', graph)
save(graph, file='graph.RData')
So new, in case they have to change title or labels or sometimes other things, they can easily load the object and change simple things.
load('graph.RData')
print(graph)
graph +
ggtitle('better title') +
ylab('seconds per quarter mile')
What do I have to do for example to change the colour to discrete scale? In the original plot I would wrap the y in as.factor. But is there a way to do it afterwards?
Or is there a better way on modifying the objects, when the data is gone. Would love to get some advice.
You could use ggplot_build() to alter the plot without the code or data:
Example plot:
data("iris")
p <- ggplot(iris) +
aes(x = Sepal.Length, y = Sepal.Width, colour = Species) +
geom_point()
Colours are respective to Species.
Disassemble the plot using ggplot_build():
q <- ggplot_build(p)
Take a look at the object q to see what is happening here.
To change the colour of the point, you can alter the respective table in q:
q$data[[1]]$colour <- "black"
Reassemble the plot using ggplot_gtable():
q <- ggplot_gtable(q)
And plot it:
plot(q)
Now, the points are black.

How to set upper bound for x-axes for ploting using ggplot?

I'm using ggplot to create the plot of the density of my data.But I want to get plot for the X value until 2.0.
How can I put this restiriction in my code?
Here is my code and the plot:
ggplot()+geom_density(aes(data=Ge,x=GeN[,1]),color='red')
ggplot(iris, aes(x = Petal.Length)) + geom_density()
ggplot(iris, aes(x = Petal.Length)) + geom_density() + xlim(0, 2)

How would you plot a box plot and specific points on the same plot?

We can draw box plot as below:
qplot(factor(cyl), mpg, data = mtcars, geom = "boxplot")
and point as:
qplot(factor(cyl), mpg, data = mtcars, geom = "point")
How would you combine both - but just to show a few specific points(say when wt is less than 2) on top of the box?
If you are trying to plot two geoms with two different datasets (boxplot for mtcars, points for a data.frame of literal values), this is a way to do it that makes your intent clear. This works with the current (Sep 2016) version of ggplot (ggplot2_2.1.0)
library(ggplot2)
ggplot() +
# box plot of mtcars (mpg vs cyl)
geom_boxplot(data = mtcars,
aes(x = factor(cyl), y= mpg)) +
# points of data.frame literal
geom_point(data = data.frame(x = factor(c(4,6,8)), y = c(15,20,25)),
aes(x=x, y=y),
color = 'red')
I threw in a color = 'red' for the set of points, so it's easy to distinguish them from the points generated as part of geom_boxplot
Use + geom_point(...) on your qplot (just add a + geom_point() to get all the points plotted).
To plot selectively just select those points that you want to plot:
n <- nrow(mtcars)
# plot every second point
idx <- seq(1,n,by=2)
qplot( factor(cyl), mpg, data=mtcars, geom="boxplot" ) +
geom_point( aes(x=factor(cyl)[idx],y=mpg[idx]) ) # <-- see [idx] ?
If you know the points before-hand, you can feed them in directly e.g.:
qplot( factor(cyl), mpg, data=mtcars, geom="boxplot" ) +
geom_point( aes(x=factor(c(4,6,8)),y=c(15,20,25)) ) # plot (4,15),(6,20),...
You can show both by using ggplot() rather than qplot(). The syntax may be a little harder to understand, but you can usually get much more done. If you want to plot both the box plot and the points you can write:
boxpt <- ggplot(data = mtcars, aes(factor(cyl), mpg))
boxpt + geom_boxplot(aes(factor(cyl), mpg)) + geom_point(aes(factor(cyl), mpg))
I don't know what you mean by only plotting specific points on top of the box, but if you want a cheap (and probably not very smart) way of just showing points above the edge of the box, here it is:
boxpt + geom_boxplot(aes(factor(cyl), mpg)) + geom_point(data = ddply(mtcars, .(cyl),summarise, mpg = mpg[mpg > quantile(mpg, 0.75)]), aes(factor(cyl), mpg))
Basically it's the same thing except for the data supplied to geom_point is adjusted to include only the mpg numbers in the top quarter of the distribution by cylinder. In general I'm not sure this is good practice because I think people expect to see points beyond the whiskers only, but there you go.

Resources