I am trying to make a plot with no information beyond the data. No axes; no grid; no title; just the plot.
But I keep getting extra margins and padding that I can't remove.
library(ggplot2)
library(grid)
theme_bare <- 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(),
#axis.ticks.length = unit(0, "lines"), # Error
axis.ticks.margin = unit(c(0,0,0,0), "lines"),
legend.position = "none",
panel.background = element_rect(fill = "gray"),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.margin = unit(c(0,0,0,0), "lines"),
plot.background = element_rect(fill = "blue"),
plot.margin = unit(c(0,0,0,0), "lines")
)
ggplot() +
geom_area (data=economics, aes(x = date, y = unemploy), linetype=0) +
theme_bare
Produces this image:
What I want is this:
I can't figure out how to get rid of the blue and make the dark gray flush with the edges.
Could any one offer some advice?
Here is the way to plot only the panel region:
p <- ggplot() + geom_area (data=economics, aes(x = date, y = unemploy), linetype=0) +
scale_x_date(expand = c(0,0)) + scale_y_continuous(expand = c(0,0)) +
theme(line = element_blank(),
text = element_blank(),
title = element_blank())
gt <- ggplot_gtable(ggplot_build(p))
ge <- subset(gt$layout, name == "panel")
grid.draw(gt[ge$t:ge$b, ge$l:ge$r])
From ggplot2_2.0.0 you can use theme_void:
ggplot() +
geom_area(data = economics, aes(x = date, y = unemploy), linetype = 0) +
theme_void()
try
last_plot() + theme(axis.ticks.length = unit(0.001, "mm")) + labs(x=NULL, y=NULL)
you may want to file a bug for the 0 tick length.
If you just want to remove the grid in theme_bw(), you can use:
+ theme_bw() + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
Related
This is my script for the plot,
data = data.frame(Kingdom = c("Bacteria", "Archaea"),
Total = c(273523, 2616))
sizeRange <- c(0,30)
library(ggplot2)
ggplot(data, aes(x=0,y=Kingdom,color=Kingdom)) +
geom_point(aes(size = Total,alpha=10),colour="blue",stroke=2) +
scale_size(range = sizeRange)+
theme_bw() +
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "white"))
somebody, please tell me how can I get a connecting line between my y-axis label and the plot
My plot looks like this
I want something like this
A clean alternative would be to label the points directly, and remove the y-axis if wanted. e.g.:
ggplot(data, aes(x=0,y=Kingdom,color=Kingdom)) +
ggrepel::geom_text_repel(aes(label = Kingdom), vjust = -1,colour="black") +
geom_point(aes(size = Total),colour="blue",stroke=2) +
scale_size(range = sizeRange)+
theme_bw() +
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "white"),
axis.text.y=element_blank(),
axis.title.y = element_blank(),
axis.ticks.y=element_blank())
you can manually add segments, but then the alpha of your points will kind of show them.
Here is a try, altought it's not perfect if the x axis expend.
ggplot(data, aes(x=0,y=Kingdom,color=Kingdom)) +
# Added the segments here before the points.
# I tried to alpha it but I can't figure out how to limit the
# segment to the point border.
geom_segment(x = rep(-100,2), xend = rep(0,2),
y = c(1, 2), yend = c(1,2),colour="blue", alpha = 10) +
geom_point(aes(size = Total,alpha=10),colour="blue",stroke=2) +
scale_size(range = sizeRange)+
theme_bw() + guides(alpha = "none") + # remove alpha from legend.
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "white"))
I have two plots I would like to combine. My data looks like this:
Year<-rep(2001:2005, each = 5)
name<-c("John","Ellen","Mark","Randy","Luisa")
Name<-c(rep(name,5))
Value<-sample(seq(0,25,by=1),25)
mydata<-data.frame(Year,Name,Value)
This is the first barplot:
tot<-aggregate(mydata$Value,list(mydata$Year),FUN=sum)
tot_y<-tot$x
tot_x<-tot$Group.1
tot_barplot <- ggplot(tot, aes(x=tot_x,y=tot_y)) +
geom_bar(stat = "identity",fill="#73D055FF") +
scale_y_continuous(limits = c(0, 125), breaks = seq(0, 125, by = 25)) +
#xlab("Pathways") +
#ylab("N° of species") +
theme(axis.line = element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_text(size=14,margin=margin(l=10),colour="black"),
axis.ticks = element_blank(),
axis.title=element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank())
tot_barplot
And this is the second plot:
p <- ggplot(mydata, aes(x=Year, y=Name, size = Value)) +
geom_point(aes(fill = Value,
alpha = I(as.numeric(Value > 0))), shape=21, colour = "black") +
scale_fill_viridis_c(option = "D", direction = -1,limits = c(1, 25), breaks=seq(1, 25, 5))+
scale_size_area(guide = "none") +
ylab("Name") +
theme(axis.line = element_blank(),
axis.text.x=element_text(size=11,margin=margin(b=10),colour="black"),
axis.text.y=element_text(size=13,margin=margin(l=10),colour="black",
face="italic"),
axis.ticks = element_blank(),
axis.title=element_text(size=18,face="bold"),
panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(),
legend.text = element_text(size=14),
legend.title = element_text(size=18))
p
I combine them like this:
grid.arrange(arrangeGrob(tot_barplot,p,nrow=2))
Now I would like to re-size the barplot to fit it better to the second plot (imagine that the original data produce a wider barplot where the bars start above the Name and end above the legend Value). I would like the bars of the barplot to be exactly centred above the line of points and the Year, but I am not very familiar with ggplot aesthetics.
Any suggestion would be appreciated. Thanks!
I'd like to make the border around a legend tighter when the legend has no title. As it is, there is a blank space above the legend key. I'd also like the border to be a dotted line.
The plot is based on the following code:
library(ggplot2)
ggplot(temp, aes(x=diff_abs, y=emp_est_15, color=diff_sign)) + geom_point(shape=1, size=2) +
scale_colour_manual(values=c("green4", "red")) +
scale_x_log10(breaks=10^(0:3)) + scale_y_log10(breaks=c(c(2,4,8) %o% 10^(0:3))) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"),
axis.text = element_text(color="black", size=13), axis.title = element_text(color="black", size=13),
legend.key = element_blank(), legend.position=c(.2,.8), legend.box.background = element_rect(),
legend.background = element_blank()) +
labs(x="\nGain ou perte d'emploi 2001-2015 (milliers, échelle log 10)",
y="Emploi 2015 (milliers, échelle log 10)\n", color="")
As Richard Telford mentioned in the comment, setting legend.title = element_blank() will remove the space occupied by legend title & hence "tighten" the legend box.
Legend box's border type can be changed with legend.box.background = element_rect(line = <some number other than 1>)
# example using mtcars dataset
p <- ggplot(mtcars, aes(wt, mpg, col = factor(cyl)))
p + geom_point() +
theme_bw() +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(),
legend.key = element_blank(), legend.position=c(.8,.8),
legend.title = element_blank(),
legend.box.background = element_rect(line = 3),
legend.background = element_blank())
A small addendum to Z. Lin answer; one can get rid of the remaining blank space on the top by setting legend.spacing.y = unit( 0, 'pt' )
p <- ggplot(mtcars, aes(wt, mpg, col = factor(cyl))) + geom_point() +
theme_bw( base_size = 80 ) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(),
legend.key = element_blank(), legend.position=c( 0.5, 0.5 ),
legend.title = element_blank(),
legend.box.background = element_rect(line = 3),
legend.background = element_blank() )
lgndA <- cowplot::get_legend( p )
lgndB <- cowplot::get_legend( p + theme( legend.spacing.y = unit( 0, 'pt' ) ) )
grid::grid.newpage()
cowplot::plot_grid( lgndA, lgndB )
Legends with different legend.spacing.y option in ggplot2
I have been asked to place a full border around my plot below:
Using panel.border = element_rect(colour = "black") results in losing in the plot becoming blank.
I can't use theme_bw() as it does not have the same functionality as the usual theme, the code I am currently using is below:
graph<-ggplot(d,aes(x=d$AOE, y=d$MEI)
)+
geom_point(shape=20, size=3)+
geom_rug()+
annotate("text", x = -1.1, y = 14000, label = "27/04/2011") +
annotate("text", x = -1.3, y = 10400, label = "03/04/1974") +
xlab("MEI")+
ylab("AOE")+
scale_y_log10()+
theme(axis.text.y = element_text(size=14),
axis.text.x = element_text(size=14),
axis.title.y = element_text(size=14),
axis.title.x = element_text(size=14),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black")
)
graph
Any advice on how to get a full black border would be very much appreciated!
To use panel.border you also have to specify a blank fill using fill=NA.
Try this:
library(ggplot2)
ggplot(mtcars, aes(mpg, disp)) + geom_point() + geom_rug() +
theme(axis.text.y = element_text(size=14),
axis.text.x = element_text(size=14),
axis.title.y = element_text(size=14),
axis.title.x = element_text(size=14),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
panel.border = element_rect(colour = "black", fill=NA, size=5)
)
You can use theme_bw() and theme() together. This should work:
# creating some data
set.seed(1)
d <- data.frame(MEI=rnorm(100), AOE=rlnorm(100, 10, 5))
# creating the plot
ggplot(d,aes(x=MEI, y=AOE)) +
geom_point(shape=20, size=3) +
geom_rug() +
scale_y_log10() +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(colour = "black", size=4))
this gives:
A solution without theme_bw() and inspired by #Andrie, but with the use of panel.background instead of panel.border:
ggplot(d,aes(x=MEI, y=AOE)) +
geom_point(shape=20, size=3) +
geom_rug() +
scale_y_log10() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(colour = "black", size=4, fill=NA))
this will give the exact same plot. The difference between panel.background and panel.border is that panel.background is drawn underneath the plot and panel.border is drawn on top of the plot.
If you use any of the panel. options you will get a border around each individual facet when facetting. If you want a border around the outside of the entire plot, title etc included, then use plot.background. E.g:
library(ggplot2)
ggplot(mtcars, aes(mpg, disp)) + geom_point() + geom_rug() +
labs(title = "Hello plot!") +
facet_wrap(~cyl) +
theme(axis.text.y = element_text(size=14),
axis.text.x = element_text(size=14),
axis.title.y = element_text(size=14),
axis.title.x = element_text(size=14),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
plot.background = element_rect(colour = "black", fill=NA, size=5)
)
Created on 2021-06-22 by the reprex package (v2.0.0)
I am trying to remove the y-axis on two ggplots that are in a gtable with a third ggplot. I would like to show the y-axis for the leftmost graph in the gtable and remove the y-axis completely from the subsequent graphs; however, I would like the x-axis to remain on all plots.
My graph looks like this:
![nucleotide diversity][1]
[1]: image produced by code
library("ggplot2")
library("gridExtra")
library("gtable")
theme_set(theme_bw(base_size=16))
p1 <- ggplot(a.pi, aes(x=window, y=measure, fill=key, colour=key)) +
geom_line() +
scale_colour_manual(values=c("#000099", "#333333", "#FF0000")) +
ylab(expression(pi)) +
xlab("Position") +
scale_x_continuous(breaks=c(1e+06, 2e+06, 3e+06, 4e+06), labels=c("1Mb", "2Mb", "3Mb", "4Mb"))+
scale_y_continuous(limits=c(0.0,0.0004)) +
theme(#axis.text.y = element_blank(),
#axis.ticks.y = element_blank(),
#axis.title.y = element_blank(),
#axis.title.x = element_blank(),
plot.margin = unit(c(0,-3,0,0), "lines"),
plot.background = element_blank(),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
legend.position="none",
axis.line = element_line()
)
p2 <- ggplot(b.pi, aes(x=window, y=measure, fill=key, colour=key)) +
geom_line() +
scale_colour_manual(values=c("#333333", "#FF0000")) +
#ylab(expression(pi)) +
xlab("Position") +
scale_x_continuous(breaks=c(1e+06, 2e+06, 3e+06, 4e+06), labels=c("1Mb", "2Mb", "3Mb", "4Mb"))+
scale_y_continuous(limits=c(0.0,0.0004)) +
theme(axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.title.y = element_blank(),
#axis.title.x = element_blank(),
plot.margin = unit(c(0,-3,0,0), "lines"),
plot.background = element_blank(),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
legend.position="none",
axis.line = element_line()
)
p3 <- ggplot(c.pi, aes(x=window, y=measure, fill=key, colour=key)) +
geom_line() +
scale_colour_manual(values=c("#333333", "#FF0000")) +
#ylab(expression(pi)) +
xlab("Position") +
scale_x_continuous(breaks=c(1e+06, 2e+06, 3e+06, 4e+06), labels=c("1Mb", "2Mb", "3Mb", "4Mb"))+
scale_y_continuous(limits=c(0.0,0.0004)) +
theme(axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.title.y = element_blank(),
#axis.title.x = element_blank(),
plot.margin = unit(c(0,-3,0,0), "lines"),
plot.background = element_blank(),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
legend.position="none",
axis.line = element_line()
)
grid.arrange(p1,p2,p3, nrow=1)
gt1 <- ggplot_gtable(ggplot_build(p1))
gt2 <- ggplot_gtable(ggplot_build(p2))
gt3 <- ggplot_gtable(ggplot_build(p3))
newWidth = unit.pmax(gt1$widths[1:3], gt2$widths[1:3], gt3$widths[1:3])
gt1$widths[1:3] = as.list(newWidth)
gt2$widths[1:3] = as.list(newWidth)
gt3$widths[1:3] = as.list(newWidth)
# New gtable with space for the three plots plus a right-hand margin
gt = gtable(widths = unit(c(1, 1, 1, 0.3), "null"), height = unit(1, "null"))
# Instert gt1, gt2 and gt2 into the new gtable
gt <- gtable_add_grob(gt, gt1, 1, 1)
gt <- gtable_add_grob(gt, gt2, 1, 2)
gt <- gtable_add_grob(gt, gt3, 1, 3)
grid.newpage()
grid.draw(gt)
Your linked image is not showing, but here is my shot in the dark:
Change from this:
plot1 <- theme(#axis.text.y = element_blank(),...
to this:
plot1 <- theme(axis.text.y = element_text(),...
if you want to change the label do this:
plot1 <- ... + ylab("y-axis label")
You need to do two things:
One is set axis.title.x and axis.title.y as blank under options (opts).
The other one is set xlab("") and ylab("")
I have included a code snippet in case it helps:
ggplot(space[1:closetos[i],], aes(dim1, dim9, colour = name,
shape=shape))+ opts(axis.line = theme_segment(colour = "black"),
panel.grid.major = theme_blank(),
panel.grid.minor = theme_blank(),
panel.border = theme_blank(),
panel.background = theme_blank(),
axis.title.x = theme_blank(),
axis.title.y = theme_blank())+
xlab("") +
ylab("")+
theme(text = element_text(size=15, colour = "black"),
axis.text.x = element_text(angle=0, vjust=1, colour = "black"),
axis.text.y = element_text(angle=0, vjust=1, colour = "black"),
axis.line = element_line(colour = 'black', size = 1),
axis.ticks = element_line(colour = 'black', size = 1),
axis.ticks.length = unit(0.3, "cm"),
axis.title.y=element_text(vjust=0.4),
legend.position = "none") +
geom_point(size=5)+
scale_color_manual("Status", values = mycolours) +
xlim((space$dim1[closetos[i]]-0.01), (space$dim1[closetos[i]]+0.01)) +
ylim((space$dim9[closetos[i]]-0.01), (space$dim9[closetos[i]]+0.01))