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
Related
Hello I have Dataframe ploting scatter plot with color using other column values plot is looking fine but the colour values are start from 0-100 and most of the values are in between 50-100 so I wanted put more color to diffrentiat can any one suggest me how can I do with the R, I tried with viridis color it is also looking same 50-100 color is almost looking similar color
link for the data
https://drive.google.com/file/d/1EKhRwup3vUC3KVFOOh4XtKERIr8FQj3x/view?usp=sharing
code what I tried
df=read.table("test.txt",sep='\t', header=TRUE)
df = data.frame(df)
p=ggplot(df, aes(log(data1), log(data2)),cex=1.9)+
geom_point(aes(color =data3)) +
theme(legend.position = "top")+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))+
theme(text = element_text(size = 20, face="bold"))
You can play with a customized color palette and scale_colour_gradientn like this:
library(RColorBrewer)
library(ggplot2)
#Data
df <- read.delim(file='test.txt',stringsAsFactors = F)
#Palette
myPalette <- colorRampPalette(rev(brewer.pal(11, "Spectral")))
sc <- scale_colour_gradientn(colours = myPalette(100))
#Plot
ggplot(df, aes(log(data1), log(data2)),cex=1.9)+
geom_point(aes(color =data3)) + sc +
theme(legend.position = "top")+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))+
theme(text = element_text(size = 20, face="bold"))
Output:
If you want more color try this:
#Palette 2
sc2 <- scale_colour_gradientn(colours = rainbow(7))
#Plot
ggplot(df, aes(log(data1), log(data2)),cex=1.9)+
geom_point(aes(color =data3)) + sc2 +
theme(legend.position = "top")+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))+
theme(text = element_text(size = 20, face="bold"))
Output:
Update2: With breaks you can define limits for the color scale:
#Plot 3
ggplot(df, aes(log(data1), log(data2),color=data3),cex=1.9)+
geom_point() +
scale_colour_gradientn(colours = rainbow(25),breaks = seq(0,100,by=5))+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))+
theme(text = element_text(size = 12, face="bold"),
legend.text = element_text(size = 7, face="bold"))
Output:
Update 3: If you want to have different colors you can mix different palettes like this:
#Plot
ggplot(df, aes(log(data1), log(data2),color=data3),cex=1.9)+
geom_point() +
scale_colour_gradientn(colours = c(viridis::inferno(5),
viridis::plasma(5),
viridis::magma(5),
viridis::viridis(5),
rainbow(5)),breaks = seq(0,100,by=5))+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))+
theme(text = element_text(size = 12, face="bold"),
legend.text = element_text(size = 7, face="bold"))
Output:
The below produces a plot with value labels - from a table containing percentages; NOT raw individual level data. So the stat="identity" argument is used. How would you instruct ggplot to add a % symbol to each value label above the bar in such a case?
#Generate data table
Percent<-c(20,80)
row_labels<-c("Male","Female")
df<-data.frame(row_labels,Percent)
#Plot data table
ggplot(df) + aes(row_labels,Percent,fill=row_labels) + geom_bar(stat="identity") +
labs(x = "",title = " ") + theme(legend.title = element_blank()) + coord_flip()+ theme(legend.position="none") + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_blank(), axis.ticks.y = element_blank(), axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank()) + geom_text(aes(label=round(Percent,digits=1)), hjust = -0.15, size = 3)
I have modified aes of the geom_text in the following way:
ggplot(df, aes(row_labels,Percent,fill=row_labels) ) +
geom_bar(stat="identity") +
geom_text(aes(y=Percent+5, label = paste(Percent, "%")) ) +
labs(x = "",title = " ") +
theme(
legend.position="none",
legend.title = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_blank(),
axis.ticks.y = element_blank(),
axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank()
) +
coord_flip()
Although the order of layers/adjustments does not matter (you have mentioned coord_flip), I have reorganised the code for better fluency.
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))
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())