I have visualized some data as a donut chart. Here is the code:
ggplot(region_average, aes(ymax=ymax, ymin=ymin, xmax=4, xmin=3, fill=state_code)) +
geom_rect() +
geom_label_repel( x=3.5, aes(y=labelPosition, label=label), size=3) +
scale_fill_brewer(palette=4) +
coord_polar(theta="y") +
xlim(c(2, 4)) +
theme(legend.position = "none",
panel.grid = element_blank(),
panel.background = element_blank(),
axis.ticks = element_blank()) +
labs(title = "Regional Distribution of Ad Reach",
subtitle = "Overall distribution for all ads studied")+
theme(
panel.grid = element_blank(),
axis.title.y = element_blank(),
axis.title.x = element_blank(),
axis.text = element_text(color=txt_col, size=8, hjust = 0.4),
axis.ticks = element_blank(),
axis.title.y.left = element_blank(),
panel.background = element_rect(fill = bg, color = bg),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.border=element_blank(),
plot.title = element_text(hjust=0,size=24, color=txt_col,lineheight=.8, face="bold", margin=margin(0,0,0,0)),
plot.subtitle = element_text(hjust=0,size=14, color=txt_col, margin=margin(5,0,20,0)),
plot.title.position = "plot",
plot.caption = element_text(hjust=.5, margin=margin(20,0,0,0), size=8, color=txt_col, face="bold"),
plot.background = element_rect(color=bg, fill=bg),
plot.margin = margin(30,30,30,30),
legend.title = element_blank(),
legend.position = "none"
)
This is the picture it produces
How can I get rid of the 2.0 ... 4.0 on the left hand side and the 0.00/1 ...0.75 around the donut?
Thank you!
As #Allan Cameron says in the comments, axis.text = element_blank()resolves the issue.
Related
I want to decrease the distance between the rows on my lollipop graph. Does anyone know how I would do this? Any assistance is appreciated it.
df <- data.frame(
parameters = c("Posting written messages online", "Text messages", "Phone calls",
"Online learning system", "Online video chat","Blackboard Collaborate"),
value = c(32,29, 19, 53, 51,26))
df %>%
ggplot(aes(x= parameters, y=value)) +
geom_segment( aes(x= parameters, xend= parameters, y=0, yend=value),
color="dodgerblue4", size=2) +
geom_point( color="darkorange2", size=4.2, alpha=0.9) +
geom_text(aes(label = paste0(value,"%")), hjust = -0.3, size=3.8,family="Arial") +
expand_limits(y = 100)+
theme_light() +
coord_flip() +
theme(
plot.margin = margin(1, 1, 4, 1.1, "cm"),
panel.grid.major.y = element_blank(),
axis.text.y = element_text(color = 'black', , size = 12, hjust = 1),
panel.border = element_blank(),
axis.ticks.y = element_blank(),
axis.ticks.x = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.minor.y = element_blank(),
axis.text.x = element_blank(),
axis.title.x.bottom = element_blank()
)
Try this for reordering:
library(ggplot2)
#Code
df %>%
ggplot(aes(x= reorder(parameters,value), y=value)) +
geom_segment( aes(x= reorder(parameters,value), xend= parameters, y=0, yend=value),
color="dodgerblue4", size=2) +
geom_point( color="darkorange2", size=4.2, alpha=0.9) +
geom_text(aes(label = paste0(value,"%")), hjust = -0.3, size=3.8,family="Arial") +
expand_limits(y = 100)+
theme_light() +
coord_flip() +
theme(
plot.margin = margin(1, 1, 4, 1.1, "cm"),
panel.grid.major.y = element_blank(),
axis.text.y = element_text(color = 'black', , size = 12, hjust = 1),
panel.border = element_blank(),
axis.ticks.y = element_blank(),
axis.ticks.x = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.minor.y = element_blank(),
axis.text.x = element_blank(),
axis.title.x.bottom = element_blank()
)
Output:
My facet_wrap plot works fine with points, but when I want to change these points to bars it does not work. scale_y_reverse.
ggplot(data=depth, aes(x=value, y=Depth)) + facet_wrap(~variable, ncol=7) + labs(x="Macroalgae genes (TPM)", y="Depth (m)") +scale_y_reverse(expand = c(0.03, 0.03), labels=layers) + geom_point(aes(color = variable), shape=1) + theme_bw() + theme(plot.margin=unit(x=c(0.3,0.8,0.3,0.3), units="cm"),
axis.text=element_text(size=13, family="Times New Roman", color="black"),
axis.title = element_text(size = 20, family="Times New Roman"), strip.background = element_blank(),
strip.text = element_text(face="bold", size=13, lineheight=5.0, family="Times New Roman"),
legend.position="none", panel.grid.major = element_blank(), panel.border = element_blank(),
panel.spacing.y=unit(0.3, "lines"), panel.spacing.x=unit(0.15, "lines"),panel.grid.minor = element_blank(),
panel.background = element_blank(), plot.background = element_rect(fill = "transparent",colour = NA),
axis.line = element_line(colour = "black"))
How do I change the points to bars?
Plot is here
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))