ggplot2 plot without axes, legends, etc - r

I want to use bioconductor's hexbin (which I can do) to generate a plot that fills the entire (png) display region - no axes, no labels, no background, no nuthin'.

As per my comment in Chase's answer, you can remove a lot of this stuff using element_blank:
dat <- data.frame(x=runif(10),y=runif(10))
p <- ggplot(dat, aes(x=x, y=y)) +
geom_point() +
scale_x_continuous(expand=c(0,0)) +
scale_y_continuous(expand=c(0,0))
p + theme(axis.line=element_blank(),axis.text.x=element_blank(),
axis.text.y=element_blank(),axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),legend.position="none",
panel.background=element_blank(),panel.border=element_blank(),panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),plot.background=element_blank())
It looks like there's still a small margin around the edge of the resulting .png when I save this. Perhaps someone else knows how to remove even that component.
(Historical note: Since ggplot2 version 0.9.2, opts has been deprecated. Instead use theme() and replace theme_blank() with element_blank().)

Re: changing opts to theme etc (for lazy folks):
theme(axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
legend.position="none",
panel.background=element_blank(),
panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
plot.background=element_blank())

Current answers are either incomplete or inefficient. Here is (perhaps) the shortest way to achieve the outcome (using theme_void():
data(diamonds) # Data example
ggplot(data = diamonds, mapping = aes(x = clarity)) + geom_bar(aes(fill = cut)) +
theme_void() + theme(legend.position="none")
The outcome is:
If you are interested in just eliminating the labels, labs(x="", y="") does the trick:
ggplot(data = diamonds, mapping = aes(x = clarity)) + geom_bar(aes(fill = cut)) +
labs(x="", y="")

'opts' is deprecated.
in ggplot2 >= 0.9.2 use
p + theme(legend.position = "none")

Late to the party, but might be of interest...
I find a combination of labs and guides specification useful in many cases:
You want nothing but a grid and a background:
ggplot(diamonds, mapping = aes(x = clarity)) +
geom_bar(aes(fill = cut)) +
labs(x = NULL, y = NULL) +
guides(x = "none", y = "none")
You want to only suppress the tick-mark label of one or both axes:
ggplot(diamonds, mapping = aes(x = clarity)) +
geom_bar(aes(fill = cut)) +
guides(x = "none", y = "none")

xy <- data.frame(x=1:10, y=10:1)
plot <- ggplot(data = xy)+geom_point(aes(x = x, y = y))
plot
panel = grid.get("panel-3-3")
grid.newpage()
pushViewport(viewport(w=1, h=1, name="layout"))
pushViewport(viewport(w=1, h=1, name="panel-3-3"))
upViewport(1)
upViewport(1)
grid.draw(panel)

use ggeasy, it is more simple.
library(ggeasy)
p + theme_classic()+easy_remove_axes() + easy_remove_legend()

Does this do what you want?
p <- ggplot(myData, aes(foo, bar)) + geom_whateverGeomYouWant(more = options) +
p + scale_x_continuous(expand=c(0,0)) +
scale_y_continuous(expand=c(0,0)) +
opts(legend.position = "none")

I didn't find this solution here. It removes all of it using the cowplot package:
library(cowplot)
p + theme_nothing() +
theme(legend.position="none") +
scale_x_continuous(expand=c(0,0)) +
scale_y_continuous(expand=c(0,0)) +
labs(x = NULL, y = NULL)
Just noticed that the same thing can be accomplished using theme.void() like this:
p + theme_void() +
theme(legend.position="none") +
scale_x_continuous(expand=c(0,0)) +
scale_y_continuous(expand=c(0,0)) +
labs(x = NULL, y = NULL)

Related

Remove primary x axis and keep only duplicated secondary axis in ggplot

This is silly. But how I can remove the x axis on the bottom of a ggplot and keep only the secondary axis?
foo <- tibble(x=1:100,y=rnorm(100))
ggplot(foo,aes(x=x,y=y)) + geom_line() +
scale_x_continuous(sec.axis = dup_axis())
EDIT: Thanks to the comments...solved.
ggplot(foo,aes(x=x,y=y)) + geom_line() +
scale_x_continuous(position ="top")
You could set axis.x.bottom parameters in theme. Try:
foo <- tibble(x=1:100,y=rnorm(100))
ggplot(foo,aes(x=x,y=y)) + geom_line() +
scale_x_continuous(sec.axis = dup_axis()) +
theme(axis.text.x.bottom = element_blank(), axis.ticks.x.bottom = element_blank(), axis.title.x.bottom = element_blank())
Embarrassing. I didn't know about the position="top" option. Thanks.
foo <- tibble(x=1:100,y=rnorm(100))
ggplot(foo,aes(x=x,y=y)) + geom_line() +
scale_x_continuous(position ="top")

"Thermal Bar" plot in R?

I want to create a plot with a single vertical bar (colored continuously), with a mark on it showing the score for a particular person. Image:
I can generate the colored bar in ggplot, but only as a legend (not the actual plot). For example the legend resulting from the following is fine:
ggplot(mtcars, aes(x=wt, y=mpg, color=mpg)) +
geom_point() +
scale_color_gradientn(colors = rainbow(5))
Is there any way to do this? Any help would be really appreciated - I'm completely stuck on this.
ggplot(data.frame(y = 51), aes( y=y)) +
geom_tile(data = data.frame(y = 0:100),
aes(x= 0.5, y = y, fill = y)) +
geom_segment(aes(x=0, xend=1, yend=y)) +
geom_text(aes(label = y, x = 1), hjust = -0.3) +
coord_cartesian(clip = "off", xlim = c(0,1.2)) +
scale_fill_gradientn(colors = rainbow(5)) +
scale_x_continuous(labels = NULL) +
guides(fill = FALSE) +
theme_minimal() +
theme(line = element_blank()) +
labs(x="", y = "")

guide_legend() to make legend icons smaller not working

I'm trying to use guides(colour = guide_legend(override.aes = list(size=1))) to modify the legend icon size (make them smaller) in ggplot2().
It doesn't work; I've tried changing colour to fill since that's what I'm calling in aes but that doesn't do the trick either.
Any help would be appreciated!
# Libraries
library(ggplot2)
# Dataframe
df <- data.frame(location=c("all","all","south","south","north","north"),gender=c("female","male","female","male","female","male"),proportion=c(72.5,27.5,71.3,28.7,75.2,24.8))
# Plot
p_mwe <- ggplot(df, aes(x=location,y=proportion,fill=gender)) +
geom_bar(position="dodge", stat="identity") + theme_classic() + coord_flip()+
scale_fill_manual(values=c("gray88","gray44")) + xlab("")+ ylab("prop (%)") +labs(fill="") +
theme(legend.position="top",legend.text = element_text(colour="black", size=10)) + ggtitle("a) MWE plot") + guides(colour = guide_legend(override.aes = list(size=1)))

Is there a way to increase the height of the strip.text bar in a facet?

I would like the grey bar at the top to be wider, as in, have the edges of it a little further from the top and bottom of the letters (the strip.text - A, B, C etc). I would have thought the lineheight would have acted as padding but it doesn't.
ggplot(diamonds, aes(carat, price, fill = ..density..)) +
xlim(0, 2) + stat_binhex(na.rm = TRUE)+
facet_wrap(~ color) +
theme(strip.text = element_text(lineheight=20))
First, modify the levels so that they include a linebreak:
levels(diamonds$color) <- paste0(" \n", levels(diamonds$color) , "\n ")
Then adjust as necessary. eg:
P <- ggplot(diamonds, aes(carat, price, fill = ..density..)) +
xlim(0, 2) + stat_binhex(na.rm = TRUE)+
facet_wrap(~ color)
P + theme(strip.text = element_text(size=9, lineheight=0.5))
P + theme(strip.text = element_text(size=9, lineheight=3.0))
With ggplot2 2.2.0, you can apply Ricardo's trick without modifying the dataframe, by using labeller:
P <- ggplot(diamonds, aes(carat, price, fill = ..density..)) +
xlim(0, 2) + stat_binhex(na.rm = TRUE)+
facet_wrap(~ color, labeller = labeller(color=setNames(paste0("\n", levels(diamonds$color), "\n"), levels(diamonds$color))))
P + theme(strip.text = element_text(size=9, lineheight=0.5))

How to create a legend for lines in scatterplot?

I want to add a legend for the main diagonal and the regression line to the scatter plot.
What I have got now:
library(ggplot2)
df = data.frame(x = 1:10, y = 1:10)
p <- ggplot(df, aes(x, y)) +
geom_point(size=1.2) +
scale_x_continuous(expand=c(0,0)) +
scale_y_continuous(expand=c(0,0)) +
geom_smooth(method="lm", se=FALSE, formula=y~x, colour="blue", fill=NA, size=1.2) +
geom_abline(intercept=0, slope=1, size=1.2, colour="red") +
geom_text(aes(x=max(df[,1])/1.4, y=max(df[,2])/1.2, label=lm_eqn(df)), colour="blue", parse=TRUE) +
# doesn't work: scale_colour_manual("Lines", labels=c("Main Diagonal", "Regression"), values=c("red", "blue")) +
labs(x="X", y="Y")
use show_guide=TRUE e.g.
p <- ggplot(df, aes(x, y)) +
geom_point(size=1.2) +
scale_x_continuous(expand=c(0,0)) +
scale_y_continuous(expand=c(0,0)) +
geom_smooth(method="lm", se=FALSE, formula=y~x, colour="blue", fill=NA, size=1.2) +
geom_abline(aes(colour="red"),intercept=0, slope=1, size=1.2,show_guide=TRUE) +
geom_text(aes(x=max(df[,1])/1.4, y=max(df[,2])/1.2, label="lm_eqn(df)"), colour="blue", parse=TRUE) +
# doesn't work: scale_colour_manual("Lines", labels=c("Main Diagonal", "Regression"), values=c("red", "blue")) +
labs(x="X", y="Y") + opts(legend.position = 'left')
plus you can move legends about using things like+ opts(legend.position = 'left') to get it on the left. I suggest you look at the link provided by Tyler Rinker and also the following:
https://github.com/hadley/ggplot2/wiki/Legend-Attributes
Also no idea what lm_eqn ia so in my code i have surrounded it with "" so it will appear as it is written..
I could finally manage to create a legend for the regression and the diagonal line which is located in the bottom right corner and that makes sense:
p <- ggplot(df, aes(x, y)) +
geom_point(size=1.2) +
scale_x_continuous(expand=c(0,0)) +
scale_y_continuous(expand=c(0,0)) +
geom_abline(aes(colour="red"),intercept=0, slope=1, size=1.2, aes(colour="1"), show_guide=TRUE) + # new code
geom_smooth(method="lm", se=FALSE, formula=y~x, fill=NA, size=1.2, aes(colour="2"), show_guide=TRUE) + # new code
scale_colour_manual("Lines", labels=c("Diagonal", "Regression"), values=c("red", "blue")) +
opts(legend.position = c(0.85, 0.15)) # relative values, must be set individually

Resources