I'm having issues with changing the size of the title, X-Y labels, X-Y axis text for my ggplot2. I'm using ggsave to save the plot as a jpg.
p <- ggplot()
p + theme(axis.title = element_text(size=30), axis.text.y = element_text(size=30),
axis.text.x = element_text(size=30))
but changing the sizes of these texts doesn't change anything on the plot. Would anyone know how to properly change the text sizes?
So I fixed the issue I was having so the changes I make to theme are not affecting the plot (I've tested with changing text color), however the size of the axis text still does not change.
p <- ggplot(d[d$user==i,], aes(x=date, y=url, group=user, label=user)) + geom_line() + geom_point() +
labs(list(title=i, x="Date and Time", y = "URL")) + # Plot labels
axis.POSIXct(1, at=seq(daterange[1], daterange[2], by="hour")) # Set x axis range to first and last date-time in data
p <- p + modifiedtheme
ggsave(plot = p, filename = "sample.jpg", height=2, width=6)
Here is a minimal, fully reproducible version of the problem (or lack of any problem, as comments have pointed out). Your own posted code appears to be correct, but maybe this example will help you solve whatever the real problem is:
library(ggplot2)
p1 = ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, colour=Species)) +
geom_point()
p2 = ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, colour=Species)) +
geom_point() +
theme(axis.title=element_text(size=30))
ggsave("figure1.jpg", plot=p1, height=3, width=4, units="in", dpi=150)
ggsave("figure2.jpg", plot=p2, height=3, width=4, units="in", dpi=150)
Related
I am a newbie attempting to change the width of a ggplot, so that I am can arrange different plots(heatmap and dotplot) in the same figure. However, after hours of trying to reduce the width of the dotplot, I am about to give up.
Code for heatmap (maybe not relevant):
heatmap_GO_NES_1<-ggplot(data=long_frame_GO_NES_1) +
geom_tile(mapping = aes(
x = factor(timepoint,levels = c("6h","12h","24h")),
y =bio_process,fill = NES)) +
ylab(label="Biological process") +
theme(axis.title.x=element_blank()) +
scale_fill_gradient(low="red",high="green")+
facet_grid( group ~. , scales="free",space="free")+
theme(axis.text.x = element_text(angle = 90))+
theme(strip.text.y = element_text(size = 8))
heatmap_GO_NES_1
Code for dotplot:
dot_GO_NES_1<- ggplot(data=long_frame_GO_NES_2)+
geom_count(mapping=aes(x=timepoint, y =bio_process, size=setsize))+
theme(axis.title.x=element_blank(), axis.text.x=element_blank(),
axis.ticks.x=element_blank(),axis.title.y=element_blank(),
axis.text.y=element_blank(),axis.ticks.y=element_blank())
dot_GO_NES_1
Code for figure:
plot_grid(heatmap_GO_NES_1,dot_GO_NES_1)
Obviously, the dotplot is stealing all the figure space, so that my heatmap does not show up in the figure.
TL;DR - you need to use the rel_widths= argument of plot_grid(). Let me illustrate with an example using mtcars:
# Plots to display
p1 <- ggplot(mtcars, aes(mpg, disp)) + geom_point()
p2 <- ggplot(mtcars, aes(x='X', y=disp)) + geom_point(aes(size=cyl))
Here are the plots, where you see p2 is like your plot... should not be too wide or it looks ridiculous. This is the default behavior of plot_grid(), which makes both plots the same width/relative size:
plot_grid(p1,p2)
Adjust the relative width of the plots using rel_widths=:
plot_grid(p1,p2, rel_widths=c(1,0.3))
I am having trouble with excessively wide panel labels on my ggplot2 faceted plot.
Here is the code that I used to generate the plot:
png(paste("/directory/", hgnc_symbol, "_", curr_gene, ".png", sep=""),
width=4, height=3, units="in", pointsize=1, res=300)
print({barplot <-
ggplot(curr_data, aes(x = condition, y = tpm, fill=condition)) +
geom_boxplot(outlier.colour=NA, lwd=0.2, color="grey18") +
stat_boxplot(geom ='errorbar', color="grey18") +
geom_jitter(size=0.8) +
facet_wrap(~target_id) +
guides(fill=FALSE) +
theme_bw() +
labs(title=paste(hgnc_symbol, "_", curr_gene, sep="")) +
labs(x="condition") + labs(y="TPM") +
theme(text = element_text(size=5), strip.background=element_rect(size = 1),
axis.text.x = element_text(angle = 90, hjust = 1, size=4.5))})
dev.off()
The plot comes out looking like this:
As you can see, the background of the panel labels is so wide, that the plots themselves are barely visible. The points plotted on the graph are also much larger than I expected them to be.
The odd thing is that I used this same exact code to produce this following plot (which looks good) just a few days ago:
What is causing this difference, and how can I fix the problem?
In ggplot, text is defined in pts, which are absolute units of measure. The plot panel is relative in size and scales according to the dimensions you specify when you save your plot. If the dimensions are small, then the text will be large relative to the panel areas. If the dimensions are large, then the text will be small relative to the panel areas. See examples below:
ggplot(diamonds, aes(x = x, y =y)) + geom_point() + facet_wrap(~ clarity)
ggsave("filepath//4x3.png", width=4, height = 3)
ggsave("filepath//8x6.png", width=8, height = 6)
The 4 x 3 in plot:
The 8 x 6 in plot:
You can edit the grobs to exert finer control on the plot dimensions (example).
I am trying to set titles to some ggplot2 charts, while leaving some without titles. Unfortunately, when title is set, the y axis and the plot shrink (see the plot on the right). I need to plot the title without changing the Y axis size, so that titled charts are on the same scale with the others (as in the middle plot).
grid.arrange(
(ggplot(mtcars, aes(mpg, hp)) + geom_point()),
(ggplot(mtcars, aes(mpg, hp)) + geom_point() +
geom_text(aes(22.5, 340, label="fake title", vjust = 1, hjust = .5, show_guide = FALSE))),
(ggplot(mtcars, aes(mpg, hp)) + geom_point() +
labs(title="real title")),
ncol=3)
I cannot use fake empty-string titles on the other plots, because I am short on space.
I could use the geom_text() method, if anyone can tell me how to make it look less garbled.
So, how do I removing any reserved space for the title above the plot, while still showing the plot title on and at the top of the plot area? The latter is done with theme(plot.title = element_text(vjust=-1)).)
Edit
Thanks to #baptiste for pointing out a more concise way to accomplish this. Given p1, p2, and p3 from below:
pl = lapply(list(p1,p2,p3), ggplotGrob)
grid.newpage()
grid.draw(do.call(cbind, c(pl, size="first")))
Original answer
You can build the ggplot grobs and standardize the heights parameter across plots:
p1 <- ggplot(mtcars, aes(mpg, hp)) + geom_point()
p2 <- ggplot(mtcars, aes(mpg, hp)) + geom_point() + labs(title="real title")
p3 <- ggplot(mtcars, aes(mpg, hp)) + geom_point() +
geom_text(aes(22.5, 340, label="fake title", vjust = 1, hjust = .5, show_guide = FALSE))
p1 <- ggplot_gtable(ggplot_build(p1))
p2 <- ggplot_gtable(ggplot_build(p2))
p3 <- ggplot_gtable(ggplot_build(p3))
p2$heights <- p1$heights
p3$heights <- p1$heights
grid.arrange(p1, p2, p3, ncol=3)
You can then use the title's vjust setting to move it off of the plot or further onto the plot, if you want:
p2 <- ggplot(mtcars, aes(mpg, hp)) + geom_point() +
labs(title="real title") +
theme(plot.title=element_text(vjust=-.3))
You should use annotate for this. If you use geom_text, it will print as many labels as there are rows in your data, hence the poor-looking overplotted label in your question. A painful work-around is to create a 1-row data frame to use as the data for the geom_text layer. However, annotate is intended for this sort of thing so you don't need a work-around. Something like:
annotate(geom = "text", x = 22.5, y = 340, label="fake title")
is good practice. Annotate is also useful for adding single horizontal or vertical lines to a plot, or to highlight a region by drawing a rectangle around it.
Edit: The problem was due to my incorrectly attempting to alter theme(title = element_text()), when I needed to alter theme(plot.title(element_text()). I would have noticed this had I more carefully scrutinized the theme() documentation.
Original Post:
Changing the title vertical justification alters the position of the x and y axis labels as well. Is this a bug? Or am I misinterpreting the function of theme()? I am running ggplot2 version 0.9.3.1
Minimal reproducible example.
require(ggplot2)
set.seed(12345)
x <- rnorm(100,10,0.5)
y <- x * 3 + rnorm(100)
df <- data.frame(y,y)
The default title is too close to the graph for my taste....
ggplot(df,aes(x,y)) +
geom_point() +
labs(title="My Nice Graph")
When I try to move the title, the axis labels move also, and illegibly plot on the graph.
ggplot(df,aes(x,y)) +
geom_point() +
labs(title="My Nice Graph") +
theme(title = element_text(vjust=2))
You want plot.title not title:
labs(title="My Nice Graph") + theme(plot.title = element_text(vjust=2))
Alternate quick fix is adding a line break:
labs(title="My Nice Graph\n")
vjust does not work for me (I also think values should be in [0, 1]). I use
... +
theme(
plot.title = element_text(margin=margin(b = 10, unit = "pt"))
)
i'm looking for some help in creating a faceted plot with angeled x-axis tick labels, which is probably best explained by the following example:
require(ggplot2)
df <- data.frame(group=factor(c('sex','sex','race','race')), variable=c('Female','Male','White','African American'), value=1:4)
p <- ggplot(aes(x=variable, y=value), data=df)
p <- p + geom_line()
p <- p + facet_grid(. ~ group, scale="free")
p <- p + opts(axis.text.x=theme_text(angle=45,hjust=1,vjust=1))
ggsave(p, file='faceted.pdf', width=6, height=4)
which produces this figure where the x-ticks on the right are misaligned:
it seems that the problem is introduced when using scale="free" in the facet_grid and is related to the varying tick label length.
any suggestions are greatly appreciated.
Looks like a bug: https://github.com/hadley/ggplot2/issues/221 that has apparently been fixed in ggplot2 0.9 and later.
> help(package='ggplot2')
Information on package ‘ggplot2’
Description:
Package: ggplot2
Type: Package
Title: An implementation of the Grammar of Graphics
Version: 0.8.9
(Too bad for me!)
Bug reproducible with:
qplot(reorder(model, hwy), hwy, data=mpg) +
facet_grid(. ~ manufacturer, scales="free") +
opts(axis.text.x = theme_text(angle=90))
If you really got just a few x-labels - who needs ticks mate? Suppress them with axis.ticks and get rid of horizontal/vertical justification.
require(ggplot2)
df <- data.frame(group=factor(c('sex','sex','race','race')),
variable=c('Female','Male','White','African American'),
value=1:4)
p <- ggplot(df,aes(x=variable, y=value)) + geom_line()
p <- p + facet_grid(. ~ group, scale="free")
p <- p + opts(axis.text.x=theme_text(angle=45),
axis.ticks = theme_blank(),axis.title.y=theme_blank())
ggsave(p, file='no_ticks.png', width=6, height=4)
A bit late (i.e. 9 years after the last answer ;) but still interesting, here is another way to define the angle of axes labels (here: 90° rotation of x-axis labels):
ggplot(df,aes(x=variable, y=value)) + geom_line() +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))
(where vjust and hjust are vertical and horizontal alignment respectively)