Problems altering axis label font sizes - r

I have effectively a very simple question.
I am using ggplot2 code to alter the font size of axis text and labels. However, wherever I position the command, none of the changes are visible on the axes. All other commands are working, so I am getting the impression that something is 'overriding' the theme(axis.text..., axis.title...) command.
ggplot(Cannock, aes(x=Capacity,color=CPType)) +
geom_histogram(fill="white",position="identity",binwidth=3,lwd=1) +
labs(title="Cannock Chase",x="Capacity", y = "Count") +
theme(axis.text=element_text(size=14), axis.title=element_text(size=16,face="bold")) +
facet_grid(CPType ~ .) +
geom_vline(data=mu1, aes(xintercept=grp.mean, color=CPType), linetype="dashed",size=1) +
theme_bw() +
theme(axis.line = element_line(colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank()) +
theme(legend.position="none") +
theme(strip.text.y = element_text(size=8, fac[![enter image description here][1]][1]e="bold"), strip.background = element_rect(colour="white", fill="white")) +
coord_cartesian(xlim = c(0,100)) +
theme(strip.background = element_blank(), strip.text = element_blank())
Any pointers for this would be greatly appreciated. Many thanks!

I think it might be that you've called theme_bw() after you change your axis text formatting. Any formatting that you want to change from defaults needs to be changed after calling theme_bw. Additionally, just to be a little cleaner and tighter, you can combine all of your theme arguments into one group so that it's easier to keep track of what you're changing. Does the code below solve the problem?
You also specify strip.text and strip.background twice, with different settings, which is probably not what you want to do.
ggplot(Cannock, aes(x=Capacity,color=CPType)) +
geom_histogram(fill="white",position="identity",binwidth=3,lwd=1) +
labs(title="Cannock Chase",x="Capacity", y = "Count") +
facet_grid(CPType ~ .) +
geom_vline(data=mu1, aes(xintercept=grp.mean, color=CPType), linetype="dashed",size=1) +
theme_bw() +
coord_cartesian(xlim = c(0,100)) +
theme(axis.text=element_text(size=14),
axis.title=element_text(size=16,face="bold"),
axis.line = element_line(colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
legend.position="none",
strip.text.y = element_text(size=8, face="bold"),
strip.text = element_blank(),
strip.background = element_rect(colour="white", fill="white"),
strip.background = element_blank())

Related

When using identity input, how do you get ggplot to display % symbol next to value label

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.

Tight legend border with no title

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

geom_errorbar and geom_facet

The following code does not display properly the error bars:
rf.imp<- read.csv("importances_byaggregations.csv",head=TRUE,sep=",") #Changes when handling the data
rf.imp$flux <- as.character(rf.imp$flux)
rf.imp$flux<-factor(rf.imp$flux,levels=unique(rf.imp$flux))
rf.imp$aggregation <- as.character(rf.imp$aggregation)
rf.imp$aggregation<-factor(rf.imp$aggregation,levels=unique(rf.imp$aggregation))
cbbPalette <- c("#F0E442", "#CC79A7","#E69F00","#56B4E9", "#009E73") # Mimicking Python colors
rf.imp$rel.influence<-rf.imp$rel.influence*100
rf.imp$SD<-rf.imp$SD*100
limits <- aes(ymax = rf.imp$rel.influence + rf.imp$SD, ymin=rf.imp$rel.influence - rf.imp$SD)
ggplot(rf.imp, aes(variable,rel.influence,fill=variable)) +
geom_bar(stat="identity",position="dodge") + scale_fill_manual(values=cbbPalette)+
theme_bw(base_size = 32, base_family = "Helvetica")+
xlab("")+
ylab("Variable importance (%)")+
facet_grid(aggregation~flux)+
geom_errorbar(limits, width=0.5)+
scale_y_continuous(limits=c(-10,90))+
theme(legend.position="none",
strip.text.x = element_blank(),
strip.text.y = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
strip.background = element_blank(),
panel.border = element_rect(colour = "black"),
panel.border = element_rect(colour = "black", fill=NA, size=1))
I would like to obtaine the following figure, but with the geom_facets swaped.
However, I get something like this:
Am I doing something wrong?
Thanks!
Your minimal example is a little too long for me to dig into, but I strongly suspect that your problem comes from using absolute (rf.imp$...) references in your error bar limits. If you use
geom_errorbar(aes(ymax=rel.influence+SD,
ymin=rel.influence-SD), width=0.5)
I think that will fix the problem.

Add panel border to 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)

ggplot2 theme with no axes or grid

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())

Resources